Skip to content

API Reference

/

Types

Types

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

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"`
}
FieldKeyWhen to use
NamespaceNSA namespace, database or record user
DatabaseDBA database or record user
AccessACA record user, through an access method
UsernameuserEvery level
PasswordpassEvery level
ScopeSCScopes 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.

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"`
}
FieldDescription
AccessThe JWT that authenticates requests. Use it with .Authenticate() on a new connection.
RefreshThe 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

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:"-"`
}
FieldDescription
Status"OK" when the statement succeeded, "ERR" when it failed
TimeHow long the statement took
ResultThe decoded result
ErrorThe QueryError of a failed statement, or nil

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) error decodes the result into dest. It does not report a failed statement: check Result.Error for that.

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
}
FieldDescription
StatementThe index of the statement, from 0
RowsResults of the statement, such as records. More frames can follow.
Donetrue on the last frame of the statement
ErrThe statement's error, as a *QueryError, on its Done frame. Every element of Rows that the statement delivered is then retracted.
Singletrue on the Done frame of a statement that returns one value
ValueThe value of a Single statement
TimeHow long the statement took, on its Done frame

See Streaming results.

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.

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"
)
ValueReturns
ReturnAfterEach record as it is after the change
ReturnBeforeEach record as it was before the change
ReturnDiffThe JSON Patch operations that made the change
ReturnNoneNothing

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"`
}
FieldTypeDescription
ID*models.RecordIDThe ID of the edge. InsertRelation uses it if you set it, and Relate ignores it.
Inmodels.RecordIDThe record the edge starts from
Outmodels.RecordIDThe record the edge points to
Relationmodels.TableThe relation table
Datamap[string]anyOther fields to store on the edge

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
}
FieldDescription
MethodThe HTTP method, such as "post". Empty means "get".
BodyThe request body, which can be any value
HeadersThe request headers
QueryThe query parameters

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.

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.

The version information that .Version() returns.

type VersionData struct {
	Version   string `json:"version"`
	Build     string `json:"build"`
	Timestamp string `json:"timestamp"`
}

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"`
}
FieldTypeDescription
ID*models.UUIDThe UUID of the live query
ActionActionWhat happened
ResultanyThe record, as a map[string]any, or the JSON Patch operations

Action is one of these constants:

ConstantValueMeaning
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

Configures a client created with FromEndpointURLStringWithOptions or FromConnectionWithOptions. Options lists the functions that return an Option.

type Option func(*options)

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.

ConstantMeaning
StatusConnectedThe client sends requests to the server
StatusDisconnectedThe client lost the connection, or an attempt to reconnect failed. Requests wait.
StatusReconnectingThe client is trying to reconnect
StatusClosedThe client was closed, or gave up reconnecting. Requests fail.

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
}
FieldDescription
StatusThe new state
AttemptThe number of the attempt to reconnect since the client lost the connection, from 1. It is 0 for the event that reports the loss.
ErrWhy 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.

The CBOR version a connection uses, from db.CBORVersion().

Package: github.com/surrealdb/surrealdb.go/v2/pkg/connection

ConstantString()Meaning
connection.CBORv2"cbor.2"cbor.2, with SurrealDB 4.0
connection.CBORv1"cbor"Plain CBOR, with earlier servers

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
}

See Table and RecordID.

The types that the s argument of the query and CRUD functions accepts.

type sendable interface {
	*DB | *Session | *Transaction
}

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
}

Was this page helpful?

Last updated