• Start

Languages

/

Swift

/

Concepts

Models

Define type-safe models for SurrealDB using the @SurrealModel macro or manual conformance in the Swift SDK.

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 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.

  • SurrealModel conformance, so the type can be used directly with select, create, update, upsert and delete.

  • A Fields namespace for building type-safe predicates such as Person.Fields.age >= 18.

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.

Was this page helpful?