From 5ee1cd2d403cd175de4e161d67fd087a11894513 Mon Sep 17 00:00:00 2001 From: mnbjhu Date: Mon, 4 Sep 2023 17:49:52 +0100 Subject: [PATCH] Renamed existing patch and added support for new patch --- .../kotlin/uk/gibby/driver/rpc/Update.kt | 73 ++++++++++++++++++- src/commonTest/kotlin/LiveQueryTest.kt | 6 +- src/commonTest/kotlin/PatchTest.kt | 63 ++++++++++++++-- 3 files changed, 130 insertions(+), 12 deletions(-) diff --git a/src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt b/src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt index ab52ec1..9db3ffa 100644 --- a/src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt +++ b/src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt @@ -116,15 +116,50 @@ class UpdateBuilder(private val table: String, private val db: Surreal) { return surrealJson.decodeFromJsonElement(response) } + /** + * Patch as json + * + * Patches all records in a table with the given Json patch. + * + * @param patchBuilder A json patch DSL to build the patch + * @receiver The update builder + * @return The records changed by the patch + */ + suspend fun patchAsJson(patchBuilder: JsonPatch.Builder.() -> Unit): JsonArray { + val builder = JsonPatch.Builder() + builder.patchBuilder() + val response = db.sendRequest("patch", buildJsonArray { + add(table) + add(surrealJson.encodeToJsonElement(builder.build())) + add(false) + }) + return surrealJson.decodeFromJsonElement(response) + } + /** * Patch * * Patches all records in a table with the given Json patch. * + * @param T The type of the returned records + * @param patchBuilder A json patch DSL to build the patch + * @receiver The update builder + * @return The records changed by the patch + */ + suspend inline fun patch(noinline patchBuilder: JsonPatch.Builder.() -> Unit): List { + val response = patchAsJson(patchBuilder) + return surrealJson.decodeFromJsonElement(response) + } + + /** + * Patch with diff + * + * Patches all records in a table with the given Json patch. + * * @param patchBuilder A json patch DSL to build the patch * @return The patches applied to the records */ - suspend fun patch(patchBuilder: JsonPatch.Builder.() -> Unit): List> { + suspend fun patchWithDiff(patchBuilder: JsonPatch.Builder.() -> Unit): List> { val builder = JsonPatch.Builder() builder.patchBuilder() val response = db.sendRequest("patch", buildJsonArray { @@ -260,9 +295,42 @@ class UpdateIdBuilder(private val id: String, private val db: Surreal) { * Patches a single record in a table with the given Json patch. * * @param patchBuilder A json patch DSL to build the patch + * @return The records changed by the patch + */ + suspend fun patchAsJson(patchBuilder: JsonPatch.Builder.() -> Unit): JsonElement { + val builder = JsonPatch.Builder() + builder.patchBuilder() + return db.sendRequest("patch", buildJsonArray { + add(id) + add(surrealJson.encodeToJsonElement(builder.build())) + add(false) + }) + } + + /** + * Patch + * + * Patches a single record in a table with the given Json patch. + * + * @param T The type of the returned records + * @param patchBuilder A json patch DSL to build the patch + * @receiver The update builder + * @return The records changed by the patch + */ + suspend inline fun patch(noinline patchBuilder: JsonPatch.Builder.() -> Unit): T { + val response = patchAsJson(patchBuilder) + return surrealJson.decodeFromJsonElement(response) + } + + /** + * Patch with diff + * + * Patches a single record in a table with the given Json patch. + * + * @param patchBuilder A json patch DSL to build the patch * @return The patches applied to the record */ - suspend fun patch(patchBuilder: JsonPatch.Builder.() -> Unit): List { + suspend fun patchWithDiff(patchBuilder: JsonPatch.Builder.() -> Unit): List { val builder = JsonPatch.Builder() builder.patchBuilder() val result = db.sendRequest("patch", buildJsonArray { @@ -272,7 +340,6 @@ class UpdateIdBuilder(private val id: String, private val db: Surreal) { }) return surrealJson.decodeFromJsonElement(result) } - } /** diff --git a/src/commonTest/kotlin/LiveQueryTest.kt b/src/commonTest/kotlin/LiveQueryTest.kt index 8e31dff..f61f9e8 100644 --- a/src/commonTest/kotlin/LiveQueryTest.kt +++ b/src/commonTest/kotlin/LiveQueryTest.kt @@ -22,7 +22,7 @@ class LiveQueryTest { val incoming = connection.observeLiveQuery("test") connection.create("test", "first").content(TestClass("thing", 1)) connection.create("test", "second").content(TestClass("thing", 2)) - connection.update("test", "first").patch { + connection.update("test", "first").patchWithDiff { replace("myText", "thing2") } connection.delete("test", "second") @@ -56,7 +56,7 @@ class LiveQueryTest { connection.create("test", "first").content(TestClass("thing", 1)) connection.create("test", "second").content(TestClass("thing", 2)) - connection.update("test", "first").patch { + connection.update("test", "first").patchWithDiff { replace("myText", "thing2") } connection.delete("test", "second") @@ -90,7 +90,7 @@ class LiveQueryTest { connection.create("test", "first").content(TestClass("thing", 1)) connection.create("test", "second").content(TestClass("thing", 2)) - connection.update("test", "first").patch { + connection.update("test", "first").patchWithDiff { replace("myText", "thing2") } connection.delete("test", "second") diff --git a/src/commonTest/kotlin/PatchTest.kt b/src/commonTest/kotlin/PatchTest.kt index 12e56f4..eb32358 100644 --- a/src/commonTest/kotlin/PatchTest.kt +++ b/src/commonTest/kotlin/PatchTest.kt @@ -8,7 +8,7 @@ import kotlin.test.assertEquals class PatchTest { @Test - fun testBasicPatch() = runTest { + fun testBasicPatchReturningDiff() = runTest { cleanDatabase() val connection = Surreal("localhost") connection.connect() @@ -16,21 +16,21 @@ class PatchTest { connection.use("test", "test") connection.create("test").content(TestClass("first", 1)) connection.create("test").content(TestClass("second", 2)) - val result = connection.update("test").patch { + val result = connection.update("test").patchWithDiff { replace("myText", "updated") } assertEquals(2, result.size) } @Test - fun testPatchWithId() = runTest { + fun testPatchWithIdReturningDiff() = runTest { cleanDatabase() val connection = Surreal("localhost") connection.connect() connection.signin("root", "root") connection.use("test", "test") connection.create("test", "123").content(TestClass("first", 1)) - connection.update("test", "123").patch { + connection.update("test", "123").patchWithDiff { replace("myText", "updated") } val result = connection.select("test", "123") @@ -38,17 +38,68 @@ class PatchTest { } @Test - fun testPatchWithThing() = runTest { + fun testPatchWithThingReturningDiff() = runTest { cleanDatabase() val connection = Surreal("localhost") connection.connect() connection.signin("root", "root") connection.use("test", "test") val thing = connection.create("test").content(TestClass("first", 1)) - connection.update(thing.id).patch { + connection.update(thing.id).patchWithDiff { replace("myText", "updated") } val result = connection.select(thing.id) assertEquals("updated", result.myText) } + + @Test + fun testPatchWithIdReturningRecords() = runTest { + cleanDatabase() + val connection = Surreal("localhost") + connection.connect() + connection.signin("root", "root") + connection.use("test", "test") + connection.create("test", "123").content(TestClass("first", 1)) + connection.update("test", "123").patch { + replace("myText", "updated") + } + val result = connection.select("test", "123") + assertEquals("updated", result.myText) + assertEquals(1, result.myNumber) + } + + @Test + fun testPatchWithThingReturningRecords() = runTest { + cleanDatabase() + val connection = Surreal("localhost") + connection.connect() + connection.signin("root", "root") + connection.use("test", "test") + val thing = connection.create("test").content(TestClass("first", 1)) + connection.update(thing.id).patch { + replace("myText", "updated") + } + val result = connection.select(thing.id) + assertEquals("updated", result.myText) + assertEquals(1, result.myNumber) + } + + @Test + fun testPatchReturningRecords() = runTest { + cleanDatabase() + val connection = Surreal("localhost") + connection.connect() + connection.signin("root", "root") + connection.use("test", "test") + connection.create("test").content(TestClass("first", 1)) + connection.create("test").content(TestClass("second", 2)) + val result = connection.update("test").patch { + replace("myText", "updated") + }.sortedBy { it.myNumber } + assertEquals(2, result.size) + assertEquals("updated", result[0].myText) + assertEquals(1, result[0].myNumber) + assertEquals("updated", result[1].myText) + assertEquals(2, result[1].myNumber) + } } \ No newline at end of file