Documentation ¶
Index ¶
- Constants
- Variables
- func Error(err error) error
- type AggregateType
- type AsyncQuerier
- type Config
- type DB
- func (d *DB) Alter(ctx context.Context, op *api.Operation) error
- func (d *DB) Cleanup()
- func (d *DB) Commit(ctx context.Context) error
- func (d *DB) Discard(ctx context.Context) error
- func (d *DB) Mutate(ctx context.Context, m Mutate) (*api.Response, error)
- func (d *DB) NewTxn(readonly bool) *Txn
- func (d *DB) OnAborted(f func(query interface{}))
- func (d *DB) OnError(f func(err error, val interface{}))
- func (d *DB) Query(ctx context.Context, q Query, objs ...interface{}) error
- func (d *DB) QueryAsync(ctx context.Context, q Query, objs ...interface{}) chan Result
- func (d *DB) Schema() SchemaList
- func (d *DB) Select(vals ...Predicate) Fields
- func (d *DB) SetValue(node DNode, pred Predicate, value interface{}) error
- type DNode
- type Deleter
- type Directive
- type Field
- type FieldList
- type FieldMeta
- type Fields
- type Filter
- type FunctionType
- type GeneratedQuery
- func (q *GeneratedQuery) At(path Predicate, op Operation) *GeneratedQuery
- func (q *GeneratedQuery) Directive(dir Directive) *GeneratedQuery
- func (q *GeneratedQuery) Facets(path Predicate, op Operation) *GeneratedQuery
- func (q *GeneratedQuery) Function(ft FunctionType) *GeneratedQuery
- func (q *GeneratedQuery) GroupBy(path Predicate, onWhich Predicate, op Operation) *GeneratedQuery
- func (q *GeneratedQuery) Language(l Language, strict bool) *GeneratedQuery
- func (q *GeneratedQuery) Process() (string, error)
- func (q *GeneratedQuery) Static() StaticQuery
- func (q *GeneratedQuery) Values(v ...interface{}) *GeneratedQuery
- func (q *GeneratedQuery) Var(name string) *GeneratedQuery
- type Language
- type Mapper
- func (m Mapper) Fields() Fields
- func (m Mapper) GetType() []string
- func (m Mapper) MustSet(child Predicate, all bool, obj DNode) Mapper
- func (m Mapper) Recurse(counter int) int
- func (m Mapper) Set(child Predicate, all bool, obj DNode) Mapper
- func (m Mapper) SetArray(child string, all bool, objs ...DNode) Mapper
- func (m Mapper) SetFunctionValue(variableName string, predicate Predicate)
- func (m Mapper) SetType()
- func (m Mapper) SetUID(uid UID)
- func (m Mapper) UID() UID
- type Mod
- type Mutate
- type MutationQuery
- type MutationType
- type NewList
- type Node
- type Operation
- type OrderType
- type Ordering
- type PaginationType
- type Predicate
- type Querier
- type Queries
- type Query
- type Result
- type Saver
- type SchemaList
- type SingleMutation
- type StaticQuery
- type Txn
- func (t *Txn) Commit(ctx context.Context) error
- func (t *Txn) Discard(ctx context.Context) error
- func (t *Txn) Mutate(ctx context.Context, q Mutate) (*api.Response, error)
- func (t *Txn) MutateAsync(ctx context.Context, q Mutate) chan Result
- func (t *Txn) Query(ctx context.Context, q Query, objs ...interface{}) error
- func (t *Txn) QueryAsync(ctx context.Context, q Query, objs ...interface{}) chan Result
- func (t *Txn) Upsert(ctx context.Context, q Query, mutations ...Mutate) (*api.Response, error)
- type UID
- type Variable
Constants ¶
const ( LanguageEnglish = "en" LanguageGerman = "de" LanguageSwedish = "se" //Same as english. LanguageNone = "" )
A list of possible languages. TODO: Do not make these static constants. Allow arbitrary languages.
Variables ¶
var ErrUID = errors.New("missing UID")
Functions ¶
Types ¶
type AggregateType ¶
type AggregateType string
AggregateType simply refers to all types of aggregation as specified in the query docs.
const ( Val AggregateType = "" Min AggregateType = "min" Sum AggregateType = "sum" Max AggregateType = "max" Avg AggregateType = "avg" Count AggregateType = "count" )
Types of aggregations.
type AsyncQuerier ¶
type AsyncQuerier interface { Querier QueryAsync(context.Context, Query, ...interface{}) chan Result MutateAsync(context.Context, Query) chan Result }
AsyncQuerier is an interface representing a querier that can also perform queries asynchronously.
type Config ¶
type Config struct { //IP for this alpha. IP string //Port for this alpha. Port int //Tls connection. Tls bool //RootCA is the path for the RootCA. RootCA string //NodeCRT is the path for the NodeCRT. NodeCRT string //NodeKey is the path for the NodeKey. NodeKey string //Log queries in stdout along their result. LogQueries bool }
Config represents the configuration for connecting to a Dgraph alpha client.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the root object for using humus. It is used to immediately communicate with Dgraph as well as spawning new transactions. It handles a pool of dgraph clients as well as the active schema for the database.
func Init ¶
func Init(conf *Config, sch SchemaList) *DB
Init is the entrypoint for humus. Init creates a a database object using the connection information as specified in the config. It uses the information specified to set up a grpc connection to the specified destination with or without TLS. It also sets the database schema using a pregenerated schema from humus/gen. Any query or mutation to the database goes through this object.
func (*DB) Mutate ¶
Mutate runs a mutation outside a transaction context. It immediately commits on success.
func (*DB) OnAborted ¶
func (d *DB) OnAborted(f func(query interface{}))
OnAborted sets the function for which to call when returned error is errAborted. The query variable is either a Mutate or a Query and should be handled accordingly.
func (*DB) QueryAsync ¶
QueryAsync runs the query and returns a channel with the result.
func (*DB) Schema ¶
func (d *DB) Schema() SchemaList
Schema returns the active schema for this database.
type DNode ¶
type DNode interface { //Returns the UID of this node. UID() UID //Sets the UID of this node. SetUID(uid UID) //Sets all types of this node. This has to be done at least once. SetType() //Returns the type. GetType() []string //Returns all fields for this node. Fields() Fields //Serializes all the scalar values that are not hidden. It usually returns //a type of *{{.typ}}Scalars. //Values() DNode //Recurse allows you to set types and UIDS for all sub nodes. Recurse(counter int) int }
DNode represents an object that can be safely stored in the database. It includes all necessary fields for automatic generation. This is a big interface but it is automatically satisfied by all values.
type Directive ¶
type Directive string
Directive contains all possible query directives for dgraph. These are applied at the root of the query.
type Field ¶
Field is a recursive data struct which represents a GraphQL query field.
type FieldList ¶
type FieldList []Field
type FieldMeta ¶
type FieldMeta uint16
A meta field for schemas. This simply defines properties surrounding fields such as language etc. This is used in generating the queries.
type Fields ¶
type Fields interface { //Sub allows you to create a sublist of predicates. //If there is an edge on a predicate name, then subbing on that //predicate gets all fields as specified by the fields interfaces. Sub(name Predicate, fields Fields) Fields //Add a field to this list. Add(fi Field) Fields //Get the fields as a slice Get() []Field //Len is the length of the fields. Len() int Select(names ...Predicate) Fields }
FieldList is a list of fields associated with a generated object. These are of global type and should never be modifierType lest the state of the entire application should be changed. Whenever fields are added to this list they are copied. Example usage: var NewFields = CharacterFields.Sub("Character.friends", CharacterFields).Sub("Character.enemies", CharacterFields. Sub("Character.items", ItemFields) This will also ensure fields are copied properly from the global list.
Fields is an interface for all possible type of fields. This includes global fields as well as manually generated fields.
func Select ¶
func Select(sch SchemaList, names ...Predicate) Fields
Select selects a subset of fields and returns a new list keeping all valid meta.
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter represents an object in the query that will be serialized as @filter (function...) It is just a wrapper over a function.
type FunctionType ¶
type FunctionType string
const ( Equals FunctionType = "eq" AllOfText FunctionType = "alloftext" AllOfTerms FunctionType = "allofterms" AnyOfTerms FunctionType = "anyofterms" FunctionUid FunctionType = "uid" Has FunctionType = "has" LessEq FunctionType = "le" Match FunctionType = "match" Less FunctionType = "lt" GreaterEq FunctionType = "ge" Greater FunctionType = "gt" Type FunctionType = "type" )
func (FunctionType) WithFunction ¶
func (f FunctionType) WithFunction(typ string) FunctionType
WithFunction creates an in/equality function with a subfunction. Possible values for typ is count and val. For instance, cases with lt(count(predicate),1). would be Less.WithFunction("count") with two function variables, predicate, 1.
type GeneratedQuery ¶
type GeneratedQuery struct {
// contains filtered or unexported fields
}
GeneratedQuery is the root object of queries that are constructed. It is constructed using a set of Fields that are either autogenerated or manually specified. From its list of modifiers(orderby, pagination etc) it automatically and efficiently builds up a query ready for sending to Dgraph.
func GetByPredicate ¶
func GetByPredicate(pred Predicate, fields Fields, values ...interface{}) *GeneratedQuery
GetByPredicate is shorthand for generating a query for getting nodes from multiple predicate values given by the fields.
func GetByUid ¶
func GetByUid(uid UID, fields Fields) *GeneratedQuery
GetByUid is shorthand for generating a query for getting a node from its uid given by the fields.
func NewQuery ¶
func NewQuery(f Fields) *GeneratedQuery
NewQuery returns a new singular generation query for use in building a single query.
func (*GeneratedQuery) At ¶
func (q *GeneratedQuery) At(path Predicate, op Operation) *GeneratedQuery
At allows you to run modifiers at a path. Modifiers include pagination, sorting, filters among others.
func (*GeneratedQuery) Directive ¶
func (q *GeneratedQuery) Directive(dir Directive) *GeneratedQuery
Directive adds a top level directive.
func (*GeneratedQuery) Facets ¶
func (q *GeneratedQuery) Facets(path Predicate, op Operation) *GeneratedQuery
Facets sets @facets for the edge specified by path along with all values as specified by op. This can be used to fetch facets, store facets in query variables or something in that manner. For instance, generating a line in the fashion of @facets(value as friendsSince) will store the facet value 'friendsSince' into the value variable 'value'.
func (*GeneratedQuery) Function ¶
func (q *GeneratedQuery) Function(ft FunctionType) *GeneratedQuery
Function sets the function type for this function. It is used alongside variables. variables are automatically mapped to GraphQL variables as a way of avoiding SQL injections.
func (*GeneratedQuery) GroupBy ¶
func (q *GeneratedQuery) GroupBy(path Predicate, onWhich Predicate, op Operation) *GeneratedQuery
GroupBy allows you to groupBy at a leaf field. Using op specify a list of variables and operations to be written as aggregation at this level. onWhich specifies what predicate to actually group on.
func (*GeneratedQuery) Language ¶
func (q *GeneratedQuery) Language(l Language, strict bool) *GeneratedQuery
Language sets the language for the query to apply to all fields. If strict do not allow untagged language.
func (*GeneratedQuery) Process ¶
func (q *GeneratedQuery) Process() (string, error)
func (*GeneratedQuery) Static ¶
func (q *GeneratedQuery) Static() StaticQuery
Static create a static query from the generated version. Since this is performed at init, panic if the query creation does not work.
func (*GeneratedQuery) Values ¶
func (q *GeneratedQuery) Values(v ...interface{}) *GeneratedQuery
Values simple performs the same as Value but for a variadic number of arguments.
func (*GeneratedQuery) Var ¶
func (q *GeneratedQuery) Var(name string) *GeneratedQuery
Var sets q as a var query with the variable name name. if name is empty it is just a basic var query.
type Language ¶
type Language string
Language represents a language that sets relevant queries to the language as specified.
type Mapper ¶
type Mapper map[string]interface{}
Mapper allows you to set subrelations manually.
func NewMapper ¶
NewMapper returns a map as a Dnode. This is useful for building up custom structures. For completely custom mutations see CreateCustomMutation. Types are not mandatory but should be supplied for new nodes.
func (Mapper) Set ¶
Set sets a singular regulation, i.e. 1-1. If all uses saver interface or the entire object. Otherwise, only uid is used.
func (Mapper) SetFunctionValue ¶
SetFunctionValue is used in relation to upsert. For instance, storing uid in "result" variable. This will set the value on the edge predicate to the value. This is a simple case and for more complicated queries manually edit the Mapper to look correct. If predicate is "uid" do not set it as a child relation.
type Mod ¶
type Mod interface { /* Paginate creates a pagination at this level given the pagination type and the value. */ Paginate(t PaginationType, value int) bool /* Filter creates a filter at this level given a function type and a list of variables with the same syntax as a function. */ Filter(t FunctionType, variables ...interface{}) bool /* Sort applies a sorting at this level. */ Sort(t OrderType, p Predicate) bool /* Aggregate sets an aggregation at this level. */ Aggregate(t AggregateType, v string, alias string) bool /* Count sets a count variable at this level, e.g. result : count(uid) given a "uid" as predicate and "result" as alias. */ Count(p Predicate, alias string) bool /* Variable sets a variable at this level. It either generates a value variable or an alias variable. If name is omitted so is the prefix for the variable. This can be useful for setting facet variables where name is omitted. */ Variable(name string, value string, isAlias bool) bool }
Mod is the core for applying operations at certain predicate levels. There are two kind of 'mods', those which exist at root/edge level and those at field level. Paginate, Filter, Sort exists at root/edge level with the remaining existing at field level. This is an important distinction For Paginate, a path of "" applies the pagination at the top level (root) and given a single predicate P it applies it on the edge. For Variable, a path of "" applies the variable at the root field level, at the top level node.
type Mutate ¶
type Mutate interface { Type() MutationType Cond() string // contains filtered or unexported methods }
Mutate is the interface satisfied by all objects used in sending a json to the database. Any mutation sent to the database satisfies this interface.
func CreateCustomMutation ¶
func CreateCustomMutation(obj interface{}, typ MutationType) Mutate
CreateCustomMutation allows you to create a mutation from an interface and not a DNode. This is useful alongside custom queries to set values, especially working with facets.
type MutationQuery ¶
type MutationQuery struct { Values []DNode Condition string MutationType MutationType }
func CreateMutations ¶
func CreateMutations(typ MutationType, muts ...DNode) *MutationQuery
CreateMutations creates a list of mutations from a variadic list of Dnodes.
func (*MutationQuery) Cond ¶
func (m *MutationQuery) Cond() string
func (*MutationQuery) SetCondition ¶
func (m *MutationQuery) SetCondition(c string) *MutationQuery
func (*MutationQuery) Type ¶
func (m *MutationQuery) Type() MutationType
type MutationType ¶
type MutationType string
MutationType defines whether a mutation sets or deletes values.
const ( MutateDelete MutationType = "delete" MutateSet MutationType = "set" )
type NewList ¶
type NewList FieldList
NewList simply represents a list of fields where no copying is needed.
type Node ¶
type Node struct { Uid UID `json:"uid,omitempty" predicate:"uid,omitempty"` Type []string `json:"dgraph.type,omitempty" predicate:"dgraph.type,omitempty"` }
The common node type that is inherited. This differs from the DNode which is an interface while node is an embedded struct containing basic dgraph properties.
type Operation ¶
type Operation func(m Mod)
Operation is a closure callback given a mod. Any operations called on this applies the given operation at the path.
type PaginationType ¶
type PaginationType string
PaginationType simply refers to a type of pagination.
const ( CountFirst PaginationType = "first" CountOffset PaginationType = "offset" CountAfter PaginationType = "after" )
type Querier ¶
type Querier interface { //Query queries the database with a variable amount of interfaces to deserialize into. //That is, if you are performing two queries q and q1 you are expected to supply two values. Query(context.Context, Query, ...interface{}) error //mutate mutates the query and returns the response. Mutate(context.Context, Query) (*api.Response, error) //Discard the transaction. This is done automatically in DB but not in Txn. Discard(context.Context) error //Commit is the same as above except it commits the transaction. Commit(context.Context) error }
Querier is an abstraction over DB/TXN. Also allows for testing.
type Queries ¶
type Queries struct {
// contains filtered or unexported fields
}
Queries represents multiple queries at once.
func NewQueries ¶
func NewQueries() *Queries
NewQueries returns a QueryList used for building multiple queries at once.
func (*Queries) NewQuery ¶
func (q *Queries) NewQuery(f Fields) *GeneratedQuery
type Query ¶
type Query interface { //Process the query type in order to send to the database. Process() (string, error) // contains filtered or unexported methods }
Query is the humus interface for a query. GeneratedQuery, Queries, StaticQuery all satisfy this interface and is used in generating all necessary information to send the query to the database.
type SingleMutation ¶
type SingleMutation struct { Object DNode MutationType MutationType //Used for upsert. Condition string }
SingleMutation represents just that, one object mutated. interface{} is used over DNode as the structure of a mutation might change so a map[string]interface{} is needed for certain mutations.
func CreateMutation ¶
func CreateMutation(obj DNode, typ MutationType) SingleMutation
CreateMutation creates a mutation object from the DNode. This can be used immediately as a value for Mutation. A simple syntax would be GetDB().Mutate(ctx, CreateMutation(node, MutateSet)) where node represents an arbitrary Node.
func (SingleMutation) Cond ¶
func (s SingleMutation) Cond() string
func (SingleMutation) Type ¶
func (s SingleMutation) Type() MutationType
func (SingleMutation) WithCond ¶
func (s SingleMutation) WithCond(cond string) SingleMutation
type StaticQuery ¶
type StaticQuery struct { Query string // contains filtered or unexported fields }
StaticQuery represents a static query.
func NewStaticQuery ¶
func NewStaticQuery(query string) StaticQuery
NewStaticQuery creates a formatted query. Use fmt.Sprintf as well as SetVar to supply parameters.
func (StaticQuery) Process ¶
func (s StaticQuery) Process() (string, error)
Process the query in order to send to DGraph.
func (StaticQuery) QueryWithVars ¶
func (s StaticQuery) QueryWithVars(vars map[string]string) StaticQuery
func (*StaticQuery) SetVar ¶
func (s *StaticQuery) SetVar(key string, val interface{}) *StaticQuery
SetVar sets the variable in the GraphQL query map. Since it is a static query the type is expected to be supplied in the string part of the query and not here.
type Txn ¶
type Txn struct { //Allows for safe storage in Queries. sync.Mutex //All queries performed by this transaction. Queries []interface{} // contains filtered or unexported fields }
Txn is an abstraction over a dgraph transaction. In order to perform multiple queries & mutations use this. TODO: Should it be thread-safe? TODO: Do not keep a storage of previous queries and reuse GeneratedQuery with sync.Pool?
func (*Txn) Discard ¶
Discard discards the given transaction. Any further queries on this txn results in an error.
func (*Txn) MutateAsync ¶
MutateAsync runs a single mutation asynchronously inside this transaction.
func (*Txn) Query ¶
Query executes the GraphQL+- query. If q is a mutation query the mutation objects are supplied in q and not in objs.
func (*Txn) QueryAsync ¶
QueryAsync runs the query asynchronous. In the result the error is returned.
type UID ¶
type UID string
UID represents the primary UID class used in communication with DGraph. This is used in code generation and for type safety.
func UIDVariable ¶
UidFromVariable returns the proper uid mapping for upserts, of the form uid(variable).
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
packrd
You can use the "packr2 clean" command to clean up this, and any other packr generated files.
|
You can use the "packr2 clean" command to clean up this, and any other packr generated files. |
parse
You can use the "packr clean" command to clean up this, and any other packr generated files.
|
You can use the "packr clean" command to clean up this, and any other packr generated files. |