-
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.
Merge pull request #12 from mnbjhu/insert_support
Adding insert statement
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 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
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,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<JsonObject>): JsonArray { | ||
val response = db.sendRequest("insert", buildJsonArray { | ||
add(table) | ||
addJsonArray { data.forEach { add(it) } } | ||
}) | ||
return response as JsonArray | ||
} | ||
|
||
suspend inline fun <reified T> content(data: List<T>): List<T> { | ||
val response = jsonContent(data.map { surrealJson.encodeToJsonElement(it) as JsonObject }) | ||
return surrealJson.decodeFromJsonElement(response) | ||
} | ||
|
||
suspend inline fun <reified T> content(vararg data: T): List<T> { | ||
val response = jsonContent(data.map { surrealJson.encodeToJsonElement(it) as JsonObject }) | ||
return surrealJson.decodeFromJsonElement(response) | ||
} | ||
} | ||
|
||
fun Surreal.insert(table: String): InsertBuilder { | ||
return InsertBuilder(table, this) | ||
} |
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,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) | ||
} | ||
} |