Skip to content

Concepts

Executing queries

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.

MethodDescription
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

Runs one or more SurrealQL statements against the database and returns an RpcResponse.

Method Syntax
client.query(query, bindings_json, session, txn)
ArgumentDescription
queryThe SurrealQL statements to run.
bindings_jsonOptional bindings as a JSON string. Defaults to "{}".
sessionAn optional session id.
txnAn optional transaction id.
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())

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

True

when there is no error.

is_error()

Returns

True

when the response carries an error.

has_result()

Returns

True

when 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]

.

query() accepts a bindings_json argument.

var resp = client.query("SELECT * FROM person;", "{}")
Note

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.

Runs a SurrealQL function, built-in or custom, and returns its result.

Method Syntax
client.call(fn_name, args, session, txn)
ArgumentDescription
fn_nameThe function name, for example fn::greet or time::now.
argsThe function arguments, each CBOR-encoded, as a List[List[UInt8]].
sessionAn optional session id.
txnAn optional transaction id.
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)

Assigns a value to a parameter for the connection, so you can reference it as $name in subsequent queries.

Method Syntax
client.set(name, value_cbor, session)
ArgumentDescription
nameThe parameter name, without the leading $.
value_cborThe value, CBOR-encoded as a List[UInt8].
sessionAn optional session id.

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)

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"))

Removes a parameter previously assigned with set().

Method Syntax
client.unset(name, session)
ArgumentDescription
nameThe parameter name to remove.
sessionAn optional session id.
client.unset("name")

Calls a custom API handler defined on the database with DEFINE API.

Method Syntax
client.api(path, method, session, txn)
ArgumentDescription
pathThe API path to call.
methodThe HTTP method. Defaults to "GET".
sessionAn optional session id.
txnAn optional transaction id.
var resp = client.api("/users", "GET")
Note

The API endpoint is checked against the active transport's capabilities. If the transport does not support it, the SDK raises an UnsupportedFeatureError.

Each query is wrapped in its own implicit transaction by the server. To run several statements atomically, see Transactions.

Was this page helpful?

Last updated