The primary way to run SurrealQL with the Mojo SDK is query(), which sends one or more statements and returns an RpcResponse. To create, read and change records without writing the SurrealQL yourself, see Data manipulation.
| Method | Description |
|---|---|
client.query() | Runs one or more SurrealQL statements |
client.call() | Runs a SurrealQL function |
client.set() | Assigns a value as a parameter for this connection |
client.unset() | Removes a parameter for this connection |
client.api() | Calls a custom API handler defined on the database |
.query()
Runs one or more SurrealQL statements against the database and returns an RpcResponse.
client.query(query, bindings_json, session, txn)Arguments
| Argument | Description |
|---|---|
query | The SurrealQL statements to run. |
bindings_json | Optional bindings as a JSON string. Defaults to "{}". |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.query("SELECT * FROM person WHERE age > 18;")
if resp.is_ok():
if resp.result:
print(resp.result.value())
else:
print("error:", resp.error_message().value())Reading the response
Every call returns an RpcResponse. Check is_ok() before reading the result. The decoded text representation is available on result, and the raw bytes on result_raw.
if resp.is_ok():
# CBOR-decoded text representation for convenience
if resp.result:
print(resp.result.value())
# Raw CBOR bytes if you need them
print("bytes:", len(resp.result_raw))
else:
print("code:", resp.error_code().value())
print("message:", resp.error_message().value())RpcResponse exposes the following:
Member | Description |
|---|---|
is_ok() | Returns Truewhen there is no error. |
is_error() | Returns Truewhen the response carries an error. |
has_result() | Returns Truewhen a result is present. |
result | The decoded text representation, as Optional[String]. |
result_raw | The raw CBOR or JSON bytes, as List[UInt8]. |
error_message() | The error message, as Optional[String]. |
error_code() | The error code, as Optional[Int]. |
Bindings
query() accepts a bindings_json argument.
var resp = client.query("SELECT * FROM person;", "{}") A dedicated API for passing arbitrary CBOR bindings is on the roadmap. Today, CBOR connections support the default "{}", while JSON-RPC connections accept raw JSON strings via bindings_json.
.call()
Runs a SurrealQL function, built-in or custom, and returns its result.
client.call(fn_name, args, session, txn)Arguments
| Argument | Description |
|---|---|
fn_name | The function name, for example fn::greet or time::now. |
args | The function arguments, each CBOR-encoded, as a List[List[UInt8]]. |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
from surrealdb import CborCodec
from std.collections import List
var codec = CborCodec()
var args = List[List[UInt8]]()
args.append(codec.encode_text("Chiru"))
var resp = client.call("fn::greet", args) .set()
Assigns a value to a parameter for the connection, so you can reference it as $name in subsequent queries.
client.set(name, value_cbor, session)Arguments
| Argument | Description |
|---|---|
name | The parameter name, without the leading $. |
value_cbor | The value, CBOR-encoded as a List[UInt8]. |
session | An optional session id. |
Convenience wrappers
For common scalar types, the SDK provides wrappers that encode the value for you:
set_string(name, value, session)set_int(name, value, session)set_bool(name, value, session)
Example usage
client.set_string("name", "Chiru")
client.set_int("age", 30)
var resp = client.query(
"SELECT * FROM person WHERE name = $name AND age >= $age;"
)To encode a value by hand, use CborCodec:
from surrealdb import CborCodec
var codec = CborCodec()
client.set("name", codec.encode_text("Chiru")) .unset()
Removes a parameter previously assigned with set().
client.unset(name, session)Arguments
| Argument | Description |
|---|---|
name | The parameter name to remove. |
session | An optional session id. |
Example usage
client.unset("name") .api()
Calls a custom API handler defined on the database with DEFINE API.
client.api(path, method, session, txn)Arguments
| Argument | Description |
|---|---|
path | The API path to call. |
method | The HTTP method. Defaults to "GET". |
session | An optional session id. |
txn | An optional transaction id. |
Example usage
var resp = client.api("/users", "GET") The API endpoint is checked against the active transport's capabilities. If the transport does not support it, the SDK raises an UnsupportedFeatureError.
Sessions and transactions
Each query is wrapped in its own implicit transaction by the server. To run several statements atomically, see Transactions.