forked from surrealdb/surrealdb.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
140 lines (111 loc) · 3.91 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package surrealdb
import (
"fmt"
"github.com/surrealdb/surrealdb.go/pkg/constants"
"github.com/surrealdb/surrealdb.go/pkg/websocket"
)
// DB is a client for the SurrealDB database that holds are websocket connection.
type DB struct {
ws websocket.WebSocket
}
// New creates a new SurrealDB client.
func New(url string, ws websocket.WebSocket) (*DB, error) {
return &DB{ws}, nil
}
// --------------------------------------------------
// Public methods
// --------------------------------------------------
// Close closes the underlying WebSocket connection.
func (db *DB) Close() {
_ = db.ws.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) Info() (interface{}, error) {
return db.send("info")
}
// Signup is a helper method for signing up a new user.
func (db *DB) Signup(vars interface{}) (interface{}, error) {
return db.send("signup", vars)
}
// Signin is a helper method for signing in a user.
func (db *DB) Signin(vars interface{}) (interface{}, error) {
return db.send("signin", vars)
}
func (db *DB) Invalidate() (interface{}, error) {
return db.send("invalidate")
}
func (db *DB) Authenticate(token string) (interface{}, error) {
return db.send("authenticate", token)
}
// --------------------------------------------------
func (db *DB) Live(table string) (interface{}, error) {
return db.send("live", table)
}
func (db *DB) Kill(query string) (interface{}, error) {
return db.send("kill", query)
}
func (db *DB) Let(key string, val interface{}) (interface{}, error) {
return db.send("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)
}
// Select a table or record from the database.
func (db *DB) Select(what string) (interface{}, error) {
return db.send("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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// --------------------------------------------------
// Private methods
// --------------------------------------------------
// send is a helper method for sending a query to the database.
func (db *DB) send(method string, params ...interface{}) (interface{}, error) {
// here we send the args through our websocket connection
resp, err := db.ws.Send(method, params)
if err != nil {
return nil, fmt.Errorf("sending request failed for method '%s': %w", method, err)
}
switch method {
case "select", "create", "update", "merge", "patch", "insert":
return db.resp(method, params, resp)
case "delete":
return nil, nil
default:
return resp, nil
}
}
// resp is a helper method for parsing the response from a query.
func (db *DB) resp(_ string, _ []interface{}, res interface{}) (interface{}, error) {
if res == nil {
return nil, constants.ErrNoRow
}
return res, nil
}