forked from fenos/dqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqlx.go
131 lines (106 loc) · 3.73 KB
/
dqlx.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
package dqlx
import (
"context"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"google.golang.org/grpc"
)
var symbolValuePlaceholder = "??"
var symbolEdgeTraversal = "->"
// DQLizer implementors are able to define a custom dql statement
type DQLizer interface {
ToDQL() (query string, args []interface{}, err error)
}
// Executor implementors are able to define a custom way
// of executing queries and mutations
type Executor interface {
ExecuteQueries(ctx context.Context, queries ...QueryBuilder) (*Response, error)
ExecuteMutations(ctx context.Context, mutations ...MutationBuilder) (*Response, error)
}
// DB represents the public API for interacting with a DGraph
// Database
type DB interface {
Query(rootFn *FilterFn) QueryBuilder
QueryType(typeName string) QueryBuilder
QueryEdge(edgeName string, rootQueryFn *FilterFn) QueryBuilder
Mutation() MutationBuilder
ExecuteQueries(ctx context.Context, queries []QueryBuilder, options ...OperationExecutorOptionFn) (*Response, error)
ExecuteMutations(ctx context.Context, mutations []MutationBuilder, options ...OperationExecutorOptionFn) (*Response, error)
Schema() *SchemaBuilder
NewTxn() *dgo.Txn
NewReadOnlyTxn() *dgo.Txn
GetDgraph() *dgo.Dgraph
}
type dqlx struct {
dgraph *dgo.Dgraph
executor Executor
}
// Connect connects to a DGraph Cluster.
// upon success a DB instance is returned ready to be used
// to interact with DGraph
func Connect(addresses ...string) (DB, error) {
clients := make([]api.DgraphClient, len(addresses))
for index, address := range addresses {
dial, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
clients[index] = api.NewDgraphClient(dial)
}
dgraph := dgo.NewDgraphClient(clients...)
return FromClient(dgraph), nil
}
// FromClient creates a DB instance from a raw dgraph client
func FromClient(dgraph *dgo.Dgraph) DB {
return &dqlx{
dgraph: dgraph,
}
}
// Query returns a QueryBuilder
func (dqlx *dqlx) Query(rootFn *FilterFn) QueryBuilder {
return Query(rootFn).WithDClient(dqlx.dgraph)
}
// QueryType returns a QueryBuilder with a default type() filter
func (dqlx *dqlx) QueryType(typeName string) QueryBuilder {
return QueryType(typeName).WithDClient(dqlx.dgraph)
}
// QueryEdge returns a QueryBuilder with the ability of providing the query name
func (dqlx *dqlx) QueryEdge(edgeName string, rootQueryFn *FilterFn) QueryBuilder {
return QueryEdge(edgeName, rootQueryFn).WithDClient(dqlx.dgraph)
}
// Mutation returns a MutationBuilder
func (dqlx *dqlx) Mutation() MutationBuilder {
return Mutation().WithDClient(dqlx.dgraph)
}
// ExecuteQueries executes multiple queries joining them into 1 request
func (dqlx *dqlx) ExecuteQueries(ctx context.Context, queries []QueryBuilder, options ...OperationExecutorOptionFn) (*Response, error) {
executor := NewDGoExecutor(dqlx.dgraph)
for _, option := range options {
option(executor)
}
return executor.ExecuteQueries(ctx, queries...)
}
// ExecuteMutations executes multiple mutations
func (dqlx *dqlx) ExecuteMutations(ctx context.Context, mutations []MutationBuilder, options ...OperationExecutorOptionFn) (*Response, error) {
executor := NewDGoExecutor(dqlx.dgraph)
for _, option := range options {
option(executor)
}
return executor.ExecuteMutations(ctx, mutations...)
}
// Schema returns a schema builder
func (dqlx *dqlx) Schema() *SchemaBuilder {
return NewSchema().WithClient(dqlx.dgraph)
}
// NewTxn creates a new transaction
func (dqlx *dqlx) NewTxn() *dgo.Txn {
return dqlx.dgraph.NewTxn()
}
// NewReadOnlyTxn creates a new read-only transaction
func (dqlx *dqlx) NewReadOnlyTxn() *dgo.Txn {
return dqlx.NewReadOnlyTxn()
}
// GetDgraph returns the dgraph client
func (dqlx *dqlx) GetDgraph() *dgo.Dgraph {
return dqlx.dgraph
}