ndgo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2019 License: MIT Imports: 6 Imported by: 0

README

ndgo Build Status codecov Go Report Card

ndgo provides dgraph dgo txn abstractions and helper func's.

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"))

response, err := q.Run(txn)

Code marshalled transactions are simplified as well:

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

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

ndgo.Txn

Create a transaction:
ndgo
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()
dgo (for comparison)
dg := NewDgraphClient()
ctx := context.Background()
txn := dg.NewTxn()
defer txn.Discard(ctx)
...
err = txn.Commit(v.ctx)
Do mutations and queries:
resp, err := txn.Set(setString)
resp, err := txn.Setb(setBytes)
resp, err := txn.Delete(deleteString)
resp, err := txn.Deleteb(deleteBytes)
resp, err := txn.Mutate(&api.Mutation{SetJson: jsonBytes})
resp, err := txn.Query(queryString)
resp, err := txn.QueryWithVars(queryWithVarsString, vars...)
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)

Plan

dgraph 1.1 transaction and upsert block helper.

Note

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

Documentation

Overview

Package ndgo <read iNDiGO> provides dgo abstractions and helper func's - 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.Assigned, 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

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.Assigned, 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.Assigned, err error)

Delete is equivalent to Mutate using DeleteJson

func (*Txn) Deleteb

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

Deleteb is equivalent to Mutate using DeleteJson

func (*Txn) Discard

func (v *Txn) Discard()

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

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.Assigned, 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.Assigned, err error)

Set is equivalent to Mutate using SetJson

func (*Txn) Setb

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

Setb is equivalent to Mutate using SetJson

Jump to

Keyboard shortcuts

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