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

surreal: added examples folder #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.DS_Store
.AppleDouble
.LSOverride
.idea/

# Thumbnail files
._*
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@
The official SurrealDB library for Golang.

[![](https://img.shields.io/badge/status-beta-ff00bb.svg?style=flat-square)](https://github.com/surrealdb/surrealdb.go) [![](https://img.shields.io/badge/docs-view-44cc11.svg?style=flat-square)](https://surrealdb.com/docs/integration/libraries/golang) [![](https://img.shields.io/badge/license-Apache_License_2.0-00bfff.svg?style=flat-square)](https://github.com/surrealdb/surrealdb.go)

## How To Use The Client For The First Time

Using https://surrealdb.com/install, install SurrealDB on your computer

Once installed, run the following command:
```surreal start --log trace --user root --pass root memory```

The above command will start up a SurrealDB instance on localhost with the ability to sign in with the following credentials:
* username: root
* password: root

Then go to the ```/examples/``` directory and run ```main.go```

That's it!

And remember, if you are using an IDE such as Intellij, you can also debug the code to gain an even better understanding of how the client is working.
16 changes: 15 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,26 @@ func (self *DB) Query(sql string, vars map[string]any) (any, error) {
return self.send("query", sql, vars)
}

func (self *DB) SchemalessSelect(what string) ([]map[string]interface{}, error) {
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
output, err := self.Select(what)
if err != nil {
return make([]map[string]interface{}, 0), err
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
}

returnArray := make([]map[string]interface{}, 0)
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
rows := (output).([]interface{})
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
for _, row := range rows {
mapValue := (row).(map[string]interface{})
returnArray = append(returnArray, mapValue)
}
return returnArray, nil
}

// Select a table or record from the database.
func (self *DB) Select(what string) (any, error) {
return self.send("select", what)
}


// Creates a table or record in the database like a POST request.
func (self *DB) Create(thing string, data map[string]any) (any, error) {
return self.send("create", thing, data)
Expand Down
108 changes: 108 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"fmt"
"github.com/surrealdb/surrealdb.go"
)

// TODO: add in ability to do INFO command on database
// TODO: should let users specify a selector other than '*' for select statements
// TODO: set up docker container so it also allows the client to sign in
func main() {

println("Step 1: Connect to SurrealDB")
ivorytoast marked this conversation as resolved.
Show resolved Hide resolved
db, newErr := surrealdb.New("ws://localhost:8000/rpc")
if newErr != nil {
panic(newErr)
}
defer db.Close()

println("Step 2: Sign In")
_, signInErr := db.Signin(map[string]interface{}{
"user": "root",
"pass": "root",
})
if signInErr != nil {
panic(signInErr)
}

println("Step 3: Set Namespace and Database")
_, useErr := db.Use("test", "test")
if useErr != nil {
panic(useErr)
}

println("Step 4: Check If There Are Rows Already In The Database. If Rows Exist, Delete Them")
rows, selectOneErr := db.SchemalessSelect("company")
if selectOneErr != nil {
panic(selectOneErr)
}

if len(rows) > 0 {
ids := make([]string, 0)
for _, row := range rows {
idString := fmt.Sprintf("%v", row["id"])
println(" found existing company: " + idString)
ids = append(ids, idString)
}

for _, id := range ids {
_, deletionErr := db.Delete(id)
if deletionErr != nil {
panic(deletionErr)
}
println(" deleted company: " + id)
}
}

println("Step 5: Check To Make Sure The Deletions Worked")
rowsAfterDeletion, selectTwoErr := db.SchemalessSelect("company")
if selectTwoErr != nil {
panic(selectTwoErr)
}

if len(rowsAfterDeletion) != 0 {
panic("there should be no rows in the database for the 'company' table!")
}

println("Step 6: Create A Row In The 'company' Table")
_, createOneErr := db.Create("company:100", map[string]interface{}{
"name": "new company 100",
"initial_shares": "100",
})
if createOneErr != nil {
panic(createOneErr)
}
println(" created company:100")

println("Step 7: Check To Make Sure The Row Was Created")
companiesInDatabaseOne, err := db.SchemalessSelect("company")
if err != nil {
panic(err)
}

for _, row := range companiesInDatabaseOne {
companyName := fmt.Sprintf("%v", row["name"])
println(" found row in company table: " + companyName)
}

println("Step 8: Prove The 'company' Table Is Schemaless. Create A Row Without 'initial_shares'")
_, createTwoErr := db.Create("company:200", map[string]interface{}{
"name": "new company 200",
})
if createTwoErr != nil {
panic(createTwoErr)
}
println(" created company:200")

println("Step 9: Check The New Row Was Created In The 'company' Table")
rowsAfterThirdCreate, createThreeErr := db.SchemalessSelect("company")
if createThreeErr != nil {
panic(createThreeErr)
}
for _, row := range rowsAfterThirdCreate {
print(" found row in company table: ")
fmt.Println(row)
}

}