Documentation
¶
Overview ¶
Package dga is a helper package to work with `github.com/dgraph-io/dgo` (v2), a Go client for accessing a DGraph cluster.
Index ¶
- Constants
- Variables
- type DGraphAccess
- func (d *DGraphAccess) AlterSchema(source string) error
- func (d *DGraphAccess) CommitTransaction() error
- func (d *DGraphAccess) CreateEdge(subject HasUID, predicate string, object interface{}) error
- func (d *DGraphAccess) CreateEdgeWithFacets(subject HasUID, predicate string, object interface{}, ...) error
- func (d *DGraphAccess) CreateNode(node HasUID) error
- func (d *DGraphAccess) DiscardTransaction() error
- func (d *DGraphAccess) FindEquals(result interface{}, predicateName, value string, literalPredicates ...string) error
- func (d *DGraphAccess) ForReadOnly(ctx context.Context) *DGraphAccess
- func (d *DGraphAccess) ForReadWrite(ctx context.Context) *DGraphAccess
- func (d *DGraphAccess) InTransactionDo(ctx context.Context, do func(da *DGraphAccess) error) error
- func (d *DGraphAccess) Upsert(query string, nQuads []NQuad) error
- func (d *DGraphAccess) UpsertRequest(query string, nQuads []NQuad) *api.Request
- func (d *DGraphAccess) WithTraceLogging() *DGraphAccess
- type DGraphTransaction
- type HasUID
- type Mutation
- type NQuad
- type RDFDatatype
- type UID
Examples ¶
Constants ¶
const ( // Star is used to model any predicate or any object in an NQuad. Star = "*" // DateTimeFormat is the format used by Dgraph for facet values of type dateTime. DateTimeFormat = "2006-01-02T15:04:05" // DgraphType is a reserved predicate name to refer to a type definition. DgraphType = "dgraph.type" )
const ( // RDFString is a RDF type RDFString = RDFDatatype("<xs:string>") RDFDateTime = RDFDatatype("<xs:dateTime>") RDFDate = RDFDatatype("<xs:date>") RDFInteger = RDFDatatype("<xs:int>") RDFBoolean = RDFDatatype("<xs:boolean>") RDFDouble = RDFDatatype("<xs:double>") RDFFloat = RDFDatatype("<xs:float>") )
see https://docs.dgraph.io/mutations/#language-and-rdf-types
Variables ¶
var ( // ErrNoClient is a DGraphAccess state error ErrNoClient = errors.New("dgo client not initialized") // ErrNoTransaction is a DGraphAccess state error ErrNoTransaction = errors.New("dgo transaction not created") // ErrNoContext is a DGraphAccess state error ErrNoContext = errors.New("dgo transaction context not created") )
var NoFacets map[string]interface{} = nil
NoFacets can be used in CreateEdge for passing no facets.
Functions ¶
This section is empty.
Types ¶
type DGraphAccess ¶
type DGraphAccess struct {
// contains filtered or unexported fields
}
DGraphAccess is a decorator for a dgo.Client that holds a Context and Transaction to perform queries and mutations.
func NewDGraphAccess ¶
func NewDGraphAccess(client *dgo.Dgraph) *DGraphAccess
NewDGraphAccess returns a new DGraphAccess using a client.
func (*DGraphAccess) AlterSchema ¶
func (d *DGraphAccess) AlterSchema(source string) error
AlterSchema uses a schema definition to change the current DGraph schema. This operation is idempotent. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) CommitTransaction ¶
func (d *DGraphAccess) CommitTransaction() error
CommitTransaction completes the current transaction. Return an error if the DGraphAccess is in the wrong state or if the Commit fails. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) CreateEdge ¶
func (d *DGraphAccess) CreateEdge(subject HasUID, predicate string, object interface{}) error
CreateEdge creates a new Edge (using an NQuad). Return an error if the mutation fails. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) CreateEdgeWithFacets ¶
func (d *DGraphAccess) CreateEdgeWithFacets(subject HasUID, predicate string, object interface{}, facetsOrNil map[string]interface{}) error
CreateEdgeWithFacets creates a new Edge (using an NQuad) that has facets (can be nil or empty) Return an error if the mutation fails. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) CreateNode ¶
func (d *DGraphAccess) CreateNode(node HasUID) error
CreateNode creates a new Node . Return an error if the mutation fails. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) DiscardTransaction ¶
func (d *DGraphAccess) DiscardTransaction() error
DiscardTransaction aborts the current transaction (unless absent).
func (*DGraphAccess) FindEquals ¶ added in v0.14.0
func (d *DGraphAccess) FindEquals(result interface{}, predicateName, value string, literalPredicates ...string) error
FindEquals populates the result with the result of matching a predicate with a value and optionally fetching other literal predicates.
func (*DGraphAccess) ForReadOnly ¶
func (d *DGraphAccess) ForReadOnly(ctx context.Context) *DGraphAccess
ForReadOnly returns a copy of DGraphAccess ready to perform read operations only.
func (*DGraphAccess) ForReadWrite ¶
func (d *DGraphAccess) ForReadWrite(ctx context.Context) *DGraphAccess
ForReadWrite returns a copy of DGraphAccess ready to perform mutations.
func (*DGraphAccess) InTransactionDo ¶
func (d *DGraphAccess) InTransactionDo(ctx context.Context, do func(da *DGraphAccess) error) error
InTransactionDo calls a function with a prepared DGraphAccess with a Write transaction. Return an error if the Commit fails.
func (*DGraphAccess) Upsert ¶
func (d *DGraphAccess) Upsert(query string, nQuads []NQuad) error
Upsert runs a mutation if the query has no results. Requires a DGraphAccess with a Write transaction.
func (*DGraphAccess) UpsertRequest ¶ added in v0.13.1
func (d *DGraphAccess) UpsertRequest(query string, nQuads []NQuad) *api.Request
func (*DGraphAccess) WithTraceLogging ¶
func (d *DGraphAccess) WithTraceLogging() *DGraphAccess
WithTraceLogging returns a copy of DGraphAccess that will trace parts of its internals.
type DGraphTransaction ¶ added in v0.12.0
type DGraphTransaction interface { Mutate(ctx context.Context, mu *api.Mutation) (*api.Response, error) Commit(ctx context.Context) error Discard(ctx context.Context) error Do(ctx context.Context, req *api.Request) (*api.Response, error) Query(ctx context.Context, q string) (*api.Response, error) }
DGraphTransaction exists for testing. It has only the methods this package needs from a *dgo.Txn
type Mutation ¶
Mutation represents an action with multiple RDF Triples represented by NQuad values.
type NQuad ¶
type NQuad struct { // Subject is the node for which the predicate must be created/modified. Subject UID // Predicate is a known schema predicate or a Star Predicate string // Object can be a primitive value or a UID or a Star (constant) Object interface{} // StorageType is used to optionally specify the type when storing the object // see https://docs.dgraph.io/mutations/#language-and-rdf-types // Example: dga.RDFString StorageType RDFDatatype // Maps to string, bool, int, float and dateTime. // For int and float, only 32-bit signed integers and 64-bit floats are accepted. Facets map[string]interface{} }
NQuad represents an RDF S P O pair.
func BlankNQuad ¶
BlankNQuad returns an NQuad value with a Blank UID subject. Use BlankUID if you want the object also to be a Blank UID from a name.
Example ¶
fmt.Println(BlankNQuad("subject", "predicate", 42).RDF())
Output: _:subject <predicate> "42" .
func (NQuad) WithStorageType ¶ added in v0.12.3
func (n NQuad) WithStorageType(t RDFDatatype) NQuad
WithStorageType returns a copy with its StorageType set. Use DetectStorageType(any interface{})
type RDFDatatype ¶ added in v0.12.3
type RDFDatatype string
RDFDatatype is to set the StorageType of an NQuad.
type UID ¶
type UID struct { // Str is exposed for JSON marshalling. Do not use it to read/write it directly. Str string // contains filtered or unexported fields }
UID represents a DGraph uid which can be expressed using an integer,string or undefined value.
func BlankUID ¶
BlankUID returns an UID with an undefined uid and a local name only valid for one write transaction. .RDF() => _:name
Example ¶
fmt.Println(BlankUID("canada").RDF())
Output: _:canada
func FunctionUID ¶ added in v0.12.0
FunctionUID returns an UID calling the uid function on the argument. .RDF() => uid(s)
Example ¶
fmt.Println(FunctionUID("v").RDF())
Output: uid(v)
func IntegerUID ¶
IntegerUID returns an UID using the integer value. .RDF() => <0x...>
Example ¶
fmt.Println(IntegerUID(42).RDF())
Output: <0x2a>
func StringUID ¶
StringUID returns an UID using a string value for uid. .RDF() => <id>
Example ¶
fmt.Println(StringUID("name").RDF())
Output: <name>
func (*UID) UnmarshalJSON ¶
UnmarshalJSON is part of JSON