ndgo

package module
Version: v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2019 License: MIT Imports: 7 Imported by: 3

README

ndgo Build Status codecov Go Report Card Maintainability GoDoc

ndgo provides dgraph dgo txn abstractions and helpers.

⚠️ Info: master branch is updated for dgraph 1.1 / dgo 2.0.

Why

  • Reduce txn related boilerplate, thus making code more readable,
  • Make using ratel like queries easier,
  • Get query execution times the easy way,
  • Don't do magic, keep dgo things exposed enough for them to be usable, if necessary.

Install

go get github.com/ppp225/ndgo

Common uses

Run transactions the same way one would do in ratel:

q := QueryJSON(fmt.Sprintf(`
  {
    %s(func: eq(%s, "%s")) {
      uid
      name
    }
  }
  `, "favActor", "name", "Keanu Reeves"))

resp, err := q.Run(txn)

Or marshal structs:

jsonBytes, err := json.Marshal(myObj)
if err != nil {
  return nil, err
}
resp, err := txn.Setb(jsonBytes)

Or don't:

resp, err := txn.Seti(myObj, myObj2, myObj3)

Do Upserts:

q := `{ a as var(func: eq(name, "Keanu")) }`
myObj := personStruct{
	UID:  "uid(a)",
	Type: "Person",
	Name: "Keanu",
}
resp, err := txn.DoSeti(q, myObj)

See TestBasic and TestComplex and TestTxnUpsert in ndgo_test.go for a complete example.

ndgo.Txn

Create a transaction:
dg := NewDgraphClient()
txn := ndgo.NewTxn(dg.NewTxn()) // or dg.NewReadOnlyTxn(), you can use any dgo.txn options you like. You can also use ndgo.NewTxnWithContext(ctx, txn)
defer txn.Discard()
...
err = txn.Commit()
Do mutations and queries:
resp, err := txn.Mutate(&api.Mutation{SetJson: jsonBytes})
resp, err := txn.Set(setString)
resp, err := txn.Setb(setBytes)
resp, err := txn.Seti(myObjs...)
resp, err := txn.Delete(deleteString)
resp, err := txn.Deleteb(deleteBytes)
resp, err := txn.Deletei(myObjs...)

resp, err := txn.Query(queryString)
resp, err := txn.QueryWithVars(queryWithVarsString, vars...)

resp, err := txn.Do(req *api.Request)
resp, err := txn.DoSetb(queryString, jsonString)
resp, err := txn.DoSetb(queryString, jsonBytes)
resp, err := txn.DoSeti(queryString, myObjs...)
resp, err := txn.DoSetnq(queryString, nquads)
Get diagnostics:
dbms := txn.GetDatabaseTime()
nwms := txn.GetNetworkTime()

ndgo.*JSON

Define and run txn's the same way as in ratel.

Set:
set := ndgo.SetJSON(fmt.Sprintf(`
  {
    "name": "%s",
    "age": "%s"
  }`, "L", "25"))

assigned, err := set.Run(txn)
Delete:
del := ndgo.DeleteJSON(fmt.Sprintf(`
  {
    "uid": "%s"
  }
  `, uid))

assigned, err := del.Run(txn)
Query:
q := ndgo.QueryJSON(fmt.Sprintf(`
  {
    %s(func: eq(%s, "%s")) {
      uid
    }
  }
  `, "favActor", "name", "Keanu Reeves"))

response, err := q.Run(txn)
Join:

You can chain queries with Join, assuming they are the same type:

response, err := ndgo.Query{}.
  GetPredUID("q1", "name", "Keanu").Join(ndgo.Query{}.
  GetPredUID("q2", "name", "L")).Join(ndgo.Query{}.
  GetPredUID("q3", "name", "Dio")).Run(txn)

Note, that query blocks have to be named uniquely.

Predefined queries

There are some predefined queries. They can be Joined and Run just like shown above.

Predefined queries and mutations:

