• Start

Languages

/

Swift

/

Concepts

Predicates

Build type-safe query conditions with the predicate DSL in the SurrealDB Swift SDK.

Predicates are type-safe conditions used to filter records in select, update, upsert and delete. They are built from the Fields namespace generated by the @SurrealModel macro.

Each field supports the standard comparison operators:

Person.Fields.name == "Ada"
Person.Fields.name != "Bob"
Person.Fields.age > 18
Person.Fields.age >= 21
Person.Fields.age < 65
Person.Fields.age <= 60

Combine predicates with the logical operators &&, || and !:

let combined = Person.Fields.age >= 18 && Person.Fields.published == true
let either = Person.Fields.age < 18 || Person.Fields.name == "Admin"
let negated = !(Person.Fields.published == false)

When you need an expression that the DSL does not cover, or when using a manually conformed model without a Fields namespace, you can supply a raw SurrealQL condition:

let raw = SurrealPredicate(raw: "age > 18 AND name != 'Bot'")

Pass a predicate to any method that accepts a where: argument:

let adults = try await client.select(
Person.self,
where: Person.Fields.age >= 18,
limit: 20,
start: 0
)

Was this page helpful?