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

Add context.Context support to the API #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type User struct {

func main() {
// Connect to SurrealDB
db, err := surrealdb.New("ws://localhost:8000/rpc")
db, err := surrealdb.New(context.Background(), "ws://localhost:8000/rpc")
if err != nil {
panic(err)
}
Expand All @@ -47,11 +47,11 @@ func main() {
Username: "root",
Password: "root",
}
if _, err = db.Signin(authData); err != nil {
if _, err = db.Signin(context.Background(), authData); err != nil {
panic(err)
}

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

Expand All @@ -62,7 +62,7 @@ func main() {
}

// Insert user
data, err := db.Create("user", user)
data, err := db.Create(context.Background(), "user", user)
if err != nil {
panic(err)
}
Expand All @@ -75,7 +75,7 @@ func main() {
}

// Get user by ID
data, err = db.Select(createdUser[0].ID)
data, err = db.Select(context.Background(), createdUser[0].ID)
if err != nil {
panic(err)
}
Expand All @@ -91,18 +91,18 @@ func main() {
changes := map[string]string{"name": "Jane"}

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

if _, err = db.Query("SELECT * FROM $record", map[string]interface{}{
if _, err = db.Query(context.Background(), "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 {
if _, err = db.Delete(context.Background(), selectedUser.ID); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ SurrealDB Go library supports smart unmarshal. It means that you can unmarshal a
```go

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

```

Expand Down
81 changes: 41 additions & 40 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package surrealdb

import (
"context"
"fmt"

"github.com/surrealdb/surrealdb.go/pkg/model"
Expand All @@ -24,8 +25,8 @@ type Auth struct {
}

// New creates a new SurrealDB client.
func New(url string, connection conn.Connection) (*DB, error) {
connection, err := connection.Connect(url)
func New(ctx context.Context, url string, connection conn.Connection) (*DB, error) {
connection, err := connection.Connect(ctx, url)
if err != nil {
return nil, err
}
Expand All @@ -44,100 +45,100 @@ func (db *DB) Close() {
// --------------------------------------------------

// Use is a method to select the namespace and table to use.
func (db *DB) Use(ns, database string) (interface{}, error) {
return db.send("use", ns, database)
func (db *DB) Use(ctx context.Context, ns, database string) (interface{}, error) {
return db.send(ctx, "use", ns, database)
}

func (db *DB) Info() (interface{}, error) {
return db.send("info")
func (db *DB) Info(ctx context.Context) (interface{}, error) {
return db.send(ctx, "info")
}

// Signup is a helper method for signing up a new user.
func (db *DB) Signup(authData *Auth) (interface{}, error) {
return db.send("signup", authData)
func (db *DB) Signup(ctx context.Context, authData *Auth) (interface{}, error) {
return db.send(ctx, "signup", authData)
}

// Signin is a helper method for signing in a user.
func (db *DB) Signin(authData *Auth) (interface{}, error) {
return db.send("signin", authData)
func (db *DB) Signin(ctx context.Context, authData *Auth) (interface{}, error) {
return db.send(ctx, "signin", authData)
}

func (db *DB) Invalidate() (interface{}, error) {
return db.send("invalidate")
func (db *DB) Invalidate(ctx context.Context) (interface{}, error) {
return db.send(ctx, "invalidate")
}

func (db *DB) Authenticate(token string) (interface{}, error) {
return db.send("authenticate", token)
func (db *DB) Authenticate(ctx context.Context, token string) (interface{}, error) {
return db.send(ctx, "authenticate", token)
}

// --------------------------------------------------

func (db *DB) Live(table string, diff bool) (string, error) {
id, err := db.send("live", table, diff)
func (db *DB) Live(ctx context.Context, table string, diff bool) (string, error) {
id, err := db.send(ctx, "live", table, diff)
return id.(string), err
}

func (db *DB) Kill(liveQueryID string) (interface{}, error) {
return db.send("kill", liveQueryID)
func (db *DB) Kill(ctx context.Context, liveQueryID string) (interface{}, error) {
return db.send(ctx, "kill", liveQueryID)
}

func (db *DB) Let(key string, val interface{}) (interface{}, error) {
return db.send("let", key, val)
func (db *DB) Let(ctx context.Context, key string, val interface{}) (interface{}, error) {
return db.send(ctx, "let", key, val)
}

// Query is a convenient method for sending a query to the database.
func (db *DB) Query(sql string, vars interface{}) (interface{}, error) {
return db.send("query", sql, vars)
func (db *DB) Query(ctx context.Context, sql string, vars interface{}) (interface{}, error) {
return db.send(ctx, "query", sql, vars)
}

// Select a table or record from the database.
func (db *DB) Select(what string) (interface{}, error) {
return db.send("select", what)
func (db *DB) Select(ctx context.Context, what string) (interface{}, error) {
return db.send(ctx, "select", what)
}

// Creates a table or record in the database like a POST request.
func (db *DB) Create(thing string, data interface{}) (interface{}, error) {
return db.send("create", thing, data)
func (db *DB) Create(ctx context.Context, thing string, data interface{}) (interface{}, error) {
return db.send(ctx, "create", thing, data)
}

// Update a table or record in the database like a PUT request.
func (db *DB) Update(what string, data interface{}) (interface{}, error) {
return db.send("update", what, data)
func (db *DB) Update(ctx context.Context, what string, data interface{}) (interface{}, error) {
return db.send(ctx, "update", what, data)
}

// Merge a table or record in the database like a PATCH request.
func (db *DB) Merge(what string, data interface{}) (interface{}, error) {
return db.send("merge", what, data)
func (db *DB) Merge(ctx context.Context, what string, data interface{}) (interface{}, error) {
return db.send(ctx, "merge", what, data)
}

// Patch applies a series of JSONPatches to a table or record.
func (db *DB) Patch(what string, data []Patch) (interface{}, error) {
return db.send("patch", what, data)
func (db *DB) Patch(ctx context.Context, what string, data []Patch) (interface{}, error) {
return db.send(ctx, "patch", what, data)
}

// Delete a table or a row from the database like a DELETE request.
func (db *DB) Delete(what string) (interface{}, error) {
return db.send("delete", what)
func (db *DB) Delete(ctx context.Context, what string) (interface{}, error) {
return db.send(ctx, "delete", what)
}

// Insert a table or a row from the database like a POST request.
func (db *DB) Insert(what string, data interface{}) (interface{}, error) {
return db.send("insert", what, data)
func (db *DB) Insert(ctx context.Context, what string, data interface{}) (interface{}, error) {
return db.send(ctx, "insert", what, data)
}

// LiveNotifications returns a channel for live query.
func (db *DB) LiveNotifications(liveQueryID string) (chan model.Notification, error) {
return db.conn.LiveNotifications(liveQueryID)
func (db *DB) LiveNotifications(ctx context.Context, liveQueryID string) (chan model.Notification, error) {
return db.conn.LiveNotifications(ctx, liveQueryID)
}

// --------------------------------------------------
// Private methods
// --------------------------------------------------

// send is a helper method for sending a query to the database.
func (db *DB) send(method string, params ...interface{}) (interface{}, error) {
func (db *DB) send(ctx context.Context, method string, params ...interface{}) (interface{}, error) {
// here we send the args through our websocket connection
resp, err := db.conn.Send(method, params)
resp, err := db.conn.Send(ctx, method, params)
if err != nil {
return nil, fmt.Errorf("sending request failed for method '%s': %w", method, err)
}
Expand Down
Loading