client

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2017 License: AGPL-3.0, Apache-2.0 Imports: 9 Imported by: 0

README

Go client

GoDoc

This package provides helper function for interacting with the Dgraph server. You can use it to run mutations and queries. You can also use BatchMutation to upload data concurrently. It communicates with the server using gRPC.

Documentation

Overview

Package client is used to interact with a Dgraph server. Queries, mutations, and most other types of admin tasks can be run from the client.

Example (SetObject)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrFinished = errors.New("Transaction has already been committed or discarded")
)

Functions

func DeleteEdges added in v0.9.2

func DeleteEdges(mu *api.Mutation, uid string, predicates ...string)

DeleteEdges sets the edges corresponding to predicates on the node with the given uid for deletion. This helper function doesn't run the mutation on the server. It must be done by the user after the function returns.

Example
Output:

Types

type Dgraph added in v0.8.2

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

Dgraph is a transaction aware client to a set of dgraph server instances.

func NewDgraphClient added in v0.8.2

func NewDgraphClient(clients ...api.DgraphClient) *Dgraph

NewDgraphClient creates a new Dgraph for interacting with the Dgraph store connected to in conns. The client can be backed by multiple connections (to the same server, or multiple servers in a cluster).

A single client is thread safe for sharing with multiple go routines.

func (*Dgraph) Alter added in v0.9.0

func (d *Dgraph) Alter(ctx context.Context, op *api.Operation) error

By setting various fields of api.Operation, Alter can be used to do the following:

1. Modify the schema.

2. Drop a predicate.

3. Drop the database.

Example (DropAll)
Output:

func (*Dgraph) NewTxn added in v0.9.0

func (d *Dgraph) NewTxn() *Txn

NewTxn creates a new transaction.

type Txn added in v0.9.0

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

Txn is a single atomic transaction.

A transaction lifecycle is as follows:

1. Created using NewTxn.

2. Various Query and Mutate calls made.

3. Commit or Discard used. If any mutations have been made, It's important that at least one of these methods is called to clean up resources. Discard is a no-op if Commit has already been called, so it's safe to defer a call to Discard immediately after NewTxn.

func (*Txn) Commit added in v0.9.0

func (txn *Txn) Commit(ctx context.Context) error

Commit commits any mutations that have been made in the transaction. Once Commit has been called, the lifespan of the transaction is complete.

Errors could be returned for various reasons. Notably, ErrAborted could be returned if transactions that modify the same data are being run concurrently. It's up to the user to decide if they wish to retry. In this case, the user should create a new transaction.

func (*Txn) Discard added in v0.9.0

func (txn *Txn) Discard(ctx context.Context) error

Discard cleans up the resources associated with an uncommitted transaction that contains mutations. It is a no-op on transactions that have already been committed or don't contain mutations. Therefore it is safe (and recommended) to call as a deferred function immediately after a new transaction is created.

In some cases, the transaction can't be discarded, e.g. the grpc connection is unavailable. In these cases, the server will eventually do the transaction clean up.

func (*Txn) Mutate added in v0.9.0

func (txn *Txn) Mutate(ctx context.Context, mu *api.Mutation) (*api.Assigned, error)

Mutate allows data stored on dgraph instances to be modified. The fields in api.Mutation come in pairs, set and delete. Mutations can either be encoded as JSON or as RDFs.

If CommitNow is set, then this call will result in the transaction being committed. In this case, an explicit call to Commit doesn't need to subsequently be made.

If the mutation fails, then the transaction is discarded and all future operations on it will fail.

Example
Output:

Example (Bytes)
Output:

Example (DeleteNode)
Output:

Example (DeletePredicate)
Output:

func (*Txn) Query added in v0.9.0

func (txn *Txn) Query(ctx context.Context, q string) (*api.Response, error)

Query sends a query to one of the connected dgraph instances. If no mutations need to be made in the same transaction, it's convenient to chain the method, e.g. NewTxn().Query(ctx, "...").

Example (Unmarshal)
Output:

Example (Variables)
Output:

func (*Txn) QueryWithVars added in v0.9.0

func (txn *Txn) QueryWithVars(ctx context.Context, q string,
	vars map[string]string) (*api.Response, error)

QueryWithVars is like Query, but allows a variable map to be used. This can provide safety against injection attacks.

Jump to

Keyboard shortcuts

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