A RecordID names one SurrealDB record by its table name and its ID, without the ambiguity of parsing text such as person:tobie. Use it as the target of a CRUD function, as a query variable, and as the id field of a struct.
Package: github.com/surrealdb/surrealdb.go/v2/pkg/models
Source: pkg/models/record_id.go
Definition
type RecordID struct {
Table string
ID any
}Table must not be empty. ID can be any value that SurrealDB accepts as a record ID: a string, an integer, an array, an object, or a Range for a range of records.
Constructors
NewRecordID
Returns a RecordID with the given table name and ID.
id := models.NewRecordID(tableName, id)Parameter | Type | Description |
|---|---|---|
tableName | string | The table name. |
id | any | The ID of the record. |
Returns: RecordID
person := models.NewRecordID("person", "tobie")
event := models.NewRecordID("event", 42)
reading := models.NewRecordID("temperature", []any{"London", 2026}) ParseRecordID
Parses text of the form table:id into a RecordID. The ID is always a string, so ParseRecordID("event:42") names the record event:⟨42⟩, not event:42. The result never matches a record whose ID is a number. The text must hold exactly one colon.
id, err := models.ParseRecordID(idStr)Returns: (*RecordID, error). For text without exactly one colon, it returns an error that wraps ErrBadRecordID.
For an ID that is not a string, build the RecordID with NewRecordID, or let the server parse the text with type::record() in a query.
Methods
.String()
Returns the record ID as text, such as person:tobie. It writes a part that needs escaping between ⟨ and ⟩, as SurrealDB 2.x displayed it. For example, models.NewRecordID("user", "john-doe") gives user:⟨john-doe⟩. The text is for display and logs. To send a record ID, pass the RecordID itself.
s := id.String()Returns: string
.SurrealString()
Returns the record ID as a SurrealQL record string, such as r'person:tobie'.
s := id.SurrealString()Returns: string
In structs
Use *models.RecordID with omitempty for the id field of a struct. You can then send the struct without an ID and read it back with one:
type Person struct {
ID *models.RecordID `json:"id,omitempty"`
Name string `json:"name"`
}The SDK sends a record ID as CBOR tag 8, which holds the table and the ID. A record ID decodes into a RecordID, including from any.
Record ranges
A RecordID whose ID is a Range names the records in that range, for Select and the other CRUD functions:
readings := models.RecordID{
Table: "reading",
ID: models.Range[int, models.BoundIncluded[int], models.BoundExcluded[int]]{
Begin: &models.BoundIncluded[int]{Value: 2},
End: &models.BoundExcluded[int]{Value: 4},
},
}See also
Table for the table name type
Value types for how values convert
Data manipulation for record IDs in CRUD functions
SurrealQL record IDs for the underlying data model