Data manipulation
The Go SDK provides generic top-level functions for common CRUD operations on records and tables. These functions work with *DB, *Session, and *Transaction through the sendable constraint, and return typed results through Go generics.
This page covers how to target tables and records, and how to select, create, insert, update, merge, patch, and delete data.
API References
| Function | Description |
|---|---|
surrealdb.Select[T](ctx, s, what) | Selects all records from a table, or a specific record |
surrealdb.Create[T](ctx, s, what, data) | Creates a new record with optional data |
surrealdb.Insert[T](ctx, s, table, data) | Inserts one or multiple records into a table |
surrealdb.InsertRelation[T](ctx, s, rel) | Inserts a relation record between two records |
surrealdb.Relate[T](ctx, s, rel) | Creates a relation with an auto-generated ID |
surrealdb.Update[T](ctx, s, what, data) | Replaces the entire content of a record or all records in a table |
surrealdb.Upsert[T](ctx, s, what, data) | Creates a record if it does not exist, or replaces it entirely |
surrealdb.Merge[T](ctx, s, what, data) | Merges data into a record, preserving unmentioned fields |
surrealdb.Patch(ctx, s, what, patches) | Applies JSON Patch operations to a record or table |
surrealdb.Delete[T](ctx, s, what) | Deletes a specific record or all records from a table |
Targeting tables and records
Most data manipulation functions accept a what parameter that determines the scope of the operation. You can pass a Table to target all records in a table, or a RecordID to target a specific record.
When a Table is passed, operations that return data return a slice. When a RecordID is passed, they return a single value. Use the appropriate type parameter to match.
Selecting records
Select retrieves records from the database. Pass a Table to get all records, or a RecordID to get a single record.
Creating records
Create creates a new record. Pass a Table to generate a random ID, or a RecordID to specify the ID explicitly. The data can be a struct or a map.
Inserting records
Insert inserts one or more records into a table. This is useful for bulk operations.
Creating relations
The SDK provides two ways to create graph edges between records.
Relate creates a relation with an auto-generated ID. The Relationship.ID field is ignored.
InsertRelation works like Insert but for relation tables. It allows you to specify the ID explicitly via the Relationship.ID field.
Replacing records
Update replaces the entire content of a record or all records in a table. Fields not included in the new data are removed.
Note
Upserting records
Upsert creates a record if it does not already exist, or replaces it entirely if it does.
Merging data
Merge deep-merges the provided data into the existing record, preserving fields not mentioned in the merge payload.
Applying patches
Patch applies JSON Patch (RFC 6902) operations to a record or all records in a table. Each operation is a PatchData with Op, Path, and Value fields.
Supported operations include add, remove, replace, move, copy, and test.
Deleting records
Delete removes a specific record or all records from a table. The function returns the deleted record(s).
Learn more
DB API reference for complete function signatures and parameters
Executing queries for running SurrealQL statements directly
Value types for the types used by data manipulation functions
RecordID reference for constructing record identifiers
SurrealQL CRUD statements for the underlying query language