func (Query) GetUIDExpandAll(queryID, uid string) QueryJSON {...}
func (Query) GetPredExpandAll(queryID, pred, val string) QueryJSON {...}
func (Query) GetPredExpandAllLevel2(queryID, pred, val string) QueryJSON {...}
func (Query) GetPredUID(queryID, pred, val string) QueryJSON {...}
func (Query) HasPredExpandAll(queryID, pred string) QueryJSON {...}

func (Query) DeleteNode(uid string) DeleteJSON {...}
func (Query) DeleteEdge(from, predicate, to string) DeleteJSON {...}

Other helpers

Flatten

Sometimes, when querying dgraph, results are nested too much, which can be de-nested one level with ndgo.Flatten:

var result interface{}
resp, err := query{}.myQuery().Run(txn)
if err != nil {
	log.Fatal(err)
}
if err := json.Unmarshal(resp.GetJson(), &result); err != nil {
	log.Fatal(err)
}
flattened := ndgo.Flatten(result)

Future plans

  • add more upsert things

Note

Everything may or may not change ¯\_(ツ)_/¯

Documentation

Overview

Package ndgo <read iNDiGO> provides dgo abstractions and helpers - github.com/ppp225/ndgo

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten

func Flatten(toFlatten interface{}) (result interface{})

Flatten flattens json/struct by 1 level. Root array should only 1 child/item

Types

type DeleteJSON

type DeleteJSON string

DeleteJSON represents a dgraph delete mutation string with some methods defined

func (DeleteJSON) Join

func (v DeleteJSON) Join(json DeleteJSON) DeleteJSON

Join allows to join multiple json Query of same type

func (DeleteJSON) Run

func (v DeleteJSON) Run(t *Txn) (resp *api.Response, err error)

Run makes a dgraph db delete mutation (need to be in an array for Join to work)

type Query

type Query struct{}

Query groups. Usage: ndgo.Query{}...

func (Query) DeleteEdge

func (Query) DeleteEdge(from, predicate, to string) DeleteJSON

DeleteEdge Usage: _, err = ndgo.Query{}.DeleteEdge(parentUID, "edgeName", childUID).Run(txn)

func (Query) DeleteNode

func (Query) DeleteNode(uid string) DeleteJSON

DeleteNode Usage: _, err = ndgo.Query{}.DeleteNode(UID).Run(txn)

func (Query) GetPredExpandAll

func (Query) GetPredExpandAll(queryID, pred, val string) QueryJSON

GetPredExpandAll Usage: ndgo.Query{}.GetPredExpandAll("q1", "userName", decode.Name).Run(txn)

func (Query) GetPredExpandAllLevel2

func (Query) GetPredExpandAllLevel2(queryID, pred, val string) QueryJSON

GetPredExpandAllLevel2 expands subnodes as well Usage: ndgo.Query{}.GetPredExpandAll("q1", "userName", decode.Name).Run(txn)

func (Query) GetPredUID

func (Query) GetPredUID(queryID, pred, val string) QueryJSON

GetPredUID Usage: Query{}.GetPredUID("q1", "userID", decode.UserID).Run(txn)

func (Query) GetUIDExpandAll

func (Query) GetUIDExpandAll(queryID, uid string) QueryJSON

GetUIDExpandAll Usage: ndgo.Query{}.GetUIDExpandAll("q", assigned.Uids["blank-0"]).Run(txn)

func (Query) HasPredExpandAll

func (Query) HasPredExpandAll(queryID, pred string) QueryJSON

HasPredExpandAll Usage: Query{}.HasPredExpandAll("q1", "userID", decode.UserID).Run(txn)

type QueryJSON

type QueryJSON string

QueryJSON represents a dgraph query string with some methods defined

func (QueryJSON) Join

func (v QueryJSON) Join(json QueryJSON) QueryJSON

Join allows to join multiple json Query of same type

func (QueryJSON) Run

func (v QueryJSON) Run(t *Txn) (resp *api.Response, err error)

Run makes a dgraph db query

type SetJSON

type SetJSON string

SetJSON represents a dgraph set mutation string with some methods defined

