DB
The DB struct is the main client for interacting with SurrealDB. It holds the underlying connection and provides methods for authentication, namespace selection, and live query management. Data operations are performed through generic top-level functions that accept *DB as a parameter.
Source: db.go
Constructors
FromEndpointURLString
Creates a new *DB and connects to a SurrealDB instance. The URL scheme determines the connection type (WebSocket or HTTP).
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for canceling the connection attempt. |
connectionURL | string | The endpoint URL. Supported schemes: ws://, wss://, http://, https://. |
Returns: (*DB, error)
Examples
FromConnection
Creates a new *DB from a custom connection.Connection implementation. Calls .Connect(ctx) automatically.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for canceling the connection attempt. |
conn | connection.Connection | A connection implementation (e.g., gorillaws.New(conf) or http.New(conf)). |
Returns: (*DB, error)
Connection Methods
.Close()
Closes the underlying connection and releases resources.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the close operation. |
Returns: error
.Use()
Selects the namespace and database to use for subsequent operations.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
ns | string | The namespace to use. |
database | string | The database to use. |
Returns: error
.Version()
Returns version information about the connected SurrealDB instance.
Returns: (*VersionData, error) — see VersionData
Examples
Authentication Methods
.SignIn()
Signs in an existing user. The fields provided in authData determine the authentication level (root, namespace, database, or record).
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
authData | any | An Auth struct or map[string]any with credentials. |
Returns: (string, error) — the JWT token string
Examples
.SignInWithRefresh()
Signs in using a TYPE RECORD access method with WITH REFRESH enabled. Returns both an access token and a refresh token. SurrealDB v3+ only.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
authData | any | Credentials as map[string]any. Use "refresh" key for token refresh. |
Returns: (*Tokens, error) — see Tokens
Examples
.SignUp()
Signs up a new record user.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
authData | any | An Auth struct or map[string]any with signup credentials. |
Returns: (string, error) — the JWT token string
.SignUpWithRefresh()
Signs up using a TYPE RECORD access method with WITH REFRESH enabled. SurrealDB v3+ only.
Returns: (*Tokens, error) — see Tokens
.Authenticate()
Authenticates the connection with a JWT token.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
token | string | The JWT token to authenticate with. |
Returns: error
.Invalidate()
Invalidates the current authentication, returning the connection to an unauthenticated state.
Returns: error
.Info()
Returns the record of the currently authenticated user.
Returns: (map[string]any, error)
Variables
.Let()
Defines a variable on the connection that can be used in subsequent queries.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
key | string | The variable name (without $ prefix). |
val | any | The value to assign. |
Returns: error
.Unset()
Removes a previously defined variable from the connection.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
key | string | The variable name to remove. |
Returns: error
Sessions and Transactions
.Attach()
Creates a new session on the WebSocket connection. Sessions are only supported on WebSocket connections (SurrealDB v3+).
Returns: (*Session, error) — see Session
.Begin()
Starts a new interactive transaction on the default session. Transactions are only supported on WebSocket connections (SurrealDB v3+).
Returns: (*Transaction, error) — see Transaction
Query Functions
These are top-level generic functions that accept *DB, *Session, or *Transaction as the s parameter.
Query
Executes a SurrealQL query string and returns typed results.
| Parameter | Type | Description |
|---|---|---|
TResult | type parameter | The expected result type for each statement. |
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender to execute the query on. |
sql | string | The SurrealQL query string. |
vars | map[string]any | Variables to bind into the query. Pass nil for no variables. |
Returns: (*[]QueryResult[TResult], error) — see QueryResult
Examples
QueryRaw
Composes and executes a batch of query statements with per-statement results.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender to execute the query on. |
queries | *[]QueryStmt | A pointer to a slice of QueryStmt objects. Results are written back to each statement. |
Returns: error
Data Functions
These are top-level generic functions that accept *DB, *Session, or *Transaction as the s parameter.
Select
Retrieves records from the database.
| Parameter | Type | Description |
|---|---|---|
TResult | type parameter | Use a slice type for tables, a single type for record IDs. |
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender. |
what | string | Table | RecordID | The table or record to select. |
Returns: (*TResult, error)
Create
Creates a new record.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender. |
what | string | Table | RecordID | The table or record ID to create. |
data | any | The record data (struct or map). |
Returns: (*TResult, error)
Insert
Inserts one or more records into a table.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender. |
table | models.Table | The table to insert into. |
data | any | A single record or slice of records to insert. |
Returns: (*[]TResult, error)
Update
Replaces the entire content of a record (like a PUT).
Returns: (*TResult, error)
Upsert
Creates a record if it does not exist, or replaces it entirely.
Returns: (*TResult, error)
Merge
Merges data into an existing record, preserving unmentioned fields.
Returns: (*TResult, error)
Patch
Applies JSON Patch operations to a record or table.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender. |
what | string | Table | RecordID | The table or record to patch. |
patches | []PatchData | JSON Patch operations. See PatchData. |
Returns: (*[]PatchData, error)
Delete
Removes records from the database.
Returns: (*TResult, error)
Relate
Creates a relationship between two records with an auto-generated ID.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | *Transaction | The sender. |
rel | *Relationship | The relationship to create. See Relationship. |
Returns: (*TResult, error)
InsertRelation
Inserts a relation record, optionally with a specified ID.
Returns: (*TResult, error)
Live Query Methods
Live
Starts a live query on a table. Only available on *DB and *Session.
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | Context for the operation. |
s | *DB | *Session | The sender (not *Transaction). |
table | models.Table | The table to watch. |
diff | bool | If true, notifications contain JSON Patch diffs. |
Returns: (*models.UUID, error)
Kill
Terminates a live query and closes its notification channel.
Returns: error
.LiveNotifications()
Returns the notification channel for a live query.
Returns: (chan connection.Notification, error)
.CloseLiveNotifications()
Closes the notification channel without killing the server-side live query.
Returns: error
Low-level RPC
Send
Sends a raw RPC request to SurrealDB. Limited to data methods: select, create, insert, insert_relation, kill, live, merge, relate, update, upsert, patch, delete, query.
Returns: error
See Also
Session for session management reference
Transaction for transaction reference
Types for type definitions used by these methods
Errors for error types
Connecting to SurrealDB for connection patterns