The Swift SDK has a method for each CRUD operation. Each one takes a model type, and the filtering methods accept a predicate in the where: argument. Where a method has a record-targeted overload, it takes a SurrealRecordID to act on a single record.
Method | Description |
|---|---|
client.select(model) | Selects all records in a table, or a specific record |
client.create(model) | Creates a record in the database |
client.update(model) | Updates matching records, or a specific record |
client.upsert(model) | Creates or updates matching records |
client.delete(model) | Deletes matching records, or a specific record |
.select()
Selects all records of a model's table, a filtered subset, or a single record by id.
try await client.select(Model.self, where: predicate, limit: limit, start: start)Arguments
| Argument | Description |
|---|---|
model (required) | The model type to select, for example Person.self. |
where (optional) | A predicate restricting which records are returned. |
limit / start (optional) | Paginate the results by limiting the count and offsetting the start. |
recordID (optional) | Select a single record by its SurrealRecordID instead of a table. |
Example usage
// Select all records
let people = try await client.select(Person.self)
// Select a filtered, paginated subset
let adults = try await client.select(
Person.self,
where: Person.Fields.age >= 18,
limit: 20,
start: 0
)
// Select a single record by id
let id = SurrealRecordID(table: "person", id: .string("ada"))
let person: Person? = try await client.select(recordID: id, as: Person.self) .create()
Creates a record in the database, either with an auto-generated id or a specific id.
try await client.create(content)Arguments
| Argument | Description |
|---|---|
content (required) | The model instance to create. The table is derived from the model. |
recordID (optional) | A SurrealRecordID to create the record with a specific id. |
Example usage
// Create with an auto-generated id
let created: [Person] = try await client.create(Person(id: nil, name: "Ada", age: 30))
// Create with a specific id
let id = SurrealRecordID(table: "person", id: .string("ada"))
let record: Person? = try await client.create(
recordID: id,
content: Person(id: nil, name: "Ada", age: 30)
) .update()
Updates matching records of a model's table, or a single record by id.
try await client.update(Model.self, content: content, where: predicate)Arguments
| Argument | Description |
|---|---|
model (required) | The model type to update, for example Person.self. |
content (required) | The content to write to matching records. |
where (optional) | A predicate selecting which records to update. |
recordID (optional) | A SurrealRecordID to update a single record. |
Example usage
// Update matching records
let updated = try await client.update(
Person.self,
content: Person(id: nil, name: "Ada", age: 31),
where: Person.Fields.name == "Ada"
)
// Update a specific record
let id = SurrealRecordID(table: "person", id: .string("ada"))
let record: Person? = try await client.update(
recordID: id,
content: Person(id: nil, name: "Ada", age: 31)
) .upsert()
Creates matching records if they do not exist, or updates them if they do.
try await client.upsert(Model.self, content: content, where: predicate)Arguments
| Argument | Description |
|---|---|
model (required) | The model type to upsert, for example Person.self. |
content (required) | The content to write to matching records. |
where (optional) | A predicate selecting which records to upsert. |
Example usage
let upserted = try await client.upsert(
Person.self,
content: Person(id: nil, name: "Ada", age: 31),
where: Person.Fields.name == "Ada"
) .delete()
Deletes matching records of a model's table, or a single record by id.
try await client.delete(Model.self, where: predicate)Arguments
| Argument | Description |
|---|---|
model (required) | The model type to delete from, for example Person.self. |
where (optional) | A predicate selecting which records to delete. |
recordID (optional) | A SurrealRecordID to delete a single record. |
Example usage
// Delete matching records
let deleted = try await client.delete(
Person.self,
where: Person.Fields.age < 18
)
// Delete a specific record
let id = SurrealRecordID(table: "person", id: .string("ada"))
let record: Person? = try await client.delete(recordID: id, as: Person.self)