Skip to content

Concepts

Executing queries

The methods below run queries against the database. query() runs a query built with the query DSL, and queryRaw() runs a raw SurrealQL string with bound parameters. To read and write records with a method for each operation, see data manipulation.

Method

Description

client.query(query)

Runs a query built with the query DSL or macros

client.queryRaw(sql, bindings)

Runs a raw SurrealQL query with bound parameters

Runs a query built with the query DSL, either a query macro such as #select or a SurrealDSL builder call.

Method Syntax
try await client.query(query)
ArgumentDescription
query (required)A query produced by a query macro or a SurrealDSL builder call.
// Using a query macro
let selectQuery = #select(Person.self, where: Person.Fields.age > 18, limit: 10)
let people = try await client.query(selectQuery)

// Using the SurrealDSL builder
let query = SurrealDSL.select(Person.self, where: Person.Fields.age >= 21, limit: 50)
let result = try await client.query(query)

Executes a raw SurrealQL string with bound parameters and returns one RPCQueryResult per statement. Use it for arbitrary SurrealQL that the DSL does not model.

Method Syntax
try await client.queryRaw(sql, bindings: bindings)
ArgumentDescription
sql (required)The raw SurrealQL query to execute.
bindings (optional)A dictionary of SurrealValue parameters referenced in the query.
let results: [RPCQueryResult] = try await client.queryRaw(
    "SELECT * FROM person WHERE age > $minAge LIMIT $limit;",
    bindings: [
        "minAge": .int(18),
        "limit": .int(50)
    ]
)

for row in results {
    if row.status == .ok {
        print(row.result)
    }
}

Was this page helpful?

Last updated