dgman

package module
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2020 License: Apache-2.0 Imports: 17 Imported by: 8

README

Build Status Coverage Status GoDoc

Dgman is a schema manager for Dgraph using the Go Dgraph client (dgo), which manages Dgraph types, schema, and indexes from Go tags in struct definitions, allowing ORM-like convenience for developing Dgraph clients in Go.

Features

  • Create types (Dgraph v1.1+), schemas, and indexes from struct tags.
  • Detect conflicts from existing schema and defined schema.
  • Mutate Helpers (Create, Update, Upsert).
  • Autoinject node type from struct.
  • Field unique checking (e.g: emails, username).
  • Query helpers.
  • Delete helper.

Roadmap

  • Query builders
  • Query fragments

Table of Contents

Installation

Using go get:

go get -u github.com/dolan-in/dgman

Usage

import(
	"github.com/dolan-in/dgman"
)

Schema Definition

Schemas are defined using Go structs which defines the predicate name from the json tag, indices and directives using the dgraph tag. To define a dgraph node struct, json fields uid and dgraph.type is required.

Node Types

Node types will be inferred from the struct name.

If you need to define a custom name for the node type, you can define it on the dgraph tag on the dgraph.type field.

type CustomNodeType struct {
	UID 	string 		`json:"uid,omitempty"`
	Name 	string 		`json:"name,omitempty"`
	DType	[]string 	`json:"dgraph.type" dgraph:"CustomNodeType"`
}
CreateSchema

Using the CreateSchema function, it will install the schema, and detect schema and index conflicts within the passed structs and with the currently existing schema in the specified Dgraph database.

// User is a node, nodes have a uid and a dgraph.type json field
type User struct {
	UID      string     `json:"uid,omitempty"`
	Name     string     `json:"name,omitempty" dgraph:"index=term"` // use term index 
	Username string     `json:"username,omitempty" dgraph:"index=hash"` // use hash index
	Email    string     `json:"email,omitempty" dgraph:"index=hash upsert"` // use hash index, use upsert directive
	Password string     `json:"password,omitempty"`
	Height   *int       `json:"height,omitempty"`
	Dob      *time.Time `json:"dob,omitempty"` // will be inferred as dateTime schema type
	Status   EnumType   `json:"status,omitempty" dgraph="type=int"`
	Created  time.Time  `json:"created,omitempty" dgraph:"index=day"` // will be inferred as dateTime schema type, with day index
	Mobiles  []string   `json:"mobiles,omitempty"` // will be inferred as using the  [string] schema type, slices with primitive types will all be inferred as lists
	Schools  []School   `json:"schools,omitempty" dgraph:"count reverse"` // defines an edge to other nodes, add count index, add reverse edges
	DType    []string   `json:"dgraph.type,omitempty"`
}

// School is another node, that will be connected to User node using the schools predicate
type School struct {
	UID      string 	`json:"uid,omitempty"`
	Name     string 	`json:"name,omitempty"`
	Location *GeoLoc 	`json:"location,omitempty" dgraph:"type=geo"` // for geo schema type, need to specify explicitly
	DType    []string   `json:"dgraph.type,omitempty"`
}

type GeoLoc struct {
	Type  string    `json:"type"`
	Coord []float64 `json:"coordinates"`
}

func main() {
	d, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}

	c := dgo.NewDgraphClient(api.NewDgraphClient(d))

	// create the schema, 
	// it will only install non-existing schema in the specified database
	schema, err := dgman.CreateSchema(c, &User{})
	if err != nil {
		panic(err)
	}
	// Check the generated schema
	fmt.Println(schema)
}

On an empty database, the above code will return the generated type and schema string used to create the schema, logging the conflicting schemas in the process:

2018/12/14 02:23:48 conflicting schema name, already defined as "name: string @index(term) .", trying to define "name: string ."
status: int .
mobiles: [string] .
email: string @index(hash) @upsert .
password: string .
height: int .
dob: datetime .
schools: [uid] @count @reverse .
name: string @index(term) .
username: string @index(hash) .
created: datetime @index(day) .
location: geo .

type School {
        location
        name
}
type User {
        status
        created
        username
        password
        height
        dob
        name
        email
        mobiles
        schools
}

When schema conflicts is detected with the existing schema already installed in the database, it will only log the differences. You would need to manually correct the conflicts by dropping or updating the schema manually.

