• Start

Languages

/

Kotlin

/

API Reference

/

Core

Query builders

The fluent query builders, expression helpers, and surql DSL of the SurrealDB Kotlin SDK.

The CRUD methods on SurrealClient return fluent builders that compile to SurrealQL. Each builder is refined with chainable methods and terminated with await() (raw JsonElement) or the typed awaitAs<T>() extension.

Source: surrealdb.kotlin

Import

import com.surrealdb.kotlin.query.awaitAs
import com.surrealdb.kotlin.query.field
import com.surrealdb.kotlin.query.gte

Executes the builder and returns the unwrapped result of the first statement as a JsonElement.

Method Syntax

builder.await()

Returns: JsonElement

Executes the builder and returns the raw [{ status, result, time, type }] response envelope.

Method Syntax

builder.awaitRaw()

Returns: JsonElement

An inline extension that executes the builder and decodes the result into T using kotlinx.serialization. Available on every builder.

Method Syntax

builder.awaitAs<T>()

Returns: T

Example

val people: List<Person> = client.select(Table("person")).awaitAs()

Compiles the builder to a BoundQuery without executing it — useful for inspection or composing with .query().

Method Syntax

builder.compile()

Returns: BoundQuery

Returned by .select(what).

MethodDescription
.fields(vararg names)Selects specific fields.
.value(field)Selects a single field's value.
.where(expr)Filters with an expression.
.start(n)Skips the first n records.
.limit(n)Limits the number of records.
.fetch(vararg fields)Fetches related records.
.timeout(seconds)Sets a query timeout.
.version(literal)Reads at a specific version.

Example

val adults: List<Person> = client
.select(Table("person"))
.where(field("age") gte 18)
.limit(50)
.awaitAs()

CreateQuery, UpdateQuery, UpsertQuery, and RelateQuery accept content and a return mode.

MethodDescription
.content(data)Sets the record content (a JsonElement).
.where(expr)Filters affected records (update, upsert, merge, patch, delete).
.returnMode(mode)Controls the returned payload (see ReturnMode).

Import

import com.surrealdb.kotlin.query.ReturnMode
ValueDescription
ReturnMode.NoneReturns nothing.
ReturnMode.BeforeReturns records as they were before the change.
ReturnMode.AfterReturns records after the change.
ReturnMode.DiffReturns a JSON Patch diff.
ReturnMode.Fields(names)Returns only the named fields.

Returned by .run(function).

MethodDescription
.args(vararg args)Sets the function arguments.
.version(version)Selects a function version.

Build WHERE expressions with the helper functions and infix operators from com.surrealdb.kotlin.query.

Import

import com.surrealdb.kotlin.query.field
import com.surrealdb.kotlin.query.value
import com.surrealdb.kotlin.query.gte
import com.surrealdb.kotlin.query.and
HelperSurrealQL
field(name)A validated field reference.
value(any)A literal value.
raw(boundQuery)A raw fragment.
a eq b=
a neq b!=
a lt b<
a lte b<=
a gt b>
a gte b>=
a contains bCONTAINS
a inside bIN
a and bAND
a or bOR
not(expr)!

Example

import com.surrealdb.kotlin.query.field
import com.surrealdb.kotlin.query.gte
import com.surrealdb.kotlin.query.eq
import com.surrealdb.kotlin.query.and

val expr = (field("age") gte 18) and (field("active") eq true)
val results = client.select(Table("person")).where(expr).awaitAs<List<Person>>()

The surql helpers build a parameterised BoundQuery with automatic, injection-safe binding of values and record identifiers.

Import

import com.surrealdb.kotlin.query.surql

Example

// Vararg bindings
val a = surql("SELECT * FROM person WHERE age > \$min", "min" to 25)

// Builder block
val b = surql {
+"SELECT * FROM person WHERE age > "
param("min", 25)
}

val result = client.query(a)

Was this page helpful?