Kotlin: Database
Run SQL with bound parameters and select, insert, update, delete and call database functions with the Kotlin SDK.
Run SQL with bound parameters and select, insert, update, delete and call database functions. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
SqlResult
The result of a SQL statement: the rows, and the raw response.
class SqlResult(val rows: List<Map<String, Any?>>, val raw: Map<String, Any?>)SqlResult.from
fun from(json: Any?): SqlResultSovariaDatabase
The project's Postgres database. Every value is a bound parameter ($1, $2, …), so user input never becomes SQL; table and column names are validated and quoted. Row security and access policies apply to app users exactly as on the web.
class SovariaDatabase(private val client: Sovaria)SovariaDatabase.sql
Run one statement: sql("SELECT * FROM todos WHERE owner = $1", listOf(userId)).
suspend fun sql(statement: String, params: List<Any?> = emptyList()): SqlResultSovariaDatabase.rpc
Call one of your Postgres functions: with the arguments by name. A function that returns one value returns that value; one that returns rows returns a list of maps.
suspend fun rpc(
name: String,
args: Map<String, Any?> = emptyMap(),
get: Boolean = false,
query: Map<String, Any?>? = null,
): Any?App users need an access policy that allows fn:<name>. get calls with GET and the arguments in the query string (functions that only read); query adds PostgREST parameters (select, order, limit, filters such as mapOf("status" to "eq.open")) for functions that return rows.
val n = sovaria.db.rpc("post_count", mapOf("u" to userId)) as? Number
SovariaDatabase.select
SELECT with equality filters.
suspend fun select(
table: String, columns: List<String> = listOf(""), where: Map<String, Any?> = emptyMap(),
orderBy: String? = null, descending: Boolean = false, limit: Int? = null, offset: Int? = null,
): List<Map<String, Any?>>SovariaDatabase.insert
INSERT … RETURNING *.
suspend fun insert(table: String, values: Map<String, Any?>): List<Map<String, Any?>>SovariaDatabase.update
UPDATE … SET … WHERE … RETURNING * (a filter is required).
suspend fun update(table: String, values: Map<String, Any?>, where: Map<String, Any?>): List<Map<String, Any?>>SovariaDatabase.delete
DELETE … WHERE … RETURNING * (a filter is required).
suspend fun delete(table: String, where: Map<String, Any?>): List<Map<String, Any?>>SovariaDatabase.quote
A safe identifier (schema.table allowed), double-quoted.
fun quote(name: String): StringThrows on anything that isn't a plain identifier, so a table or column name can never smuggle SQL (matches the other clients).