This may be useful to prevent unnecessary or unwanted re-indexing of your data.

MutateSchema

To overwrite/update index definitions, you can use the MutateSchema function, which will update the schema indexes.

	// update the schema indexes
	schema, err := dgman.MutateSchema(c, &User{})
	if err != nil {
		panic(err)
	}
	// Check the generated schema
	fmt.Println(schema)

Mutate Helpers

Mutate

Using the Mutate function, before sending a mutation, it will marshal a struct into JSON and injecting the Dgraph node type ("dgraph.type" predicate).

user := User{
	Name: "Alexander",
	Email: "alexander@gmail.com",
	Username: "alex123",
}

// Create a transaction with context.Background() as the context
// can be shorthened to dgman.NewTxn(c)
tx := dgman.NewTxnContext(context.Background(), c)
// pass true as the second parameter to commit now
if err := tx.Mutate(&user, true); err != nil {
	panic(err)
}

// UID will be set
fmt.Println(user.UID)

The above will insert a node with the following JSON string, with the field "dgraph.type":["User"] added in:

{"email":"alexander@gmail.com","username":"alex123","dgraph.type":["User"]}
Create (Mutate with Unique Checking)

If you need unique checking for a particular field of a node with a certain node type, e.g: Email of users, you can use the Create function.

To define a field to be unique, add unique in the dgraph tag on the struct definition.

type User struct {
	UID 			string `json:"uid,omitempty"`
	Name 			string `json:"name,omitempty" dgraph:"index=term"`
	Email 			string `json:"email,omitempty" dgraph:"index=hash unique"`
	Username 		string `json:"username,omitempty" dgraph:"index=term unique"`
}

...
	user := User{
		Name: "Alexander",
		Email: "alexander@gmail.com",
		Username: "alex123",
	}

	tx := dgman.NewTxn(c)
	if err := tx.Create(&user, true); err != nil {
		panic(err)
	}
	
	// try to create user with a duplicate email
	duplicateEmail := User{
		Name: "Alexander",
		Email: "alexander@gmail.com",
		Username: "alexa",
	}

	// will return a dgman.UniqueError
	tx = dgman.NewTxnContext(context.Background(), c)
	if err := tx.Create(&duplicateEmail, true); err != nil {
		if uniqueErr, ok := err.(*dgman.UniqueError); ok {
			// check the duplicate field
			fmt.Println(uniqueErr.Field, uniqueErr.Value)
		}
	}

Update (Mutate existing node with Unique Checking)

This is similar to Create, but for existing nodes. So the uid field must be specified.

type User struct {
	UID 			string 		`json:"uid,omitempty"`
	Name 			string 		`json:"name,omitempty"`
	Email 			string 		`json:"email,omitempty" dgraph:"index=hash unique"`
	Username 		string 		`json:"username,omitempty" dgraph:"index=term unique"`
	Dob				time.Time	`json:"dob" dgraph:"index=day"`
}

...
	users := []*User{
		User{
			Name: "Alexander",
			Email: "alexander@gmail.com",
			Username: "alex123",
		},
		User{
			Name: "Fergusso",
			Email: "fergusso@gmail.com",
			Username: "fergusso123",
		},
	}

	tx := dgman.NewTxn(c)
	if err := tx.Create(&users, true); err != nil {
		panic(err)
	}
	
	// try to update the user with existing username
	alexander := users[0]
	alexander.Username = "fergusso123"
	// UID should have a value
	fmt.Println(alexander.UID)

	// will return a dgman.UniqueError
	tx := dgman.NewTxn(c)
	if err := tx.Update(&alexander, true); err != nil {
		if uniqueErr, ok := err.(*dgman.UniqueError); ok {
			// will return duplicate error for username
			fmt.Println(uniqueErr.Field, uniqueErr.Value)
		}
	}

	// try to update the user with non-existing username
	alexander.Username = "wildan"

	tx = dgman.NewTxn(c)
	if err := tx.Update(&alexander, true); err != nil {
		panic(err)
	}

	// should be updated
	fmt.Println(alexander)

Upsert

Upsert updates a node if a node with a value of a specified predicate already exists, otherwise insert the node.

