GitXplorerGitXplorer
s

surrealdb.go

public
235 stars
63 forks
45 issues

Commits

List of commits on branch main.
Verified
8f4a6983912fdf114d0aaa164b5c8b0bda8e4432

fix #119, panic gorilla ws when connection lost. (#138)

EElecTwix committed 3 months ago
Verified
bc55e641625bfcb88aebfaa2ed54ef1a65c5b297

Fix linter errors (#136)

EElecTwix committed 4 months ago
Unverified
9ced370252952390f015cd3ed15c0a22e24df978

Improve README

ttobiemh committed 4 months ago
Unverified
a69992ef8215288ac77fe81b3f5f0ab52ee1622f

Improve README

ttobiemh committed 4 months ago
Unverified
f83cef6dc2989d7976f801269ea7777cb74757b9

Update README

ttobiemh committed 4 months ago
Verified
7c2584a964ab985a34bfa8179a3d815888eff890

Fix #124 (#125)

EElecTwix committed 7 months ago

README

The README file for this repository.

 

The official SurrealDB SDK for Golang.


     

     

surrealdb.go

The official SurrealDB SDK for Golang.

Documentation

View the SDK documentation here.

How to install

go get github.com/surrealdb/surrealdb.go

Getting started

In the example below you can see how to connect to a remote instance of SurrealDB, authenticating with the database, and issuing queries for creating, updating, and selecting data from records.

This example requires SurrealDB to be installed and running on port 8000.

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)
	}
}

Instructions for running the example

  • In a new folder, create a file called main.go and paste the above code
  • Run go mod init github.com/<github-username>/<project-name> to initialise a go.mod file
  • Run go mod tidy to download the surrealdb.go dependency
  • Run go run main.go to run the example.

Contributing

You can run the Makefile commands 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, and the library will automatically convert it to the correct type.

// 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, and the 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))