-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/commonMain/kotlin/uk/gibby/driver/rpc/functions/LiveQuery.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package uk.gibby.driver.rpc.functions | ||
|
||
import kotlinx.coroutines.channels.consumeEach | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.consumeAsFlow | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.add | ||
import kotlinx.serialization.json.buildJsonArray | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import uk.gibby.driver.Surreal | ||
import uk.gibby.driver.rpc.model.LiveQueryAction | ||
import uk.gibby.driver.surrealJson | ||
|
||
suspend fun Surreal.live(table: String): Flow<LiveQueryAction> { | ||
val response = sendRequest("live", buildJsonArray{ add(table) }) | ||
val id: String = surrealJson.decodeFromJsonElement(response) | ||
return subscribe(id).consumeAsFlow() | ||
} |
7 changes: 7 additions & 0 deletions
7
src/commonMain/kotlin/uk/gibby/driver/rpc/model/LiveQueryAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package uk.gibby.driver.rpc.model | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonObject | ||
|
||
@Serializable | ||
data class LiveQueryAction(val action: String, val id: String, val result: JsonObject? = null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import uk.gibby.driver.Surreal | ||
import uk.gibby.driver.rpc.functions.* | ||
import uk.gibby.driver.rpc.model.bind | ||
import utils.cleanDatabase | ||
import kotlin.test.Test | ||
|
||
class LiveQueryTest { | ||
@Test | ||
fun testLiveQuery() = runTest { | ||
cleanDatabase() | ||
val connection = Surreal("localhost", 8000) | ||
connection.connect() | ||
connection.signin("root", "root") | ||
connection.use("test", "test") | ||
val incoming = connection.live("test") | ||
connection.create("test").content(buildJsonObject { put("thing", "thing") }) | ||
println(incoming.first()) | ||
} | ||
} |