type User struct {
	UID 			string 		`json:"uid,omitempty"`
	Name 			string 		`json:"name,omitempty"`
	Email 			string 		`json:"email,omitempty" dgraph:"index=hash unique"`
	Username 		string 		`json:"username,omitempty" dgraph:"index=term unique"`
	Dob				time.Time	`json:"dob" dgraph:"index=day"`
}

...
	users := []*User{
		User{
			Name: "Alexander",
			Email: "alexander@gmail.com",
			Username: "alex123",
		},
		User{
			Name: "Fergusso",
			Email: "fergusso@gmail.com",
			Username: "fergusso123",
		},
	}

	tx := dgman.NewTxn(c)
	if err := tx.Upsert(&users, "email", true); err != nil {
		panic(err)
	}
Create Or Get

CreateOrGet creates a node if a node with a value of a specified predicate does not exist, otherwise return the node.

	users := []*User{
		User{
			Name: "Alexander",
			Email: "alexander@gmail.com",
			Username: "alex123",
		},
		User{
			Name: "Fergusso",
			Email: "fergusso@gmail.com",
			Username: "fergusso123",
		},
	}

	tx := dgman.NewTxn(c)
	if err := tx.CreateOrGet(&users, "email", true); err != nil {
		panic(err)
	}

Query Helpers

Queries and Filters can be constructed by using ordinal parameter markers in query or filter strings, for example $1, $2, which should be safe against injections. Alternatively, you can also pass GraphQL named vars, with the Query.Vars method, although you have to manually convert your data into strings.

Get by Filter
name := "wildanjing"

tx := dgman.NewReadOnlyTxn(c)

user := User{}
// get node with node type `user` that matches filter
err := tx.Get(&user).
	Filter("allofterms(name, $1)", name). // dgraph filter
	All(1). // returns all predicates, expand on 1 level of edge predicates
	Node() // get single node from query
if err != nil {
	if err == dgman.ErrNodeNotFound {
		// node using the specified filter not found
	}
}

// struct will be populated if found
fmt.Println(user)
Get by query

Get by query

tx := dgman.NewReadOnlyTxn(c)

users := []User{}
// get nodes with node type `user` that matches filter
err := tx.Get(&users).
	Query(`{
		uid
		name
		friends @filter(allofterms(name, $1)) {
			uid 
			name
		}
		schools @filter(allofterms(name, $2)) {
			uid
			name
		}
	}`, "wildan", "harvard"). // dgraph query portion (without root function)
	OrderAsc("name"). // ordering ascending by predicate
	OrderDesc("dob"). // multiple ordering is allowed
	First(10). // get first 10 nodes from result
	Nodes() // get all nodes from the prepared query
if err != nil {
}

// slice will be populated if found
fmt.Println(users)
Get by filter query

You can also combine Filter with Query.

name := "wildanjing"
friendName := "wildancok"
schoolUIDs := []string{"0x123", "0x1f"}

tx := dgman.NewReadOnlyTxn(c)

users := []User{}
// get nodes with node type `user` that matches filter
err := tx.Get(&users).
	Filter("allofterms(name, $1)", name).
	Query(`{
		uid
		name
		friends @filter(name, $1) {
			uid 
			name
		}
		schools @filter(uid_in($2)) {
			uid
			name
		}
	}`, friendName, dgman.UIDs(schoolUIDs)). // UIDs is a helper type to parse list of uids as a parameter
	OrderAsc("name"). // ordering ascending by predicate
	OrderDesc("dob"). // multiple ordering is allowed
	First(10). // get first 10 nodes from result
	Nodes() // get all nodes from the prepared query
if err != nil {
}

// slice will be populated if found
fmt.Println(users)
Get by UID
// Get by UID
tx := dgman.NewReadOnlyTxn(c)

user := User{}
if err := tx.Get(&user).UID("0x9cd5").Node(); err != nil {
	if err == dgman.ErrNodeNotFound {
		// node not found
	}
}

// struct will be populated if found
fmt.Println(user)
Get and Count
tx := dgman.NewReadOnlyTxn(c)

users := []*User{}

count, err := tx.Get(&users).
	Filter(`anyofterms(name, "wildan")`).
	First(3).
	Offset(3).
	NodesAndCount()

// count should return total of nodes regardless of pagination
fmt.Println(count)
Custom Scanning Query results

You can alternatively specify a different destination for your query results, by passing it as a parameter to the Node or Nodes.

type checkPassword struct {
	Valid `json:"valid"`	
}

