surrealdb

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package surrealdb is a database/sql driver for SurrealDB, which runs SurrealQL over HTTP. It registers the one name "surrealdb" (D47), and takes a DSN of the form surrealdb://user:pass@host:8000/namespace/database?key=value (D48).

db, err := sql.Open("surrealdb", "surrealdb://root:root@localhost:8000/test/test")

The driver sends each statement to POST /rpc in CBOR (D49 and D50). The key encoding=json makes it speak JSON, to read the traffic while debugging. The key auth names where the user is defined: root, namespace or database (D51). An argument is named, and sql.Named("x", v) binds $x.

Each statement of a query is a result set, which Rows.NextResultSet moves to (D52). The columns of a result set are the keys of its first object, in the order that the server sends them, which is the order of their names. A record id is a RecordID, and each other type has the Go type of D53. The driver has no transactions, because a transaction of SurrealDB lives in one request (D54). docs/SURREALDB.md holds what the driver knows about the server.

Index

Constants

View Source
const (
	AuthRoot      = "root"
	AuthNamespace = "namespace"
	AuthDatabase  = "database"
)

The levels of a user, for the key auth (D51).

View Source
const (
	EncodingCBOR = "cbor"
	EncodingJSON = "json"
)

The encodings, for the key encoding (D49).

View Source
const Name = "surrealdb"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func Version

func Version(ctx context.Context, dc any) (string, error)

Version returns the version of the server, such as "surrealdb-3.3.0", through the RPC method version (D57). No statement of SurrealQL returns it, so the driver answers SELECT version() with the same value (D181). dc is a connection of this driver, which sql.Conn.Raw hands over:

conn, err := db.Conn(ctx)
...
err = conn.Raw(func(dc any) error {
	v, err = surrealdb.Version(ctx, dc)
	return err
})

func WithOptions added in v0.6.0

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the server.
	Host string
	// Port is the port of the server, 8000 by default.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, sent with basic
	// authentication.
	User     string
	Password string
	// Namespace and Database name where each statement runs (D48).
	Namespace string
	Database  string
	// Auth is where the user is defined: AuthRoot, AuthNamespace or
	// AuthDatabase (D51).
	Auth string
	// Encoding is EncodingCBOR, or EncodingJSON to read the requests and the
	// responses while debugging (D49).
	Encoding string
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form surrealdb://user:pass@host:port/namespace/database?key=value (D27, D35 and D48).

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg, which ParseDSN reads back as cfg.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector opens connections to one SurrealDB server. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg. An empty Auth is AuthRoot, and an empty Encoding is EncodingCBOR.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport. database/sql calls it when the database closes.

func (*Connector) Connect

func (c *Connector) Connect(context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds no state on the server, because each request of HTTP stands alone (D54).

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for SurrealDB.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// Code is the code of the error: the code of a failed RPC call, -1 for an
	// RPC error with no code, the HTTP status of a refused request, or 0 for
	// a statement that failed.
	Code int
	// Kind is the kind of the error that 3.x names, such as "NotFound" or
	// "Thrown", or "" when the server names none.
	Kind string
	// Msg is the message of the error.
	Msg string
}

Error is one error that SurrealDB reported, such as a statement that failed, a parse error of the RPC call with code -32000, or a request that the server refused with HTTP 400 (D55).

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Option added in v0.6.0

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins. The driver has no transaction (D54), so no option applies to BeginTx.

func WithDatabase added in v0.6.0

func WithDatabase(name string) Option

WithDatabase sets the database of the statement, which the header Surreal-DB names, as the second segment of the path of the DSN does. The credentials stay those of the DSN.

func WithNamespace added in v0.6.0

func WithNamespace(name string) Option

WithNamespace sets the namespace of the statement, which the header Surreal-NS names, as the first segment of the path of the DSN does. The credentials stay those of the DSN.

func WithParameter added in v0.6.0

func WithParameter(name string, value any) Option

WithParameter sets any key of the body of the RPC request by its name, such as "id". The value is encoded as a variable is (D53). A key named here replaces one that the driver sets itself, as in Couchbase. The server ignores a key that it does not know (measured by hand on 2.7.0 and 3.3.0).

func WithReadonly added in v0.6.0

func WithReadonly(v bool) Option

WithReadonly would make the server refuse a write. The RPC protocol has no read-only mode (the RPC manual), so a statement with it fails with dbimp.ErrNotSupported (D109).

func WithTimeout added in v0.6.0

func WithTimeout(d time.Duration) Option

WithTimeout would set the time that the server gives the request. The RPC method query takes the text and the variables only, and the server has a timeout for all requests only (the RPC manual). So a statement with it fails with dbimp.ErrNotSupported (D109). The TIMEOUT clause of SurrealQL limits one statement.

type RecordID

type RecordID struct {
	// Table is the name of the table.
	Table string
	// ID is the key of the record in its table.
	ID any
}

RecordID is the id of a SurrealDB record, such as person:tobie: the name of its table and its key (D53). The key is a string, an int64, a uuid.UUID, an []any or a map[string]any.

func (RecordID) MarshalText added in v0.3.0

func (r RecordID) MarshalText() ([]byte, error)

MarshalText satisfies encoding.TextMarshaler. It writes the text of String, so that a record id inside an array or an object is person:tobie in JSON, and not the fields of the struct (D70).

func (RecordID) String

func (r RecordID) String() string

String writes the record id as SurrealQL writes it, such as person:tobie, person:123 or person:['a', 1]. A name or a key that is not a plain identifier is quoted with backticks.

type ResponseError

type ResponseError struct {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Status is the status of the statement that failed, or "" for a failed
	// RPC call and for a refused request.
	Status string
	// Errs are the errors, in the order that the server sent them.
	Errs []Error
}

ResponseError holds the errors of one response, with its HTTP status and the status of the statement that failed, such as "ERR". Use errors.As to read an Error of it.

func (*ResponseError) Error

func (err *ResponseError) Error() string

Error satisfies the error interface.

func (*ResponseError) Unwrap

func (err *ResponseError) Unwrap() []error

Unwrap returns each Error, for errors.As and errors.Is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL