JetBrains / JetBrains/Exposed

Use Kotlinx Coroutines SharedFlow to subscribe to updates

Open
#1,560 3 comments 18 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
9.3k
Forks
798
Avg merge
4d 2h
Merged PRs (30d)
26

Description

I would like to use a Kotlinx Coroutines [`SharedFlow`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/) to subscribe to entity updates.

### Example usage

Here's a very quick demo showing how I'd like to use it:

```kotlin
object StarWarsFilms : IntIdTable() {
val sequelId: Column = integer("sequel_id").uniqueIndex()
val name: Column = varchar("name", 50)
val director: Column = varchar("director", 50)
}

val starWarsFilmNames: SharedFlow =
StarWarsFilms
.select {
StarWarsFilms.sequelId eq 8
}
.sharedFlow {
// map the entity
it[StarWarsFilms.name]
}

suspend fun main() {
starWarsFilmNames.onEach { name ->
println("Star Wars film name $name") // will print the name every time a new entity is added
}.launchIn(this)
}
```

### Updating

I would also like to be able to push updates into a table using a [`MutableSharedFlow`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-mutable-shared-flow/). Although I think this can be achieved already, having a built-in library function would help with ergonomics.

### Restrictions

I understand that there are issues with the database drivers being inherently synchronous (['Working with Coroutines'](https://github.com/JetBrains/Exposed/wiki/Transactions#Working-with-Coroutines), https://github.com/JetBrains/Exposed/issues/1551#issuecomment-1198542569). However, I would like it if this functionality was implemented as best-effort, even if the underlying driver was not optimally.

### Current options?

I couldn't see any easy way to do this presently.

I couldn't find any 'subscribe' or 'listening' options described in the [DSL](https://github.com/JetBrains/Exposed/wiki/DSL) or [DAO](https://github.com/JetBrains/Exposed/wiki/DAO) docs, and I couldn't see [any existing usage of a `Flow` in the project](https://github.com/search?q=repo%3AJetBrains%2FExposed+flow+language%3Akotlin&type=code).

SqlDelight has similar functionality: https://cashapp.github.io/sqldelight/jvm_sqlite/coroutines/

### Related

* https://github.com/JetBrains/Exposed/issues/326#issuecomment-1199431112
* https://github.com/JetBrains/Exposed/issues/662
* https://github.com/JetBrains/Exposed/issues/1551#issuecomment-1198542569

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the Kotlinx Coroutines SharedFlow and MutableSharedFlow documentation, then inspect the Exposed DSL and DAO documentation and the related issues 326, 662, and 1551. Define how entity subscriptions and table updates should work despite synchronous database drivers. Done means a documented, usable API for observing updates and pushing updates, with behavior and limitations established.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.