Models describe the shape of your records and give the SDK the type information it needs for type-safe CRUD operations and predicates.
The @SurrealModel macro
The @SurrealModel macro is the recommended way to declare a model. It takes the table name and generates everything the SDK needs:
import SurrealDB
@SurrealModel("person")
struct Person: Codable, Sendable {
let id: String?
let name: String
let age: Int
}The macro generates:
static let surrealTable, the table name passed to the macro.SurrealModelconformance, so the type can be used directly withselect,create,update,upsertanddelete.A
Fieldsnamespace for building type-safe predicates such asPerson.Fields.age >= 18.
Manual conformance
If you prefer not to use the macro, you can conform to SurrealModel manually by declaring the surrealTable property:
struct Article: SurrealModel, Codable, Sendable {
static let surrealTable = "article"
let id: String?
let title: String
}Manual conformance does not generate a Fields namespace, so you will need to write raw predicates for queries that filter on fields.