From b47b49f2265b776acc3772e7af7ab9d543768f6e Mon Sep 17 00:00:00 2001 From: james Date: Sun, 10 Sep 2023 10:57:57 +0100 Subject: [PATCH] Adding insert statement --- .../kotlin/uk/gibby/driver/Surreal.kt | 2 -- .../kotlin/uk/gibby/driver/rpc/Insert.kt | 30 +++++++++++++++++ src/commonTest/kotlin/InsertTest.kt | 32 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/commonMain/kotlin/uk/gibby/driver/rpc/Insert.kt create mode 100644 src/commonTest/kotlin/InsertTest.kt diff --git a/src/commonMain/kotlin/uk/gibby/driver/Surreal.kt b/src/commonMain/kotlin/uk/gibby/driver/Surreal.kt index 63a593f..fd66383 100644 --- a/src/commonMain/kotlin/uk/gibby/driver/Surreal.kt +++ b/src/commonMain/kotlin/uk/gibby/driver/Surreal.kt @@ -41,7 +41,6 @@ class Surreal(private val host: String, private val port: Int = 8000) { context.launch { it.incoming.receiveAsFlow().collect { it as Frame.Text - println(it.readText()) val response = try { surrealJson.decodeFromString(RpcResponseSerializer, it.readText()) } catch (e: Exception) { @@ -97,7 +96,6 @@ class Surreal(private val host: String, private val port: Int = 8000) { val request = RpcRequest(id, method, params) val channel = Channel(1) requests[id] = channel - println(request) (connection ?: throw Exception("SurrealDB: Websocket not connected")).sendSerialized(request) return channel.receive() } diff --git a/src/commonMain/kotlin/uk/gibby/driver/rpc/Insert.kt b/src/commonMain/kotlin/uk/gibby/driver/rpc/Insert.kt new file mode 100644 index 0000000..ffd97c7 --- /dev/null +++ b/src/commonMain/kotlin/uk/gibby/driver/rpc/Insert.kt @@ -0,0 +1,30 @@ +package uk.gibby.driver.rpc + +import kotlinx.serialization.json.* +import uk.gibby.driver.Surreal +import uk.gibby.driver.surrealJson + +class InsertBuilder(private val table: String, private val db: Surreal) { + + suspend fun jsonContent(data: List): JsonArray { + val response = db.sendRequest("insert", buildJsonArray { + add(table) + addJsonArray { data.forEach { add(it) } } + }) + return response as JsonArray + } + + suspend inline fun content(data: List): List { + val response = jsonContent(data.map { surrealJson.encodeToJsonElement(it) as JsonObject }) + return surrealJson.decodeFromJsonElement(response) + } + + suspend inline fun content(vararg data: T): List { + val response = jsonContent(data.map { surrealJson.encodeToJsonElement(it) as JsonObject }) + return surrealJson.decodeFromJsonElement(response) + } +} + +fun Surreal.insert(table: String): InsertBuilder { + return InsertBuilder(table, this) +} \ No newline at end of file diff --git a/src/commonTest/kotlin/InsertTest.kt b/src/commonTest/kotlin/InsertTest.kt new file mode 100644 index 0000000..495ae30 --- /dev/null +++ b/src/commonTest/kotlin/InsertTest.kt @@ -0,0 +1,32 @@ +import kotlinx.coroutines.test.runTest +import uk.gibby.driver.Surreal +import uk.gibby.driver.rpc.insert +import uk.gibby.driver.rpc.signin +import uk.gibby.driver.rpc.use +import utils.cleanDatabase +import kotlin.test.Test +import kotlin.test.assertEquals + +class InsertTest { + + @Test + fun basicInsertTest() = runTest { + cleanDatabase() + val db = Surreal("localhost", 8000) + db.connect() + db.signin("root", "root") + db.use("test", "test") + val created = db.insert("test").content( + TestClass("first", 1), + TestClass("second", 2), + TestClass("third", 3) + ) + assertEquals(3, created.size) + assertEquals("first", created[0].myText) + assertEquals(1, created[0].myNumber) + assertEquals("second", created[1].myText) + assertEquals(2, created[1].myNumber) + assertEquals("third", created[2].myText) + assertEquals(3, created[2].myNumber) + } +} \ No newline at end of file