• Stars
    star
    227
  • Rank 175,873 (Top 4 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 3 months ago

Reviews

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

Repository Details

SurrealDB SDK 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
27,324
star
2

surrealist

Surrealist is the ultimate way to visually manage your SurrealDB database
TypeScript
1,046
star
3

awesome-surreal

A curated list of SurrealDB resources, tools, utilities, and applications.
405
star
4

surrealdb.js

SurrealDB SDK for JavaScript
TypeScript
300
star
5

surrealkv

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

surrealdb.py

SurrealDB SDK for Python
Python
175
star
7

echodb

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

surrealdb.wasm

A WebAssembly engine for the SurrealDB JavaScript SDK
Rust
114
star
9

www.surrealdb.com

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

surrealdb.net

SurrealDB SDK for .NET
C#
82
star
11

indxdb

A key-value database engine abstraction layer for IndexedDB in WASM
Rust
81
star
12

vart

A timed adaptive radix trie data-structure, used in SurrealKV
Rust
75
star
13

surrealml

A machine learning library for Python and Rust, for PyTorch, Tensorflow and SKLearn models
Rust
72
star
14

docs.surrealdb.com

The documentation for SurrealDB, powered by Docusaurus
MDX
66
star
15

surrealdb.rs

SurrealDB driver for Rust
64
star
16

surrealdb.java

SurrealDB SDK for Java
Java
63
star
17

surrealdb.node

A Node.js engine for the SurrealDB JavaScript SDK
Rust
61
star
18

surrealdb.php

SurrealDB SDK for PHP
PHP
42
star
19

tutorials

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

examples

Example applications and scripts which run on SurrealDB
TypeScript
40
star
21

docker.surrealdb.com

Configuration files for running a multi-node SurrealDB cluster using Docker
Shell
35
star
22

rixxdb

A versioned, embedded, strongly-consistent, key-value database
Go
28
star
23

revision

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

ascua

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

showcase

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

storekey

Binary encoding data format which ensures lexicographic sort order
Rust
23
star
27

surrealql-grammar

SurrealQL grammar definition for TextMate, VSCode, and other IDEs
23
star
28

fibre

A simple and fast HTTP and WebSocket framework for Go
Go
19
star
29

dmp

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

cork

A binary serialisation format for Go
Go
13
star
31

surrealdb.deno

A Deno engine for the SurrealDB JavaScript SDK
TypeScript
12
star
32

discord-bot

Rust
12
star
33

surrealdb.c

SurrealDB driver for C
11
star
34

web-indexer

TypeScript
11
star
35

ember-surreal

A realtime SurrealDB data adapter for Ember.js
JavaScript
11
star
36

windows.surrealdb.com

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

app.surrealdb.com

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

surrealdb.react

SurrealDB driver for React
TypeScript
9
star
39

install.surrealdb.com

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

rfcs

Proposals and discussions on changes to SurrealDB
9
star
41

memodb

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

setup-surreal

GitHub Action for running SurrealDB in CI environments
Shell
8
star
43

bung

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

trice

A minimal replacement for std::time::Instant that works in WASM
Rust
7
star
45

bump

An efficient buffering library for Go
Go
7
star
46

surrealdb-derive

Private implementation of procedural macros for SurrealDB
Rust
7
star
47

vtree

A versioned radix tree library implementation for Go
Go
6
star
48

surrealql.wasm

WebAssembly utility functions for SurrealQL parsing and validation
Rust
6
star
49

tlist

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

blist

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

surrealdb-macros

Private implementation of function macros for SurrealDB
Rust
5
star
52

deb.surrealdb.com

The script for installing SurrealDB on Debian-based operating systems
Shell
5
star
53

homebrew-tap

Homebrew tap for SurrealDB
Ruby
5
star
54

lexicmp

Library for comparing and sorting strings lexicographically
Rust
4
star
55

license

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

surrealql

SurrealQL data types for use in SurrealDB
4
star
57

openapi

The OpenAPI specification for the SurrealDB HTTP API
4
star
58

yokudb

4
star
59

blog

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

angular-surreal

4
star
61

surreal.go

Golang driver for SurrealDB
Go
3
star
62

download.surrealdb.com

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

version.surrealdb.com

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

ptree

A radix tree library implementation for Go
Go
3
star
65

.github

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

www.surrealdb.world

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

build

2
star
68

surreal.js

Javascript driver for SurrealDB
JavaScript
2
star
69

helm-charts

Smarty
2
star
70

chocolatey

The script for packaging SurrealDB for the Chocolatey package manager
PowerShell
2
star
71

icons

Icons for use in other repositories in the SurrealDB organisation
1
star