sdbc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 14 Imported by: 1

README


SDBC - an independent SurrealDB client for Go


Go 1.21.9 - 1.22.2 Go Report Card Tests Codecov Discord Contributors

SDBC (SurrealDB Client) is an independent Go client for the innovative SurrealDB.

DISCLAIMER: SDBC is not the official Go client for SurrealDB but rather an independent implementation. You can find the repository for the official implementation here. Currently, SDBC is designed for direct use with SOM. It's important to note that SDBC is in the early stages of development and is not yet stable or ready for production use.

What is SurrealDB?

SurrealDB is a cutting-edge database system that offers a SQL-style query language with real-time queries
and efficient related data retrieval. It supports both schema-full and schema-less data handling. With its full graph database functionality, SurrealDB enables advanced querying and analysis by allowing records (or vertices) to be connected with edges, each with its own properties and metadata. This facilitates multi-table, multi-depth document retrieval without complex JOINs, all within the database.

(Information extracted from the official homepage).

Why is SDBC needed instead of the official client?

  • The official Go client for SurrealDB is currently not in a really usable state.
  • Inconsistencies exist in the codebase, such as the unused url parameter in the New function.
  • It lacks essential features, particularly after the 1.0.0 release of SurrealDB.
  • The SurrealDB team has other priorities, and it seems as if they are currently not actively maintaining the Go client.
  • Future versions of the official client may require CGO for direct bindings to an underlying driver, whereas SDBC remains pure Go.
  • Writing this custom client was and is an enjoyable endeavor 😉

SDBC is a practical choice until the official client becomes stable, actively maintained, and supports all the features required by SOM. It also maintains purity in Go and avoids CGO dependencies.

It is still open whether this project will be maintained after the official client becomes stable.

Table of Contents

Getting Started

Installation

To install SDBC, run the following command:

go get github.com/go-surreal/sdbc

Usage

To use SDBC, import it in your Go code:

import (
	"github.com/go-surreal/sdbc"
)

Then, create a new client:

func main() {
	client, err := sdbc.NewClient(ctx, sdbc.Config{
		Address:   "ws://localhost:8000/rpc", 
		Username:  "root", 
		Password:  "root", 
		Namespace: "test",
		Database:  "test",
	}
	
	if err != nil {
        log.Fatal(err)
    }
		
    // ...
}

Contributing

We welcome contributions! If you'd like to contribute to SDBC, please read our Contributing Guidelines for instructions on how to get started.

License

SDBC is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(ctx context.Context, conf Config, opts ...Option) (*Client, error)

NewClient creates a new client and connects to the database using a websocket connection.

func (*Client) Close

func (c *Client) Close() error

Close closes the client and the websocket connection. Furthermore, it cleans up all idle goroutines.

func (*Client) Create

func (c *Client) Create(ctx context.Context, thing string, data any) ([]byte, error)

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, thing string) ([]byte, error)

Delete a table or a row from the database like a DELETE request.

func (*Client) Kill

func (c *Client) Kill(ctx context.Context, uuid string) ([]byte, error)

func (*Client) Live

func (c *Client) Live(ctx context.Context, query string, vars map[string]any) (<-chan []byte, error)

Live executes a live query request and returns a channel to receive the results.

NOTE: SurrealDB does not yet support proper variable handling for live queries. To circumvent this limitation, params are registered in the database before issuing the actual live query. Those params are given the values of the variables passed to this method. This way, the live query can be filtered by said params. Please note that this is a workaround and may not work as expected in all cases.

References: Bug: Using variables in filters does not emit live messages (https://github.com/surrealdb/surrealdb/issues/2623) Bug: LQ params should be evaluated before registering (https://github.com/surrealdb/surrealdb/issues/2641) Bug: parameters do not work with live queries (https://github.com/surrealdb/surrealdb/issues/3602)

TODO: prevent query from being more than one statement

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, vars map[string]any) ([]byte, error)

Query is a convenient method for sending a query to the database.

func (*Client) Select

func (c *Client) Select(ctx context.Context, thing string) ([]byte, error)

Select a table or record from the database.

func (*Client) Update

func (c *Client) Update(ctx context.Context, thing string, data any) ([]byte, error)

Update a table or record in the database like a PUT request.

type Config

type Config struct {

	// Address is the address of the database.
	Address string

	// Username is the username to use for authentication.
	Username string

	// Password is the password to use for authentication.
	Password string

	// Namespace is the namespace to use.
	// It will automatically be created if it does not exist.
	Namespace string

	// Database is the database to use.
	// It will automatically be created if it does not exist.
	Database string
}

Config is the configuration for the client.

type JsonMarshal

type JsonMarshal func(val any) ([]byte, error)

type JsonUnmarshal

type JsonUnmarshal func(buf []byte, val any) error

type Option

type Option func(*options)

func WithJsonHandlers

func WithJsonHandlers(marshal JsonMarshal, unmarshal JsonUnmarshal) Option

WithJsonHandlers sets custom json marshal and unmarshal functions. If not set, the default json marshal and unmarshal functions are used.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger. If not set, no log output is created.

func WithReadLimit

func WithReadLimit(limit int64) Option

WithReadLimit sets a custom read limit (in bytes) for the websocket connection. If not set, the default read limit is 1 MB.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets a custom timeout for requests. If not set, the default timeout is 1 minute.

Jump to

Keyboard shortcuts

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