func (SetJSON) Join

func (v SetJSON) Join(json SetJSON) SetJSON

Join allows to join multiple json Query of same type

func (SetJSON) Run

func (v SetJSON) Run(t *Txn) (resp *api.Response, err error)

Run makes a dgraph db set mutation (need to be in an array for Join to work)

type Txn

type Txn struct {
	// contains filtered or unexported fields
}

Txn is a dgo.Txn wrapper with additional diagnostic data Helps with Queries, by providing abstractions for dgraph Query and Mutation

func NewTxn

func NewTxn(txn *dgo.Txn) *Txn

NewTxn creates new Txn

func NewTxnWithContext

func NewTxnWithContext(ctx context.Context, txn *dgo.Txn) *Txn

NewTxnWithContext creates new Txn (with ctx)

func (*Txn) Commit

func (v *Txn) Commit() (err error)

Commit commits dgo.Txn

func (*Txn) Delete

func (v *Txn) Delete(json string) (resp *api.Response, err error)

Delete is equivalent to Mutate using DeleteJson

func (*Txn) Deleteb

func (v *Txn) Deleteb(json []byte) (resp *api.Response, err error)

Deleteb is equivalent to Mutate using DeleteJson

func (*Txn) Deletei

func (v *Txn) Deletei(jsonMutations ...interface{}) (resp *api.Response, err error)

Deletei is equivalent to Deleteb, but it marshalls structs into one slice of mutations

func (*Txn) Discard

func (v *Txn) Discard()

Discard cleans up dgo.Txn resources. Always defer this on creation.

func (*Txn) Do

func (v *Txn) Do(req *api.Request) (resp *api.Response, err error)

Do executes a query followed by one or more mutations. Possible to run query without mutations, or vice versa

func (*Txn) DoSet

func (v *Txn) DoSet(query string, json string) (resp *api.Response, err error)

DoSet is equivalent to Do using mutation with SetJson

func (*Txn) DoSetb

func (v *Txn) DoSetb(query string, json []byte) (resp *api.Response, err error)

DoSetb is equivalent to Do using mutation with SetJson

func (*Txn) DoSetbi

func (v *Txn) DoSetbi(query string, jsonMutations ...interface{}) (resp *api.Response, err error)

DoSetbi is equivalent to DoSeti, but it uses single api.Mutation, as it marshalls structs into one slice of mutations

func (*Txn) DoSeti

func (v *Txn) DoSeti(query string, jsonMutations ...interface{}) (resp *api.Response, err error)

DoSeti is equivalent to Do, but it marshalls structs into mutations

func (*Txn) DoSetnq

func (v *Txn) DoSetnq(query string, nquads string) (resp *api.Response, err error)

DoSetnq is equivalent to Do using mutation with SetNquads

func (*Txn) GetDatabaseTime

func (v *Txn) GetDatabaseTime() float64

GetDatabaseTime gets time txn spend in db

func (*Txn) GetNetworkTime

func (v *Txn) GetNetworkTime() float64

GetNetworkTime gets total time until response

func (*Txn) Mutate

func (v *Txn) Mutate(mu *api.Mutation) (resp *api.Response, err error)

Mutate performs dgraph mutation

func (*Txn) Query

func (v *Txn) Query(q string) (resp *api.Response, err error)

Query performs dgraph query

func (*Txn) QueryWithVars

func (v *Txn) QueryWithVars(q string, vars map[string]string) (resp *api.Response, err error)

QueryWithVars performs dgraph query with vars

func (*Txn) Set

func (v *Txn) Set(json string) (resp *api.Response, err error)

Set is equivalent to Mutate using SetJson

func (*Txn) Setb

func (v *Txn) Setb(json []byte) (resp *api.Response, err error)

Setb is equivalent to Mutate using SetJson

func (*Txn) Seti

func (v *Txn) Seti(jsonMutations ...interface{}) (resp *api.Response, err error)

Seti is equivalent to Setb, but it marshalls structs into one slice of mutations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL