Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…river into live_queries

# Conflicts:
#	src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt
  • Loading branch information
mnbjhu committed Aug 23, 2023
2 parents 55678f5 + 865e6ff commit 96df546
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ on:

permissions:
contents: write
checks: write
pull-requests: write

jobs:
build:
Expand All @@ -39,6 +41,11 @@ jobs:
VERSION=$(git tag --sort=committerdate | tail -1)
echo Generating API documentation for version $VERSION
./gradlew -Pversion=$VERSION dokkaHtml
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: |
build/test-results/**/*.xml
- name: Deploy API documentation to Github Pages
if: ${{ github.ref == 'refs/heads/master' && github.event_name != 'pull_request' }}
uses: JamesIves/github-pages-deploy-action@v4
Expand Down
11 changes: 7 additions & 4 deletions src/commonMain/kotlin/uk/gibby/driver/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
14 changes: 13 additions & 1 deletion src/commonMain/kotlin/uk/gibby/driver/rpc/Delete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gibby.driver.rpc
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/Select.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package uk.gibby.driver.rpc

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)
}
23 changes: 17 additions & 6 deletions src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import uk.gibby.driver.model.query.Bind
import uk.gibby.driver.model.JsonPatch
import uk.gibby.driver.surrealJson
import kotlin.jvm.JvmName
import uk.gibby.driver.model.Thing


/**
Expand Down Expand Up @@ -154,7 +155,7 @@ fun Surreal.update(table: String) = UpdateBuilder(table, this)
* @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 +167,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 +197,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 +227,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 +265,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 +281,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)

1 change: 0 additions & 1 deletion src/commonTest/kotlin/CreateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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.model.query.bind
import uk.gibby.driver.rpc.create
Expand Down Expand Up @@ -45,4 +47,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 @@ -37,4 +37,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 @@ -40,4 +40,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 @@ -41,4 +41,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)
}
}

0 comments on commit 96df546

Please sign in to comment.