Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for selecting updating and deleting thing #5

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/commonMain/kotlin/uk/gibby/driver/rpc/functions/Delete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gibby.driver.rpc.functions
import kotlinx.serialization.json.add
import kotlinx.serialization.json.buildJsonArray
import uk.gibby.driver.Surreal
import uk.gibby.driver.rpc.model.Thing

/**
* Delete
Expand All @@ -25,4 +26,15 @@ suspend fun Surreal.delete(table: String) {
*/
suspend fun Surreal.delete(table: String, id: String) {
sendRequest("delete", buildJsonArray { add("$table:$id") })
}
}

/**
* Delete
*
* Deletes a specific record in a table
*
* @param id The id of the record to delete
*/
suspend fun Surreal.delete(id: Thing<*>) {
sendRequest("delete", buildJsonArray { add(id.id) })
}
38 changes: 31 additions & 7 deletions src/commonMain/kotlin/uk/gibby/driver/rpc/functions/Select.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package uk.gibby.driver.rpc.functions

import kotlinx.serialization.json.*
import uk.gibby.driver.Surreal
import uk.gibby.driver.rpc.model.Thing
import uk.gibby.driver.surrealJson
import kotlin.jvm.JvmName

/**
* Select
Expand All @@ -12,8 +12,7 @@ import kotlin.jvm.JvmName
*
* @return The records in the table
*/
@JvmName("selectJson")
suspend fun Surreal.select(table: String): JsonArray {
suspend fun Surreal.selectAsJson(table: String): JsonArray {
return sendRequest("select", buildJsonArray { add(table) }) as JsonArray
}

Expand All @@ -26,7 +25,7 @@ suspend fun Surreal.select(table: String): JsonArray {
* @return The records in the table
*/
suspend inline fun <reified T>Surreal.select(table: String): List<T> {
val response = select(table)
val response = selectAsJson(table)
return surrealJson.decodeFromJsonElement(response)
}

Expand All @@ -39,11 +38,22 @@ suspend inline fun <reified T>Surreal.select(table: String): List<T> {
* @param id The id of the record to select
* @return The record in the table
*/
@JvmName("selectIdJson")
suspend fun Surreal.select(table: String, id: String): JsonObject {
suspend fun Surreal.selectAsJson(table: String, id: String): JsonObject {
return sendRequest("select", buildJsonArray { add("$table:$id") }) as JsonObject
}

/**
* Select
*
* Select a specific record in a table.
*
* @param id The id of the record to select
* @return The record in the table
*/
suspend fun Surreal.selectAsJson(id: Thing<*>): JsonObject {
return sendRequest("select", buildJsonArray { add(id.id) }) as JsonObject
}


/**
* Select
Expand All @@ -56,6 +66,20 @@ suspend fun Surreal.select(table: String, id: String): JsonObject {
* @return The record in the table
*/
suspend inline fun <reified T>Surreal.select(table: String, id: String): T {
val response = select(table, id)
val response = selectAsJson(table, id)
return surrealJson.decodeFromJsonElement(response)
}

/**
* Select
*
* Select a specific record in a table.
*
* @param id The id of the record to select
* @param T The type of the record
* @return The record in the table
*/
suspend inline fun <reified T>Surreal.select(id: Thing<T>): T {
val response = selectAsJson(id)
return surrealJson.decodeFromJsonElement(response)
}
24 changes: 17 additions & 7 deletions src/commonMain/kotlin/uk/gibby/driver/rpc/functions/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.serialization.json.*
import uk.gibby.driver.Surreal
import uk.gibby.driver.rpc.model.Bind
import uk.gibby.driver.rpc.model.JsonPatch
import uk.gibby.driver.rpc.model.Thing
import uk.gibby.driver.surrealJson
import kotlin.jvm.JvmName

Expand Down Expand Up @@ -149,12 +150,11 @@ fun Surreal.update(table: String) = UpdateBuilder(table, this)
*
* This class is used to build update requests affecting a single record in a table.
*
* @property table The table to update
* @property id The id of the record to update
* @property db The database connection to use
* @constructor Creates an update builder for the given table and id
*/
class UpdateIdBuilder(private val table: String, private val id: String, private val db: Surreal) {
class UpdateIdBuilder(private val id: String, private val db: Surreal) {

/**
* Content
Expand All @@ -166,7 +166,7 @@ class UpdateIdBuilder(private val table: String, private val id: String, private
*/
suspend fun jsonContent(data: JsonElement): JsonObject {
return db.sendRequest("update", buildJsonArray {
add("$table:$id")
add(id)
add(data)
}) as JsonObject
}
Expand Down Expand Up @@ -196,7 +196,7 @@ class UpdateIdBuilder(private val table: String, private val id: String, private
@JvmName("mergeJson")
suspend fun merge(data: JsonObject): JsonObject {
return db.sendRequest("merge", buildJsonArray {
add("$table:$id")
add(id)
add(data)
}) as JsonObject
}
Expand Down Expand Up @@ -226,7 +226,7 @@ class UpdateIdBuilder(private val table: String, private val id: String, private
@JvmName("mergeBindJson")
suspend fun merge(vararg data: Bind): JsonObject {
return db.sendRequest("merge", buildJsonArray {
add("$table:$id")
add(id)
add(
buildJsonObject {
data.forEach {
Expand Down Expand Up @@ -264,7 +264,7 @@ class UpdateIdBuilder(private val table: String, private val id: String, private
val builder = JsonPatch.Builder()
builder.patchBuilder()
val result = db.sendRequest("patch", buildJsonArray {
add("$table:$id")
add(id)
add(surrealJson.encodeToJsonElement(builder.build()))
})
return surrealJson.decodeFromJsonElement(result)
Expand All @@ -280,4 +280,14 @@ class UpdateIdBuilder(private val table: String, private val id: String, private
* @param table The table to update
* @param id The id of the record to update
*/
fun Surreal.update(table: String, id: String) = UpdateIdBuilder(table, id, this)
fun Surreal.update(table: String, id: String) = UpdateIdBuilder("$table:$id", this)

/**
* Update
*
* Creates an update builder for the given table and id.
*
* @param id The id of the record to update
*/
fun Surreal.update(id: Thing<*>) = UpdateIdBuilder(id.id, this)

11 changes: 7 additions & 4 deletions src/commonMain/kotlin/uk/gibby/driver/rpc/model/Thing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.decodeStructure
import kotlin.jvm.JvmInline


/**
Expand All @@ -22,10 +23,12 @@ import kotlinx.serialization.encoding.decodeStructure
* @property id the id of the thing
*/
@Serializable(with = ThingSerializer::class)
sealed class Thing<T> {
abstract val id: String
data class Reference<T>(override val id: String): Thing<T>()
data class Record<T>(override val id: String, val result: T): Thing<T>()
sealed interface Thing<T> {
val id: String

@JvmInline
value class Reference<T>(override val id: String): Thing<T>
data class Record<T>(override val id: String, val result: T): Thing<T>
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/commonTest/kotlin/CreateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,5 @@ class CreateTest {
val linked = other.linked as Thing.Record<TestClass>
assertEquals("test", linked.result.myText)
assertEquals(1, linked.result.myNumber)

}
}
13 changes: 13 additions & 0 deletions src/commonTest/kotlin/DeleteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ class DeleteTest {
val result = connection.select<TestClass>("test")
assertEquals(0, result.size)
}

@Test
fun testDeleteThing() = 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.delete(thing.id)
val result = connection.select<TestClass>("test")
assertEquals(0, result.size)
}
}
56 changes: 56 additions & 0 deletions src/commonTest/kotlin/MergeTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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
Expand Down Expand Up @@ -42,4 +44,58 @@ class MergeTest {
assertEquals("updated", result.myText)
assertEquals(1, result.myNumber)
}

@Test
fun testMergeWithThing() = 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))
val result = connection.update(thing.id)
.merge<TestClass>(
bind("myText", "updated"),
)
assertEquals("updated", result.myText)
assertEquals(1, result.myNumber)
}

@Test
fun testMergeJson() = 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")
.merge<TestClass>(buildJsonObject {
put("myText", "updated")
put("myNumber", 2)
})
assertEquals(2, result.size)
result.forEach {
assertEquals("updated", it.myText)
assertEquals(2, it.myNumber)
}
}

@Test
fun testMergeJsonWithThing() = 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))
val result = connection.update(thing.id)
.merge<TestClass>(buildJsonObject {
put("myText", "updated")
put("myNumber", 2)
})
assertEquals("updated", result.myText)
assertEquals(2, result.myNumber)
}
}
15 changes: 15 additions & 0 deletions src/commonTest/kotlin/PatchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ class PatchTest {
val result = connection.select<TestClass>("test", "123")
assertEquals("updated", result.myText)
}

@Test
fun testPatchWithThing() = 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)
}
}
13 changes: 13 additions & 0 deletions src/commonTest/kotlin/SelectTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ class SelectTest {
assertEquals("first", result.myText)
assertEquals(1, result.myNumber)
}

@Test
fun testSelectThing() = 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))
val result = connection.select<TestClass>(thing.id)
assertEquals("first", result.myText)
assertEquals(1, result.myNumber)
}
}
12 changes: 12 additions & 0 deletions src/commonTest/kotlin/UpdateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ class UpdateTest {
assertEquals(-1, result.myNumber)
}

@Test
fun testUpdateThing() = 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))
val result = connection.update(thing.id).content(TestClass("updated", -1))
assertEquals("updated", result.myText)
assertEquals(-1, result.myNumber)
}
}