result := &checkPassword{}

tx := dgman.NewReadOnlyTxnContext(ctx, s.c)
err := tx.Get(&User{}). // User here is only to specify the node type
	Filter("eq(email, $1)", email).
	Query(`{ valid: checkpwd(password, $1) }`, password).
	Node(result)

fmt.Println(result.Valid)
Multiple Query Blocks

You can specify multiple query blocks, by passing multiple Query objects into tx.Query.

tx := dgman.NewReadOnlyTxn(c)

type pagedResults struct {
	Paged    []*User `json:"paged"`
	PageInfo []struct {
		Total int
	}
}

result := &pagedResults{}

query := tx.
	Query(
		dgman.NewQuery().
			As("result"). // sets a variable name to the root query
			Var(). // sets the query as a var, making it not returned in the results
			Type(&User{}). // sets the node type to query by
			Filter(`anyofterms(name, $name)`),
		dgman.NewQuery().
			Name("paged"). // query block name to be returned in the query
			UID("result"). // uid from var
			First(2).
			Offset(2).
			All(1),
		dgman.NewQuery().
			Name("pageInfo").
			UID("result").
			Query(`{ total: count(uid) }`),
	).
	Vars("getByName($name: string)", map[string]string{"$name": "wildan"}) // GraphQL query variables

if err := query.Scan(&result); err != nil {
	panic(err)
}

// result should be populated
fmt.Println(result)

Delete Helper

Delete Nodes

Delete helpers can be used to simplify deleting nodes that matches a query, using the same query format as Query Helpers.

tx := dgman.NewTxn(c)

query := `@filter() {
	uid
	expand(_all_) {
		uid
	}
}`
// delete all nodes with node type `user` that matches query
// all edge nodes that are specified in the query will also be deleted
deletedUids, err := tx.Delete(&User{}, true).
	Vars("getUsers($name: string)", map[string]string{"$name": "wildan"}). // function defintion and Graphql variables
	Query(query). // dgraph query portion (without root function)
	OrderAsc("name"). // ordering ascending by predicate
	OrderDesc("dob"). // multiple ordering is allowed
	First(10). // get first 10 nodes from result
	Nodes() // delete all nodes from the prepared query
if err != nil {
}

// check the deleted uids
fmt.Println(deletedUids)
Delete Edges

For deleting edges, you only need to specify node UID, edge predicate, and edge UIDs

tx := dgman.NewTxn(c)
err := tx.Delete(&User{}, true).
	Edge("0x12", "schools", "0x13", "0x14")

If no edge UIDs are specified, all edges of the specified predicate will be deleted.

tx := dgman.NewTxn(c)
err := tx.Delete(&User{}, true).
	Edge("0x12", "schools")

Development

Make sure you have a running dgraph cluster, and set the DGMAN_TEST_DATABASE environment variable to the connection string of your dgraph alpha grpc connection, e.g: localhost:9080.

Run the tests:

go test -v .

Documentation

Overview

* Copyright (C) 2019 Dolan and Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright (C) 2019 Dolan and Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

* Copyright (C) 2019 Dolan and Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNodeNotFound = errors.New("node not found")
)

Functions

func GetNodeType

func GetNodeType(data interface{}) string

GetNodeType gets node type from the struct name, or "dgraph" tag in the "dgraph.type" predicate/json tag

func SetTypes added in v1.3.3

func SetTypes(data interface{}) error

SetTypes recursively walks all structures in data and sets the value of the `dgraph.type` struct field. The type, in order of preference, is either the value of the `dgraph` struct tag on the `dgraph.type` struct field, or the struct name. Courtesy of @freb

Types

type Deleter added in v0.5.0

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

func (*Deleter) After added in v0.5.0

func (d *Deleter) After(uid string) *Deleter

func (*Deleter) All added in v0.5.0

func (d *Deleter) All(depthParam ...int) *Deleter

All returns expands all predicates, with a depth parameter that specifies how deep should edges be expanded

func (*Deleter) Edge added in v0.6.0

func (d *Deleter) Edge(uid, edgePredicate string, edgeUIDs ...string) error

Edge delete edges of a node of specified edge predicate, if no edgeUIDs specified, delete all edges

func (*Deleter) Filter added in v0.5.0

func (d *Deleter) Filter(filter string, params ...interface{}) *Deleter

