• Stars
    star
    203
  • Rank 186,066 (Top 4 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SurrealDB driver for Golang

surrealdb.go

The official SurrealDB library for Golang.

Go Reference Go Report Card Mentioned in Awesome Go

Getting Started

For instructions on how to follow SurrealDB, follow Installation Guide

Installation

go get github.com/surrealdb/surrealdb.go

Usage

package main
import (
	"github.com/surrealdb/surrealdb.go"

)

type User struct {
	ID      string `json:"id,omitempty"`
	Name    string `json:"name"`
	Surname string `json:"surname"`
}

func main() {
	// Connect to SurrealDB
	db, err := surrealdb.New("ws://localhost:8000/rpc")
	if err != nil {
		panic(err)
	}

	authData := &surrealdb.Auth{
		Database:  "test",
		Namespace: "test",
		Username:  "root",
		Password:  "root",
	}
	if _, err = db.Signin(authData); err != nil {
		panic(err)
	}

	if _, err = db.Use("test", "test"); err != nil {
		panic(err)
	}

	// Define user struct
	user := User{
		Name:    "John",
		Surname: "Doe",
	}

	// Insert user
	data, err := db.Create("user", user)
	if err != nil {
		panic(err)
	}

	// Unmarshal data
	createdUser := make([]User, 1)
	err = surrealdb.Unmarshal(data, &createdUser)
	if err != nil {
		panic(err)
	}

	// Get user by ID
	data, err = db.Select(createdUser[0].ID)
	if err != nil {
		panic(err)
	}

	// Unmarshal data
	selectedUser := new(User)
	err = surrealdb.Unmarshal(data, &selectedUser)
	if err != nil {
		panic(err)
	}

	// Change part/parts of user
	changes := map[string]string{"name": "Jane"}

	// Update user
	if _, err = db.Update(selectedUser.ID, changes); err != nil {
		panic(err)
	}

	if _, err = db.Query("SELECT * FROM $record", map[string]interface{}{
		"record": createdUser[0].ID,
	}); err != nil {
		panic(err)
	}

	// Delete user by ID
	if _, err = db.Delete(selectedUser.ID); err != nil {
		panic(err)
	}
}
  • Step 1: Create a file called main.go and paste the above code
  • Step 2: Run the command go mod init github.com/<github-username>/<project-name> to create a go.mod file
  • Step 3: Run the command go mod tidy to download surreal db
  • Step 4: Run go run main.go to run the application.

Documentation

Full documentation is available at surrealdb doc

## Building

You can run the Makefile helper to run and build the project

make build
make test
make lint

You also need to be running SurrealDB alongside the tests. We recommend using the nightly build, as development may rely on the latest functionality.

Helper functions

Smart Marshal

SurrealDB Go library supports smart marshal. It means that you can use any type of data as a value in your struct. SurrealDB Go library will automatically convert it to the correct type.

// Recommended to use with SmartUnmarshal SmartMarshal
// User struct is a test struct
user, err := surrealdb.SmartUnmarshal[testUser](surrealdb.SmartMarshal(s.db.Create, user[0]))

// Can be used without SmartUnmarshal
data, err := surrealdb.SmartMarshal(s.db.Create, user[0])

Smart Unmarshal

SurrealDB Go library supports smart unmarshal. It means that you can unmarshal any type of data to the generic type provided. SurrealDB Go library will automatically convert it to that type.

// User struct is a test struct
data, err := surrealdb.SmartUnmarshal[testUser](s.db.Select(user[0].ID))

More Repositories

1

surrealdb

A scalable, distributed, collaborative, document-graph database, for the realtime web
Rust
24,630
star
2

awesome-surreal

A curated list of SurrealDB resources, tools, utilities, and applications.
321
star
3

surrealdb.js

SurrealDB driver for JavaScript
TypeScript
235
star
4

surrealdb.py

SurrealDB driver for Python
Python
154
star
5

surrealkv

A low-level, versioned, embedded, ACID-compliant, key-value database for Rust
Rust
120
star
6

echodb

An embedded, in-memory, immutable, copy-on-write, key-value database engine
Rust
117
star
7

www.surrealdb.com

The website, and blog for SurrealDB, built using Ember.js
Handlebars
93
star
8

surrealdb.wasm

SurrealDB driver for WebAssembly
Rust
83
star
9

indxdb

A key-value database engine abstraction layer for IndexedDB in WASM
Rust
69
star
10

surrealdb.net

SurrealDB driver for .NET
C#
65
star
11

surrealdb.rs

SurrealDB driver for Rust
63
star
12

surrealdb.java

SurrealDB driver for Java
Java
57
star
13

vart

A timed adaptive radix trie data-structure, used in SurrealKV
Rust
43
star
14

tutorials

A collection of official and community tutorials and code walkthroughs
JavaScript
42
star
15

surrealml

Rust
40
star
16

surrealdb.node

SurrealDB driver for Node.js
JavaScript
40
star
17

docker.surrealdb.com

Configuration files for running a multi-node SurrealDB cluster using Docker
Shell
33
star
18

rixxdb

A versioned, embedded, strongly-consistent, key-value database
Go
29
star
19

surrealdb.php

PHP
25
star
20

examples

TypeScript
25
star
21

ascua

A library for building advanced, optimised Ember.js apps
SCSS
22
star
22

showcase

A showcase of applications which have been built with, or built upon SurrealDB
22
star
23

docs.surrealdb.com

The documentation for SurrealDB, powered by Docusaurus
MDX
22
star
24

storekey

Binary encoding data format which ensures lexicographic sort order
Rust
20
star
25

surrealql-grammar

SurrealQL grammar definition for TextMate, VSCode, and other IDEs
19
star
26

revision

A Rust library for revision-tolerant serialisation and deserialisation, with support for schema evolution over time
Rust
18
star
27

fibre

A simple and fast HTTP and WebSocket framework for Go
Go
17
star
28

dmp

An efficient plain text comparison and patching library written in Rust
Rust
16
star
29

cork

A binary serialisation format for Go
Go
13
star
30

surrealdb.deno

SurrealDB driver for Deno
TypeScript
11
star
31

surrealdb.c

SurrealDB driver for C
11
star
32

discord-bot

Rust
11
star
33

windows.surrealdb.com

The script for installing SurrealDB on windows operating systems
PowerShell
11
star
34

app.surrealdb.com

The user interface application, for managing SurrealDB, built using Ember.js
JavaScript
10
star
35

web-indexer

TypeScript
10
star
36

ember-surreal

A realtime SurrealDB data adapter for Ember.js
JavaScript
10
star
37

rfcs

Proposals and discussions on changes to SurrealDB
9
star
38

bung

A serialization implementation for SurrealDB, based on MessagePack
Rust
7
star
39

install.surrealdb.com

The script for installing SurrealDB on unix operating systems
Shell
7
star
40

bump

An efficient buffering library for Go
Go
7
star
41

surrealdb-derive

Private implementation of procedural macros for SurrealDB
Rust
7
star
42

homebrew-tap

Homebrew tap for SurrealDB
Ruby
6
star
43

trice

A minimal replacement for std::time::Instant that works in WASM
Rust
6
star
44

blist

A binary time series list implementation for Go
Go
5
star
45

tlist

A linked time series list implementation for Go
Go
5
star
46

vtree

A versioned radix tree library implementation for Go
Go
5
star
47

license

Licensing information for the SurrealDB database, components, libraries, and SDKs
4
star
48

deb.surrealdb.com

The script for installing SurrealDB on Debian-based operating systems
Shell
4
star
49

yokudb

4
star
50

blog

The blog posts published on the SurrealDB website, and the DEV community
4
star
51

angular-surreal

4
star
52

surrealdb-macros

Private implementation of function macros for SurrealDB
Rust
3
star
53

surreal.go

Golang driver for SurrealDB
Go
3
star
54

surrealql

SurrealQL data types for use in SurrealDB
3
star
55

openapi

The OpenAPI specification for the SurrealDB HTTP API
3
star
56

download.surrealdb.com

The global repository for binary release files for macOS, Linux, and Windows
JavaScript
3
star
57

version.surrealdb.com

A domain endpoint for announcing the latest published SurrealDB version
JavaScript
3
star
58

ptree

A radix tree library implementation for Go
Go
3
star
59

.github

This repository houses the SurrealDB Github organisation profile file
3
star
60

surrealdb.react

SurrealDB driver for React
TypeScript
2
star
61

www.surrealdb.world

The website for SurrealDB World, built using Ember.js
SCSS
2
star
62

lexicmp

Library for comparing and sorting strings lexicographically
Rust
2
star
63

build

2
star
64

surreal.js

Javascript driver for SurrealDB
JavaScript
2
star
65

helm-charts

Smarty
1
star