• Start

Concepts

Query builders

Build SurrealQL statements fluently with the Mojo SDK query builders.

The Mojo SDK ships a set of fluent builders that construct SurrealQL statements for you. Each builder is Copyable and Movable, so you can chain calls or pass it around, and each has a build() method that returns the statement as a string.

var qb = client.select_builder("person")
    .fields("id, name, age")
    .where_clause("age >= 18")
    .order_by("age DESC")
    .limit(20)

var resp = client.query_select(qb)

query_select() runs a SelectBuilder. The generic query_builder() runs any builder via its build() output.

The client exposes a factory method for each builder.

Builder

Factory

Methods

SelectBuilderselect_builder(target)fields

,

where_clause

,

order_by

,

limit

,

start

,

fetch
CreateBuildercreate_builder(target)content

,

set_field
UpdateBuilderupdate_builder(target)content

,

merge

,

patch

,

replace

,

where_clause
UpsertBuilderupsert_builder(target)content

,

merge
DeleteBuilderdelete_builder(target)where_clause
InsertBuilderinsert_builder(table)values

,

relation

Build and inspect a statement without running it:

var qb = client.select_builder("person")
    .fields("name, age")
    .where_clause("age >= 18")
    .limit(10)

print(qb.build())  # SELECT name, age FROM person WHERE age >= 18 LIMIT 10;

Create a record:

var cb = client.create_builder("person").content('{ "name": "Chiru" }')
var resp = client.query(cb.build())
Note

The older QueryBuilder is kept for backwards compatibility. New code should use the builders above.

Was this page helpful?