Filter defines a query filter, return predicates at the first depth

func (*Deleter) First added in v0.5.0

func (d *Deleter) First(n int) *Deleter

func (*Deleter) Node added in v0.5.0

func (d *Deleter) Node() (uids []string, err error)

Node deletes the first single root node from the query including edge nodes that may be specified on the query

func (*Deleter) Nodes added in v0.5.0

func (d *Deleter) Nodes() (uids []string, err error)

Nodes deletes all nodes matching the delete query including edge nodes that may be specified on the query

func (*Deleter) Offset added in v0.5.0

func (d *Deleter) Offset(n int) *Deleter

func (*Deleter) OrderAsc added in v0.5.0

func (d *Deleter) OrderAsc(clause string) *Deleter

func (*Deleter) OrderDesc added in v0.5.0

func (d *Deleter) OrderDesc(clause string) *Deleter

func (*Deleter) Query added in v0.5.0

func (d *Deleter) Query(query string, params ...interface{}) *Deleter

Query defines the query portion other than the root function for deletion

func (*Deleter) RootFunc added in v0.5.1

func (d *Deleter) RootFunc(rootFunc string) *Deleter

RootFunc modifies the dgraph query root function, if not set, the default is "type(NodeType)"

func (*Deleter) String added in v0.5.0

func (d *Deleter) String() string

func (*Deleter) UID added in v0.6.0

func (d *Deleter) UID(uid string) *Deleter

UID returns the node with the specified uid

func (*Deleter) Vars added in v0.5.0

func (d *Deleter) Vars(funcDef string, vars map[string]string) *Deleter

Vars specify the GraphQL variables to be passed on the query, by specifying the function definition of vars, and variable map. Example funcDef: getUserByEmail($email: string, $age: number)

type PageInfo added in v1.3.0

type PageInfo struct {
	Count int
}

type PagedResults added in v1.3.0

type PagedResults struct {
	Result   json.RawMessage
	PageInfo []*PageInfo
}

type ParamFormatter added in v1.0.0

type ParamFormatter interface {
	FormatParams() []byte
}

ParamFormatter provides an interface for types to implement custom parameter formatter for query parameters

type Query added in v0.5.0

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

func NewQuery added in v1.3.0

func NewQuery() *Query

NewQuery returns a new empty query

func (*Query) After added in v0.5.0

func (q *Query) After(uid string) *Query

After uses default UID ordering to skip directly past a node specified by UID

func (*Query) All added in v0.5.0

func (q *Query) All(depthParam ...int) *Query

All returns expands all predicates, with a depth parameter that specifies how deep should edges be expanded

func (*Query) As added in v1.3.0

func (q *Query) As(varName string) *Query

As defines a query variable name https://dgraph.io/docs/query-language/#query-variables

func (*Query) Filter added in v0.5.0

func (q *Query) Filter(filter string, params ...interface{}) *Query

Filter defines a query filter, return predicates at the first depth

func (*Query) First added in v0.5.0

func (q *Query) First(n int) *Query

First returns n number of results

func (*Query) GroupBy added in v1.3.0

func (q *Query) GroupBy(predicate string) *Query

GroupBy defines the predicate to group the query by

func (*Query) Name added in v1.3.0

func (q *Query) Name(queryName string) *Query

Name defines the query block name, which identifies the query results

func (*Query) Node added in v0.5.0

func (q *Query) Node(dst ...interface{}) (err error)

Node returns the first single node from the query, optional destination can be passed, otherwise bind to model

func (*Query) Nodes added in v0.5.0

func (q *Query) Nodes(dst ...interface{}) error

Nodes returns all results from the query, optional destination can be passed, otherwise bind to model

func (*Query) NodesAndCount added in v1.3.0

func (q *Query) NodesAndCount() (count int, err error)

NodesAndCount return paged nodes result with the total count of the query

func (*Query) Offset added in v0.5.0

func (q *Query) Offset(n int) *Query

Offset skips n number of results

func (*Query) OrderAsc added in v0.5.0

func (q *Query) OrderAsc(clause string) *Query

OrderAsc adds an ascending order clause

func (*Query) OrderDesc added in v0.5.0

func (q *Query) OrderDesc(clause string) *Query

OrderDesc adds an descending order clause

func (*Query) Query added in v0.5.0

func (q *Query) Query(query string, params ...interface{}) *Query

