-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
63 lines (49 loc) · 1.29 KB
/
shared.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
package dal
import (
"context"
"encoding/json"
"log"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
)
// Mutator is a wrapper to handle DGraph mutations
func Mutator(db *dgo.Dgraph, structToMutate interface{}) (uid string, err error) {
// Handle validation, etc
// Marshal to json for dgraph
dbQuery, err := json.Marshal(structToMutate)
if err != nil {
return "", err
}
// Set up transaction
txn := db.NewTxn()
defer txn.Discard(context.Background())
// Run the query
out, err := txn.Mutate(context.Background(), &api.Mutation{
SetJson: dbQuery,
})
if err != nil {
return "", err
}
txn.Commit(context.Background())
// Get the UID to return
for _, value := range out.GetUids() {
return value, nil
}
log.Fatal("Mutation didn't return a result")
return
}
// SimpleQuery is a genericized function to wrap queries for a node by UID
func SimpleQuery(db *dgo.Dgraph, q string, match string) (resp []byte, err error) {
// Set up transaction
txn := db.NewReadOnlyTxn()
defer txn.Discard(context.Background())
// Make variables map
variables := map[string]string{"$match": match}
// Run the query
out, err := txn.QueryWithVars(context.Background(), q, variables)
if err != nil {
return resp, err
}
txn.Commit(context.Background())
return out.GetJson(), err
}