surreal

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Unofficial Go driver for SurrealDB

Install

go get github.com/terawatthour/surreal-go

Why? There's already an official Go driver for SurrealDB.

I found the code dodgy and hard to work with. I wanted to write my own driver that was easier to work with, especially for my own use. Most of the code is based on the official driver, but I've made some changes to make it more appealing to me.

Usage

package main

import (
    "fmt"
    "math/rand"
    "github.com/terawatthour/surreal-go"
)
    
func main() {
    // establish a connection to the SurrealDB server
    db, _ := surreal.Connect("ws://localhost:8000/rpc", &surreal.Options{
        Verbose: true,
        WebSocketOptions: surreal.WebSocketOptions{
            // the websocket driver doesn't handle reconnections, but you can do it yourself  
            OnDropCallback: func(reason error) {
                fmt.Println("dropped connection", reason)
            },
        },
    })
    defer db.Close()
    
    // set the desired namespace and database
    _ = db.Use("ns-test", "db-test")
	
    // select desired data into either a map, struct, or a slice 
    var user map[string]any
    _ = db.Select("users:eqxomgmyq9z4lnl1gp65", &user)
}

Documentation

Index

Constants

View Source
const (
	Alphanumeric   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	DefaultTimeout = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthArgs

type AuthArgs struct {
	Namespace string `json:"NS"`
	Database  string `json:"DB"`
	Scope     string `json:"SC,omitempty"`
	Access    string `json:"AC,omitempty"`
	Other     Map    `json:"-"`
}

func (AuthArgs) MarshalJSON

func (s AuthArgs) MarshalJSON() ([]byte, error)

type Connection

type Connection interface {
	Run()
	Send(method string, params []any) ([]byte, error)
	RegisterLiveCallback(id string, callback func(notification rpc.LiveNotification))
	Close() error
}

type DB

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

func Connect

func Connect(connectionUrl string, options *Options) (*DB, error)

func (*DB) Authenticate

func (db *DB) Authenticate(token string) error

func (*DB) Close

func (db *DB) Close() error

Close closes the connection to the database.

func (*DB) Create

func (db *DB) Create(table string, data any, destination ...any) error

Create creates a record in a table, then decodes the row into the destination, if provided. Destination may be either a pointer to a slice or a pointer to a single record (struct, map).

func (*DB) Delete

func (db *DB) Delete(id string, destination ...any) error

Delete deletes a record, or all records, from a table, then decodes the rows into the destination, if provided.

func (*DB) Info

func (db *DB) Info(destination any) error

Info retrieves information about the current scope(!) user.

func (*DB) Insert

func (db *DB) Insert(table string, data any, destination ...any) error

Insert inserts a record, or multiple records, into a table, then decodes the rows into the destination, if provided. Destination may be either a pointer to a slice or a pointer to a single record (struct, map).

func (*DB) Invalidate

func (db *DB) Invalidate() error

func (*DB) Kill added in v0.0.3

func (db *DB) Kill(id string) error

func (*DB) Let

func (db *DB) Let(identifier string, value any) error

Let binds an identifier to a value. The value may be used in subsequent queries.

func (*DB) Live added in v0.0.3

func (db *DB) Live(id string, callback func(notification rpc.LiveNotification), diff bool) (string, error)

func (*DB) Merge

func (db *DB) Merge(id string, data any, destination ...any) error

func (*DB) Patch

func (db *DB) Patch(id string, diff []Diff, destination ...any) error

func (*DB) Ping

func (db *DB) Ping() error

func (*DB) Query

func (db *DB) Query(query string, vars Map, scanDestinations ...any) error

Query sends a query (or multiple semicolon separated queries) to the database and returns the result (results). The result is decoded into the scanDestinations. If there are multiple queries, the results are decoded into the corresponding scanDestinations. `vars` is a map of variables that are used to bind the query (or queries).

func (*DB) Relate

func (db *DB) Relate(from any, thing string, to any, data any, destination ...any) error

func (*DB) Select

func (db *DB) Select(id string, destination any) error

Select performs a select query and decodes the results into the destination. May target a single record or all records in a table. Returns error if id is not a table name and there is no row found.

func (*DB) SignIn

func (db *DB) SignIn(args AuthArgs) error

func (*DB) SignUp

func (db *DB) SignUp(args AuthArgs) error

func (*DB) Unset

func (db *DB) Unset(identifier string) error

Unset removes an identifier from the current session.

func (*DB) Update

func (db *DB) Update(id string, data any, destination ...any) error

func (*DB) Upsert

func (db *DB) Upsert(id string, data any, destination ...any) error

func (*DB) Use

func (db *DB) Use(namespace, databaseName string) error

Use sets the namespace and database name for the current connection. Should be called after the connection is established, but before any queries are sent.

func (*DB) Version

func (db *DB) Version() (string, error)

Version retrieves the version of the database.

type Diff

type Diff struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	Value any    `json:"value"`
}

type LiveDiff added in v0.0.3

type LiveDiff struct {
	Op    string          `json:"op"`
	Path  string          `json:"path"`
	Value json.RawMessage `json:"value"`
}

type Map

type Map map[string]any

type Options

type Options struct {
	Verbose          bool
	WebSocketOptions WebSocketOptions
}

type QueryError

type QueryError struct {
	QueryNo int
	Message string
}

type QueryErrors

type QueryErrors []QueryError

func (QueryErrors) Error

func (q QueryErrors) Error() string

type WebSocketConnection

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

func (*WebSocketConnection) Close

func (ws *WebSocketConnection) Close() error

func (*WebSocketConnection) RegisterLiveCallback added in v0.0.3

func (ws *WebSocketConnection) RegisterLiveCallback(event string, callback func(notification rpc.LiveNotification))

func (*WebSocketConnection) Run

func (ws *WebSocketConnection) Run()

func (*WebSocketConnection) Send

func (ws *WebSocketConnection) Send(method string, params []any) ([]byte, error)

Send writes a message to the websocket connection and waits for a response. Expects a JSON serializable object.

type WebSocketOptions

type WebSocketOptions struct {
	// DisableCompression enables compression for the websocket connection.
	DisableCompression bool

	// OnDropCallback is called when the websocket connection is dropped.
	OnDropCallback func(reason error)

	// ResponseTimeout is the duration to wait for a response before timing out. Defaults to 10 seconds.
	ResponseTimeout time.Duration
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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