Query defines the query portion other than the root function

func (*Query) RootFunc added in v0.5.1

func (q *Query) RootFunc(rootFunc string) *Query

RootFunc modifies the dgraph query root function, if not set, the default is "type(NodeType)"

func (*Query) String added in v0.5.0

func (q *Query) String() string

func (*Query) Type added in v1.3.0

func (q *Query) Type(model interface{}) *Query

Type sets the model struct to infer the node type

func (*Query) UID added in v0.5.0

func (q *Query) UID(uid string) *Query

UID returns the node with the specified uid

func (*Query) Var added in v1.3.0

func (q *Query) Var() *Query

Var defines whether a query block is a var, which are not returned in query results

func (*Query) Vars added in v0.5.0

func (q *Query) Vars(funcDef string, vars map[string]string) *Query

Vars specify the GraphQL variables to be passed on the query, by specifying the function definition of vars, and variable map. Example funcDef: getUserByEmail($email: string)

type QueryBlock added in v1.3.0

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

func NewQueryBlock added in v1.3.0

func NewQueryBlock(queries ...*Query) *QueryBlock

NewQueryBlock returns a new empty query block

func (*QueryBlock) Add added in v1.3.0

func (q *QueryBlock) Add(query ...*Query) *QueryBlock

Add adds queries to the query block

func (*QueryBlock) Blocks added in v1.3.0

func (q *QueryBlock) Blocks(query ...*Query) *QueryBlock

Blocks set the query blocks

func (*QueryBlock) Scan added in v1.3.0

func (q *QueryBlock) Scan(dst interface{}) error

Scan unmarshals the query result into provided destination

func (*QueryBlock) String added in v1.3.0

func (q *QueryBlock) String() string

func (*QueryBlock) Vars added in v1.3.0

func (q *QueryBlock) Vars(funcDef string, vars map[string]string) *QueryBlock

Vars specify the GraphQL variables to be passed on the query, by specifying the function definition of vars, and variable map. Example funcDef: getUserByEmail($email: string)

type Schema

type Schema struct {
	Predicate  string
	Type       string
	Index      bool
	Tokenizer  []string
	Reverse    bool
	Count      bool
	List       bool
	Upsert     bool
	Noconflict bool `json:"no_conflict"`
	Unique     bool
}

func (Schema) String

func (s Schema) String() string

type SchemaMap

type SchemaMap map[string]*Schema

SchemaMap maps the underlying schema defined for a predicate

func (SchemaMap) String

func (s SchemaMap) String() string

type SchemaType added in v1.1.1

type SchemaType interface {
	SchemaType() string
}

SchemaType allows defining a custom type as a dgraph schema type

type TxnContext added in v1.0.0

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

TxnContext is dgo transaction coupled with context

func NewReadOnlyTxn added in v1.0.0

func NewReadOnlyTxn(c *dgo.Dgraph) *TxnContext

NewReadOnlyTxn creates a new read only transaction

func NewReadOnlyTxnContext added in v1.0.0

func NewReadOnlyTxnContext(ctx context.Context, c *dgo.Dgraph) *TxnContext

NewReadOnlyTxnContext creates a new read only transaction coupled with a context

func NewTxn added in v1.0.0

func NewTxn(c *dgo.Dgraph) *TxnContext

NewTxn creates a new transaction

func NewTxnContext added in v1.0.0

func NewTxnContext(ctx context.Context, c *dgo.Dgraph) *TxnContext

NewTxnContext creates a new transaction coupled with a context

func (*TxnContext) BestEffort added in v1.0.0

func (t *TxnContext) BestEffort() *TxnContext

BestEffort enables best effort in read-only queries.

func (*TxnContext) Commit added in v1.0.0

func (t *TxnContext) Commit() error

Commit calls Commit on the dgo transaction.

func (*TxnContext) Context added in v1.0.0

func (t *TxnContext) Context() context.Context

Context returns the transaction context

func (*TxnContext) Create added in v1.0.0

func (t *TxnContext) Create(data interface{}, commitNow ...bool) error

Create create node(s) with field unique checking, similar to Mutate, will inject node type from the Struct name

func (*TxnContext) CreateOrGet added in v1.0.0

func (t *TxnContext) CreateOrGet(data interface{}, predicate string, commitNow ...bool) error

CreateOrGet will create a node or if a node with a value from the passed predicate exists, return the node

