Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated README for surrealdb go driver #111

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,39 @@ The official SurrealDB library for Golang.
go get github.com/surrealdb/surrealdb.go
```

### Usage
### Quick Start
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved

```go
package main

import (
"fmt"
"github.com/surrealdb/surrealdb.go"
"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
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
db, err := surrealdb.New("ws://localhost:8000/rpc")
if err != nil {
panic(err)
}

// Sign in
if _, err = db.Signin(map[string]string{
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
if _, err = db.Signin(map[string]interface{}{
"user": "root",
"pass": "root",
}); err != nil {
panic(err)
}

// Select namespace and database
if _, err = db.Use("test", "test"); err != nil {
panic(err)
}

// Create user struct
// Create user
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
user := User{
Name: "John",
Surname: "Doe",
Expand Down Expand Up @@ -81,16 +83,12 @@ func main() {

// Change part/parts of user
changes := map[string]string{"name": "Jane"}
if _, err = db.Change(selectedUser.ID, changes); err != nil {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
panic(err)
}

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

// Raw Query user
if _, err = db.Query("SELECT * FROM $record", map[string]interface{}{
"record": createdUser[0].ID,
}); err != nil {
Expand All @@ -101,10 +99,17 @@ func main() {
if _, err = db.Delete(selectedUser.ID); err != nil {
panic(err)
}
}
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved

}
```

* 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.

Note: Would be helpful to add `fmt.Println` statements to debug.

sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
# Documentation

Full documentation is available at [surrealdb doc](https://surrealdb.com/docs/integration/libraries/golang)
Expand Down