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 ¶
const ( AuthRoot = "root" AuthNamespace = "namespace" AuthDatabase = "database" )
The levels of a user, for the key auth (D51).
const ( EncodingCBOR = "cbor" EncodingJSON = "json" )
The encodings, for the key encoding (D49).
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 ¶
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. 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
})
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).
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 ¶
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 ¶
Close closes the idle connections of the transport. database/sql calls it when the database closes.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver for SurrealDB.
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).
type Option ¶ added in v0.6.0
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
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
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
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
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
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
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).
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.