Hello, I was hoping someone more experienced could comment on my Dgraph database handler, whether it could be improved in any way, and whether it’s a bad idea to just keep the connection open all the time? Here’s the code:
package db
import (
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
)
type DgraphDB struct {
Client *dgo.Dgraph
Conn *grpc.ClientConn
}
var dgraphDB *dgo.Dgraph
func init() {
configureDgraphDB()
}
func configureDgraphDB() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
panic(err)
}
dgraphDB = dgo.NewDgraphClient(api.NewDgraphClient(conn))
}
func DgraphClient() *dgo.Dgraph {
if dgraphDB == nil {
doOnce.Do(configureDgraphDB)
}
return dgraphDB
}
And then I will be able to call the Dgraph client with:
db.DgraphClient()..Alter(context.Background(), &api.Operation{
Schema: `
username: string @index(term) .
password: password .
email: string @index(hash) @upsert .
token: string .
`,
})