The Go SDK defines types for the arguments and results of its functions. Most are in the surrealdb package, and a few are in pkg/connection. The value types, such as RecordID, are in the models package.
Package: github.com/surrealdb/surrealdb.go/v2
Source: types.go
Authentication
Auth
Holds the credentials of a sign-in. The fields you set decide the level of the user.
type Auth struct {
Namespace string `json:"NS,omitempty"`
Database string `json:"DB,omitempty"`
Scope string `json:"SC,omitempty"`
Access string `json:"AC,omitempty"`
Username string `json:"user,omitempty"`
Password string `json:"pass,omitempty"`
}| Field | Key | When to use |
|---|---|---|
Namespace | NS | A namespace, database or record user |
Database | DB | A database or record user |
Access | AC | A record user, through an access method |
Username | user | Every level |
Password | pass | Every level |
Scope | SC | Scopes from SurrealDB 1.x, which current servers do not have |
A map[string]any with the same keys works too. The map can also hold the other fields that an access method reads, such as email, key for a bearer key, or refresh for a refresh token.
Tokens
The access token and the refresh token that .SignInWithRefresh(), .SignUpWithRefresh() and .Refresh() return, for a TYPE RECORD access method defined WITH REFRESH.
type Tokens struct {
Access string `cbor:"access"`
Refresh string `cbor:"refresh"`
}| Field | Description |
|---|---|
Access | The JWT that authenticates requests. Use it with .Authenticate() on a new connection. |
Refresh | The refresh token, of the form surreal-refresh-.... Use it with .Refresh() to get a new pair. It works only once. |
Available as: surrealdb.Tokens, an alias of connection.Tokens
Query results
QueryResult
The result of one statement of a Query or GQL call. T is the type parameter of the call.
type QueryResult[T any] struct {
Status string `json:"status"`
Time string `json:"time"`
Result T `json:"result"`
Error *QueryError `json:"-"`
}| Field | Description |
|---|---|
Status | "OK" when the statement succeeded, "ERR" when it failed |
Time | How long the statement took |
Result | The decoded result |
Error | The QueryError of a failed statement, or nil |
QueryStmt
One statement of a QueryRaw call. After the call, Result holds the statement's result undecoded, and .GetResult() decodes it.
type QueryStmt struct {
SQL string
Vars map[string]any
Result QueryResult[cbor.RawMessage]
}Methods:
.GetResult(dest any) errordecodes the result intodest. It does not report a failed statement: checkResult.Errorfor that.
StreamFrame
One step of the result of a QueryStream call, received while the query runs. T is the type of one element of Rows.
type StreamFrame[T any] struct {
Statement int
Rows []T
Done bool
Err error
Single bool
Value T
Time string
}| Field | Description |
|---|---|
Statement | The index of the statement, from 0 |
Rows | Results of the statement, such as records. More frames can follow. |
Done | true on the last frame of the statement |
Err | The statement's error, as a *QueryError, on its Done frame. Every element of Rows that the statement delivered is then retracted. |
Single | true on the Done frame of a statement that returns one value |
Value | The value of a Single statement |
Time | How long the statement took, on its Done frame |
See Streaming results.
Data types
PatchData
One JSON Patch operation, for Patch and PatchWithResult, and the operations they return.
type PatchData struct {
Op string `json:"op"`
Path string `json:"path"`
Value any `json:"value"`
}Op is add, remove, replace, move, copy or test.
ReturnMode
Chooses what PatchWithResult returns for the records it changes.
type ReturnMode string
const (
ReturnAfter ReturnMode = "AFTER"
ReturnBefore ReturnMode = "BEFORE"
ReturnDiff ReturnMode = "DIFF"
ReturnNone ReturnMode = "NONE"
)| Value | Returns |
|---|---|
ReturnAfter | Each record as it is after the change |
ReturnBefore | Each record as it was before the change |
ReturnDiff | The JSON Patch operations that made the change |
ReturnNone | Nothing |
Relationship
An edge between two records, for Relate and InsertRelation.
type Relationship struct {
ID *models.RecordID `json:"id"`
In models.RecordID `json:"in"`
Out models.RecordID `json:"out"`
Relation models.Table `json:"relation"`
Data map[string]any `json:"data"`
}| Field | Type | Description |
|---|---|---|
ID | *models.RecordID | The ID of the edge. InsertRelation uses it if you set it, and Relate ignores it. |
In | models.RecordID | The record the edge starts from |
Out | models.RecordID | The record the edge points to |
Relation | models.Table | The relation table |
Data | map[string]any | Other fields to store on the edge |
APIRequest
A request to an endpoint defined with DEFINE API, for API.
type APIRequest struct {
Method string
Body any
Headers map[string]string
Query map[string]string
}| Field | Description |
|---|---|
Method | The HTTP method, such as "post". Empty means "get". |
Body | The request body, which can be any value |
Headers | The request headers |
Query | The query parameters |
APIResponse
The response of an endpoint defined with DEFINE API. T is the type of the body.
type APIResponse[T any] struct {
Status int `json:"status"`
Headers map[string]string `json:"headers,omitempty"`
Body T `json:"body"`
}A response with an unsuccessful status is not an error. When its body does not fit T, Body is the zero value.
ExportOptions
Chooses what db.Export() writes. The zero value exports every definition and every record.
type ExportOptions struct {
ExcludeUsers bool
ExcludeAccesses bool
ExcludeParams bool
ExcludeFunctions bool
ExcludeAnalyzers bool
ExcludeRecords bool
Tables []string
}ExcludeRecords exports the definitions of tables without their records. When Tables is not empty, it limits the export to those tables. See Import and export.
VersionData
The version information that .Version() returns.
type VersionData struct {
Version string `json:"version"`
Build string `json:"build"`
Timestamp string `json:"timestamp"`
} Notification
One live query notification, received on the channel from .LiveNotifications().
Package: github.com/surrealdb/surrealdb.go/v2/pkg/connection
type Notification struct {
ID *models.UUID `json:"id,omitempty"`
Action Action `json:"action"`
Result interface{} `json:"result"`
}| Field | Type | Description |
|---|---|---|
ID | *models.UUID | The UUID of the live query |
Action | Action | What happened |
Result | any | The record, as a map[string]any, or the JSON Patch operations |
Action is one of these constants:
| Constant | Value | Meaning |
|---|---|---|
connection.CreateAction | "CREATE" | A record was created |
connection.UpdateAction | "UPDATE" | A record was updated |
connection.DeleteAction | "DELETE" | A record was deleted |
connection.KilledAction | "KILLED" | The live query was stopped |
Client configuration
Option
Configures a client created with FromEndpointURLStringWithOptions or FromConnectionWithOptions. Options lists the functions that return an Option.
type Option func(*options) ConnectionStatus
The state of a client's connection, from .Status() and in a StatusEvent. Its String() method returns the name in lower case, such as connected.
| Constant | Meaning |
|---|---|
StatusConnected | The client sends requests to the server |
StatusDisconnected | The client lost the connection, or an attempt to reconnect failed. Requests wait. |
StatusReconnecting | The client is trying to reconnect |
StatusClosed | The client was closed, or gave up reconnecting. Requests fail. |
StatusEvent
A change of the state of a client's connection. The client passes it to the function that you give to .OnStatus().
type StatusEvent struct {
Status ConnectionStatus
Attempt int
Err error
}| Field | Description |
|---|---|
Status | The new state |
Attempt | The number of the attempt to reconnect since the client lost the connection, from 1. It is 0 for the event that reports the loss. |
Err | Why the client lost the connection, or why an attempt failed. On StatusConnected after a reconnect, what the client could not restore, or nil. On StatusClosed, why the client gave up. |
CBORVersion
The CBOR version a connection uses, from db.CBORVersion().
Package: github.com/surrealdb/surrealdb.go/v2/pkg/connection
| Constant | String() | Meaning |
|---|---|---|
connection.CBORv2 | "cbor.2" | cbor.2, with SurrealDB 4.0 |
connection.CBORv1 | "cbor" | Plain CBOR, with earlier servers |
Type constraints
TableOrRecord
The types that the what argument of the CRUD functions accepts. A string is always a table name.
type TableOrRecord interface {
string | models.Table | models.RecordID | []models.Table | []models.RecordID
} sendable
The types that the s argument of the query and CRUD functions accepts.
type sendable interface {
*DB | *Session | *Transaction
} liveQueryable
The types that the s argument of Live and Kill accepts. A live query belongs to a session, so a *Transaction is not one of them.
type liveQueryable interface {
*DB | *Session
}See also
DB reference for the functions that use these types
Errors reference for the error types
Values reference for value types such as
RecordIDandTable