/
connection.go
46 lines (38 loc) · 1.04 KB
/
connection.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
package db
import (
"context"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
)
// ConfigDB is the configuration for DGraph
type ConfigDB struct {
DBClient *dgo.Dgraph
}
// NewClient sets up a gRPC and returns a new dgraph connection
func NewClient(url string) (*ConfigDB, error) {
// Dial a gRPC connection. The address to dial to can be configured when
// setting up the dgraph cluster.
dialOpts := append([]grpc.DialOption{},
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
d, err := grpc.Dial(url, dialOpts...)
if err != nil {
return nil, err
}
db := dgo.NewDgraphClient(
api.NewDgraphClient(d),
)
return &ConfigDB{
DBClient: db,
}, nil
}
// Setup initiates the schema into the database
func (config *ConfigDB) Setup() error {
// Install a schema into dgraph. Accounts have a `name` and a `balance`.
err := config.DBClient.Alter(context.Background(), &api.Operation{
Schema: Schema,
})
return err
}