func (*TxnContext) Delete added in v1.0.0

func (t *TxnContext) Delete(model interface{}, commitNow ...bool) *Deleter

Delete prepares a delete mutation using a query

func (*TxnContext) Discard added in v1.0.0

func (t *TxnContext) Discard() error

Discard calls Discard on the dgo transaction.

func (*TxnContext) Get added in v1.0.0

func (t *TxnContext) Get(model interface{}) *Query

Get prepares a query for a model

func (*TxnContext) Mutate added in v1.0.0

func (t *TxnContext) Mutate(data interface{}, commitNow ...bool) error

Mutate is a shortcut to create mutations from data to be marshalled into JSON, it will inject the node type from the Struct name

func (*TxnContext) Query added in v1.3.0

func (t *TxnContext) Query(query ...*Query) *QueryBlock

Query prepares a query with multiple query block

func (*TxnContext) Txn added in v1.0.0

func (t *TxnContext) Txn() *dgo.Txn

Txn returns the dgo transaction

func (*TxnContext) Update added in v1.0.0

func (t *TxnContext) Update(data interface{}, commitNow ...bool) error

Update updates a node by their UID with field unique checking, similar to Mutate, will inject node type from the Struct name

func (*TxnContext) Upsert added in v1.0.0

func (t *TxnContext) Upsert(data interface{}, predicate string, commitNow ...bool) error

Upsert will update a node when a value from the passed predicate (with the node type) exists, otherwise insert the node. On all conditions, unique checking holds on the node type on other unique fields.

func (*TxnContext) WithContext added in v1.0.0

func (t *TxnContext) WithContext(ctx context.Context)

WithContext replaces the current transaction context

type TxnInterface added in v1.1.1

type TxnInterface interface {
	Commit() error
	Discard() error
	BestEffort() *TxnContext
	Txn() *dgo.Txn
	WithContext(context.Context)
	Context() context.Context
	Mutate(data interface{}, commitNow ...bool) error
	Create(data interface{}, commitNow ...bool) error
	Update(data interface{}, commitNow ...bool) error
	Upsert(data interface{}, predicate string, commitNow ...bool) error
	CreateOrGet(data interface{}, predicate string, commitNow ...bool) error
	Delete(model interface{}, commitNow ...bool) *Deleter
	Get(model interface{}) *Query
}

TxnInterface provides interface for dgman.TxnContext

type TypeMap added in v1.0.0

type TypeMap map[string]SchemaMap

TypeMap maps a dgraph type with its predicates

func (TypeMap) String added in v1.0.0

func (t TypeMap) String() string

type TypeSchema added in v1.0.0

type TypeSchema struct {
	Types  TypeMap
	Schema SchemaMap
}

func CreateSchema

func CreateSchema(c *dgo.Dgraph, models ...interface{}) (*TypeSchema, error)

CreateSchema generate indexes, schema, and types from struct models, returns the created schema map and types, does not update duplicate/conflict predicates.

func MutateSchema added in v0.4.1

func MutateSchema(c *dgo.Dgraph, models ...interface{}) (*TypeSchema, error)

MutateSchema generate indexes and schema from struct models, attempt updates for type, schema, and indexes.

func NewTypeSchema added in v1.0.0

func NewTypeSchema() *TypeSchema

NewTypeSchema returns a new TypeSchema with allocated Schema and Types

func (*TypeSchema) Marshal added in v1.0.0

func (t *TypeSchema) Marshal(parseType bool, models ...interface{})

Marshal marshals passed models into type and schema definitions

func (*TypeSchema) String added in v1.0.0

func (t *TypeSchema) String() string

type UID added in v1.0.0

type UID string

UID type allows passing uid's as query parameters

func (UID) FormatParams added in v1.0.0

func (u UID) FormatParams() []byte

FormatParams implements the ParamFormatter interface

type UIDs added in v1.0.0

type UIDs []string

UIDs type allows passing list of uid's as query parameters

func (UIDs) FormatParams added in v1.0.0

func (u UIDs) FormatParams() []byte

FormatParams implements the ParamFormatter interface

type UniqueError

type UniqueError struct {
	NodeType string
	Field    string
	Value    interface{}
	UID      string
}

UniqueError returns the field and value that failed the unique node check

func (*UniqueError) Error

func (u *UniqueError) Error() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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