github.com/cayleygraph/cayley@v0.7.7/docs/gizmoapi.md (about)

     1  # Gizmo API
     2  
     3  ![Autogenerated file](https://img.shields.io/badge/file-generated-orange.svg)
     4  
     5  ## The `graph` object
     6  
     7  Name: `graph`, Alias: `g`
     8  
     9  This is the only special object in the environment, generates the query objects. Under the hood, they're simple objects that get compiled to a Go iterator tree when executed.
    10  
    11  ### `graph.addDefaultNamespaces()`
    12  
    13  AddDefaultNamespaces register all default namespaces for automatic IRI resolution.
    14  
    15  ### `graph.addNamespace(pref, ns)`
    16  
    17  AddNamespace associates prefix with a given IRI namespace.
    18  
    19  ### `graph.emit(*)`
    20  
    21  Emit adds data programmatically to the JSON result list. Can be any JSON type.
    22  
    23  ```javascript
    24  g.emit({ name: "bob" }); // push {"name":"bob"} as a result
    25  ```
    26  
    27  ### `graph.loadNamespaces()`
    28  
    29  LoadNamespaces loads all namespaces saved to graph.
    30  
    31  ### `graph.M()`
    32  
    33  M is a shorthand for Morphism.
    34  
    35  ### `graph.Morphism()`
    36  
    37  Morphism creates a morphism path object. Unqueryable on it's own, defines one end of the path. Saving these to variables with
    38  
    39  ```javascript
    40  var shorterPath = graph
    41    .Morphism()
    42    .out("foo")
    43    .out("bar");
    44  ```
    45  
    46  is the common use case. See also: path.follow\(\), path.followR\(\).
    47  
    48  ### `graph.IRI(s)`
    49  
    50  Uri creates an IRI values from a given string.
    51  
    52  ### `graph.V(*)`
    53  
    54  V is a shorthand for Vertex.
    55  
    56  ### `graph.Vertex([nodeId],[nodeId]...)`
    57  
    58  Vertex starts a query path at the given vertex/vertices. No ids means "all vertices".
    59  
    60  Arguments:
    61  
    62  * `nodeId` \(Optional\): A string or list of strings representing the starting vertices.
    63  
    64  Returns: Path object
    65  
    66  ## Path object
    67  
    68  Both `.Morphism()` and `.Vertex()` create path objects, which provide the following traversal methods. Note that `.Vertex()` returns a query object, which is a subclass of path object.
    69  
    70  For these examples, suppose we have the following graph:
    71  
    72  ```text
    73  +-------+                        +------+
    74  | alice |-----                 ->| fred |<--
    75  +-------+     \---->+-------+-/  +------+   \-+-------+
    76                ----->| #bob# |       |         |*emily*|
    77  +---------+--/  --->+-------+       |         +-------+
    78  | charlie |    /                    v
    79  +---------+   /                  +--------+
    80    \---    +--------+             |*#greg#*|
    81        \-->| #dani# |------------>+--------+
    82            +--------+
    83  ```
    84  
    85  Where every link is a `<follows>` relationship, and the nodes with an extra `#` in the name have an extra `<status>` link. As in,
    86  
    87  ```text
    88  <dani> -- <status> --> "cool_person"
    89  ```
    90  
    91  Perhaps these are the influencers in our community. So too are extra `*`s in the name -- these are our smart people, according to the `<smart_graph>` label, eg, the quad:
    92  
    93  ```text
    94  <greg> <status> "smart_person" <smart_graph> .
    95  ```
    96  
    97  ### `path.all()`
    98  
    99  All executes the query and adds the results, with all tags, as a string-to-string \(tag to node\) map in the output set, one for each path that a traversal could take.
   100  
   101  ### `path.and(path)`
   102  
   103  And is an alias for Intersect.
   104  
   105  ### `path.as(tags)`
   106  
   107  As is an alias for Tag.
   108  
   109  ### `path.back([tag])`
   110  
   111  Back returns current path to a set of nodes on a given tag, preserving all constraints.
   112  
   113  If still valid, a path will now consider their vertex to be the same one as the previously tagged one, with the added constraint that it was valid all the way here. Useful for traversing back in queries and taking another route for things that have matched so far.
   114  
   115  Arguments:
   116  
   117  * `tag`: A previous tag in the query to jump back to.
   118  
   119  Example:
   120  
   121  ```javascript
   122  // Start from all nodes, save them into start, follow any status links,
   123  // jump back to the starting node, and find who follows them. Return the result.
   124  // Results are:
   125  //   {"id": "<alice>", "start": "<bob>"},
   126  //   {"id": "<charlie>", "start": "<bob>"},
   127  //   {"id": "<charlie>", "start": "<dani>"},
   128  //   {"id": "<dani>", "start": "<bob>"},
   129  //   {"id": "<dani>", "start": "<greg>"},
   130  //   {"id": "<dani>", "start": "<greg>"},
   131  //   {"id": "<fred>", "start": "<greg>"},
   132  //   {"id": "<fred>", "start": "<greg>"}
   133  g.V()
   134    .Tag("start")
   135    .out("<status>")
   136    .back("start")
   137    .in("<follows>")
   138    .all();
   139  ```
   140  
   141  ### `path.both([predicatePath], [tags])`
   142  
   143  Both follow the predicate in either direction. Same as Out or In.
   144  
   145  Example:
   146  
   147  ```javascript
   148  // Find all followers/followees of fred. Returns bob, emily and greg
   149  g.V("<fred>")
   150    .both("<follows>")
   151    .all();
   152  ```
   153  
   154  ### `path.count()`
   155  
   156  Count returns a number of results and returns it as a value.
   157  
   158  Example:
   159  
   160  ```javascript
   161  // Save count as a variable
   162  var n = g.V().count();
   163  // Send it as a query result
   164  g.emit(n);
   165  ```
   166  
   167  ### `path.difference(path)`
   168  
   169  Difference is an alias for Except.
   170  
   171  ### `path.except(path)`
   172  
   173  Except removes all paths which match query from current path.
   174  
   175  In a set-theoretic sense, this is \(A - B\). While `g.V().except(path)` to achieve `U - B = !B` is supported, it's often very slow. Example:
   176  
   177  ```javascript
   178  var cFollows = g.V("<charlie>").out("<follows>");
   179  var dFollows = g.V("<dani>").out("<follows>");
   180  // People followed by both charlie (bob and dani) and dani (bob and greg) -- returns bob.
   181  cFollows.except(dFollows).all(); // The set (dani) -- what charlie follows that dani does not also follow.
   182  // Equivalently, g.V("<charlie>").out("<follows>").except(g.V("<dani>").out("<follows>")).all()
   183  ```
   184  
   185  ### `path.filter(args)`
   186  
   187  Filter applies constraints to a set of nodes. Can be used to filter values by range or match strings.
   188  
   189  #### `path.filter(regex(expression, includeIRIs))`
   190  
   191  Filters by match a regular expression \([syntax](https://github.com/google/re2/wiki/Syntax)\). By default works only on literals unless includeEntities is set to `true`.
   192  
   193  ### `path.follow(path)`
   194  
   195  Follow is the way to use a path prepared with Morphism. Applies the path chain on the morphism object to the current path.
   196  
   197  Starts as if at the g.M\(\) and follows through the morphism path.
   198  
   199  Example:
   200  
   201  ```javascript
   202  var friendOfFriend = g
   203    .Morphism()
   204    .out("<follows>")
   205    .out("<follows>");
   206  // Returns the followed people of who charlie follows -- a simplistic "friend of my friend"
   207  // and whether or not they have a "cool" status. Potential for recommending followers abounds.
   208  // Returns bob and greg
   209  g.V("<charlie>")
   210    .follow(friendOfFriend)
   211    .has("<status>", "cool_person")
   212    .all();
   213  ```
   214  
   215  ### `path.followR(path)`
   216  
   217  FollowR is the same as Follow but follows the chain in the reverse direction. Flips "In" and "Out" where appropriate, the net result being a virtual predicate followed in the reverse direction.
   218  
   219  Starts at the end of the morphism and follows it backwards \(with appropriate flipped directions\) to the g.M\(\) location.
   220  
   221  Example:
   222  
   223  ```javascript
   224  var friendOfFriend = g
   225    .Morphism()
   226    .out("<follows>")
   227    .out("<follows>");
   228  // Returns the third-tier of influencers -- people who follow people who follow the cool people.
   229  // Returns charlie (from bob), charlie (from greg), bob and emily
   230  g.V()
   231    .has("<status>", "cool_person")
   232    .followR(friendOfFriend)
   233    .all();
   234  ```
   235  
   236  ### `path.followRecursive(*)`
   237  
   238  FollowRecursive is the same as Follow but follows the chain recursively.
   239  
   240  Starts as if at the g.M\(\) and follows through the morphism path multiple times, returning all nodes encountered.
   241  
   242  Example:
   243  
   244  ```javascript
   245  var friend = g.Morphism().out("<follows>");
   246  // Returns all people in Charlie's network.
   247  // Returns bob and dani (from charlie), fred (from bob) and greg (from dani).
   248  g.V("<charlie>")
   249    .followRecursive(friend)
   250    .all();
   251  ```
   252  
   253  ### `path.forEach(callback) or (limit, callback)`
   254  
   255  ForEach calls callback\(data\) for each result, where data is the tag-to-string map as in All case.
   256  
   257  Arguments:
   258  
   259  * `limit` \(Optional\): An integer value on the first `limit` paths to process.
   260  * `callback`: A javascript function of the form `function(data)`
   261  
   262  Example:
   263  
   264  ```javascript
   265  // Simulate query.all().all()
   266  graph.V("<alice>").ForEach(function(d) {
   267    g.emit(d);
   268  });
   269  ```
   270  
   271  ### `path.getLimit(limit)`
   272  
   273  GetLimit is the same as All, but limited to the first N unique nodes at the end of the path, and each of their possible traversals.
   274  
   275  ### `path.has(predicate, object)`
   276  
   277  Has filters all paths which are, at this point, on the subject for the given predicate and object, but do not follow the path, merely filter the possible paths.
   278  
   279  Usually useful for starting with all nodes, or limiting to a subset depending on some predicate/value pair.
   280  
   281  Arguments:
   282  
   283  * `predicate`: A string for a predicate node.
   284  * `object`: A string for a object node or a set of filters to find it.
   285  
   286  Example:
   287  
   288  ```javascript
   289  // Start from all nodes that follow bob -- results in alice, charlie and dani
   290  g.V()
   291    .has("<follows>", "<bob>")
   292    .all();
   293  // People charlie follows who then follow fred. Results in bob.
   294  g.V("<charlie>")
   295    .out("<follows>")
   296    .has("<follows>", "<fred>")
   297    .all();
   298  // People with friends who have names sorting lower then "f".
   299  g.V()
   300    .has("<follows>", gt("<f>"))
   301    .all();
   302  ```
   303  
   304  ### `path.hasR(*)`
   305  
   306  HasR is the same as Has, but sets constraint in reverse direction.
   307  
   308  ### `path.in([predicatePath], [tags])`
   309  
   310  In is inverse of Out. Starting with the nodes in `path` on the object, follow the quads with predicates defined by `predicatePath` to their subjects.
   311  
   312  Arguments:
   313  
   314  * `predicatePath` \(Optional\): One of:
   315    * null or undefined: All predicates pointing into this node
   316    * a string: The predicate name to follow into this node
   317    * a list of strings: The predicates to follow into this node
   318    * a query path object: The target of which is a set of predicates to follow.
   319  * `tags` \(Optional\): One of:
   320    * null or undefined: No tags
   321    * a string: A single tag to add the predicate used to the output set.
   322    * a list of strings: Multiple tags to use as keys to save the predicate used to the output set.
   323  
   324  Example:
   325  
   326  ```javascript
   327  // Find the cool people, bob, dani and greg
   328  g.V("cool_person")
   329    .in("<status>")
   330    .all();
   331  // Find who follows bob, in this case, alice, charlie, and dani
   332  g.V("<bob>")
   333    .in("<follows>")
   334    .all();
   335  // Find who follows the people emily follows, namely, bob and emily
   336  g.V("<emily>")
   337    .out("<follows>")
   338    .in("<follows>")
   339    .all();
   340  ```
   341  
   342  ### `path.inPredicates()`
   343  
   344  InPredicates gets the list of predicates that are pointing in to a node.
   345  
   346  Example:
   347  
   348  ```javascript
   349  // bob only has "<follows>" predicates pointing inward
   350  // returns "<follows>"
   351  g.V("<bob>")
   352    .inPredicates()
   353    .all();
   354  ```
   355  
   356  ### `path.intersect(path)`
   357  
   358  Intersect filters all paths by the result of another query path.
   359  
   360  This is essentially a join where, at the stage of each path, a node is shared. Example:
   361  
   362  ```javascript
   363  var cFollows = g.V("<charlie>").out("<follows>");
   364  var dFollows = g.V("<dani>").out("<follows>");
   365  // People followed by both charlie (bob and dani) and dani (bob and greg) -- returns bob.
   366  cFollows.intersect(dFollows).all();
   367  // Equivalently, g.V("<charlie>").out("<follows>").And(g.V("<dani>").out("<follows>")).all()
   368  ```
   369  
   370  ### `path.is(node, [node..])`
   371  
   372  Filter all paths to ones which, at this point, are on the given node.
   373  
   374  Arguments:
   375  
   376  * `node`: A string for a node. Can be repeated or a list of strings.
   377  
   378  Example:
   379  
   380  ```javascript
   381  // Starting from all nodes in the graph, find the paths that follow bob.
   382  // Results in three paths for bob (from alice, charlie and dani).all()
   383  g.V()
   384    .out("<follows>")
   385    .is("<bob>")
   386    .all();
   387  ```
   388  
   389  ### `path.labelContext([labelPath], [tags])`
   390  
   391  LabelContext sets \(or removes\) the subgraph context to consider in the following traversals. Affects all In\(\), Out\(\), and Both\(\) calls that follow it. The default LabelContext is null \(all subgraphs\).
   392  
   393  Arguments:
   394  
   395  * `predicatePath` \(Optional\): One of:
   396    * null or undefined: In future traversals, consider all edges, regardless of subgraph.
   397    * a string: The name of the subgraph to restrict traversals to.
   398    * a list of strings: A set of subgraphs to restrict traversals to.
   399    * a query path object: The target of which is a set of subgraphs.
   400  * `tags` \(Optional\): One of:
   401    * null or undefined: No tags
   402    * a string: A single tag to add the last traversed label to the output set.
   403    * a list of strings: Multiple tags to use as keys to save the label used to the output set.
   404  
   405  Example:
   406  
   407  ```javascript
   408  // Find the status of people Dani follows
   409  g.V("<dani>")
   410    .out("<follows>")
   411    .out("<status>")
   412    .all();
   413  // Find only the statuses provided by the smart_graph
   414  g.V("<dani>")
   415    .out("<follows>")
   416    .labelContext("<smart_graph>")
   417    .out("<status>")
   418    .all();
   419  // Find all people followed by people with statuses in the smart_graph.
   420  g.V()
   421    .labelContext("<smart_graph>")
   422    .in("<status>")
   423    .labelContext(null)
   424    .in("<follows>")
   425    .all();
   426  ```
   427  
   428  ### `path.labels()`
   429  
   430  Labels gets the list of inbound and outbound quad labels
   431  
   432  ### `path.limit(limit)`
   433  
   434  Limit limits a number of nodes for current path.
   435  
   436  Arguments:
   437  
   438  * `limit`: A number of nodes to limit results to.
   439  
   440  Example:
   441  
   442  ```javascript
   443  // Start from all nodes that follow bob, and limit them to 2 nodes -- results in alice and charlie
   444  g.V()
   445    .has("<follows>", "<bob>")
   446    .limit(2)
   447    .all();
   448  ```
   449  
   450  ### `path.map(*)`
   451  
   452  Map is a alias for ForEach.
   453  
   454  ### `path.or(path)`
   455  
   456  Or is an alias for Union.
   457  
   458  ### `path.out([predicatePath], [tags])`
   459  
   460  Out is the work-a-day way to get between nodes, in the forward direction. Starting with the nodes in `path` on the subject, follow the quads with predicates defined by `predicatePath` to their objects.
   461  
   462  Arguments:
   463  
   464  * `predicatePath` \(Optional\): One of:
   465    * null or undefined: All predicates pointing out from this node
   466    * a string: The predicate name to follow out from this node
   467    * a list of strings: The predicates to follow out from this node
   468    * a query path object: The target of which is a set of predicates to follow.
   469  * `tags` \(Optional\): One of:
   470    * null or undefined: No tags
   471    * a string: A single tag to add the predicate used to the output set.
   472    * a list of strings: Multiple tags to use as keys to save the predicate used to the output set.
   473  
   474  Example:
   475  
   476  ```javascript
   477  // The working set of this is bob and dani
   478  g.V("<charlie>")
   479    .out("<follows>")
   480    .all();
   481  // The working set of this is fred, as alice follows bob and bob follows fred.
   482  g.V("<alice>")
   483    .out("<follows>")
   484    .out("<follows>")
   485    .all();
   486  // Finds all things dani points at. Result is bob, greg and cool_person
   487  g.V("<dani>")
   488    .out()
   489    .all();
   490  // Finds all things dani points at on the status linkage.
   491  // Result is bob, greg and cool_person
   492  g.V("<dani>")
   493    .out(["<follows>", "<status>"])
   494    .all();
   495  // Finds all things dani points at on the status linkage, given from a separate query path.
   496  // Result is {"id": "cool_person", "pred": "<status>"}
   497  g.V("<dani>")
   498    .out(g.V("<status>"), "pred")
   499    .all();
   500  ```
   501  
   502  ### `path.outPredicates()`
   503  
   504  OutPredicates gets the list of predicates that are pointing out from a node.
   505  
   506  Example:
   507  
   508  ```javascript
   509  // bob has "<follows>" and "<status>" edges pointing outwards
   510  // returns "<follows>", "<status>"
   511  g.V("<bob>")
   512    .outPredicates()
   513    .all();
   514  ```
   515  
   516  ### `path.save(predicate, tag)`
   517  
   518  Save saves the object of all quads with predicate into tag, without traversal.
   519  
   520  Arguments:
   521  
   522  * `predicate`: A string for a predicate node.
   523  * `tag`: A string for a tag key to store the object node.
   524  
   525  Example:
   526  
   527  ```javascript
   528  // Start from dani and bob and save who they follow into "target"
   529  // Returns:
   530  //   {"id" : "<bob>", "target": "<fred>" },
   531  //   {"id" : "<dani>", "target": "<bob>" },
   532  //   {"id" : "<dani>", "target": "<greg>" }
   533  g.V("<dani>", "<bob>")
   534    .save("<follows>", "target")
   535    .all();
   536  ```
   537  
   538  ### `path.saveInPredicates(tag)`
   539  
   540  SaveInPredicates tags the list of predicates that are pointing in to a node.
   541  
   542  Example:
   543  
   544  ```javascript
   545  // bob only has "<follows>" predicates pointing inward
   546  // returns {"id":"<bob>", "pred":"<follows>"}
   547  g.V("<bob>")
   548    .saveInPredicates("pred")
   549    .all();
   550  ```
   551  
   552  ### `path.saveOpt(*)`
   553  
   554  SaveOpt is the same as Save, but returns empty tags if predicate does not exists.
   555  
   556  ### `path.saveOptR(*)`
   557  
   558  SaveOptR is the same as SaveOpt, but tags values via reverse predicate.
   559  
   560  ### `path.saveOutPredicates(tag)`
   561  
   562  SaveOutPredicates tags the list of predicates that are pointing out from a node.
   563  
   564  Example:
   565  
   566  ```javascript
   567  // bob has "<follows>" and "<status>" edges pointing outwards
   568  // returns {"id":"<bob>", "pred":"<follows>"}
   569  g.V("<bob>")
   570    .saveInPredicates("pred")
   571    .all();
   572  ```
   573  
   574  ### `path.saveR(*)`
   575  
   576  SaveR is the same as Save, but tags values via reverse predicate.
   577  
   578  ### `path.skip(offset)`
   579  
   580  Skip skips a number of nodes for current path.
   581  
   582  Arguments:
   583  
   584  * `offset`: A number of nodes to skip.
   585  
   586  Example:
   587  
   588  ```javascript
   589  // Start from all nodes that follow bob, and skip 2 nodes -- results in dani
   590  g.V()
   591    .has("<follows>", "<bob>")
   592    .skip(2)
   593    .all();
   594  ```
   595  
   596  ### `path.tag(tags)`
   597  
   598  Tag saves a list of nodes to a given tag.
   599  
   600  In order to save your work or learn more about how a path got to the end, we have tags. The simplest thing to do is to add a tag anywhere you'd like to put each node in the result set.
   601  
   602  Arguments:
   603  
   604  * `tag`: A string or list of strings to act as a result key. The value for tag was the vertex the path was on at the time it reached "Tag"
   605  
   606    Example:
   607  
   608  ```javascript
   609  // Start from all nodes, save them into start, follow any status links, and return the result.
   610  // Results are:
   611  //   {"id": "cool_person", "start": "<bob>"},
   612  //   {"id": "cool_person", "start": "<dani>"},
   613  //   {"id": "cool_person", "start": "<greg>"},
   614  //   {"id": "smart_person", "start": "<emily>"},
   615  //   {"id": "smart_person", "start": "<greg>"}
   616  g.V()
   617    .tag("start")
   618    .out("<status>")
   619    .all();
   620  ```
   621  
   622  ### `path.tagArray(*)`
   623  
   624  TagArray is the same as ToArray, but instead of a list of top-level nodes, returns an Array of tag-to-string dictionaries, much as All would, except inside the JS environment.
   625  
   626  Example:
   627  
   628  ```javascript
   629  // bobTags contains an Array of followers of bob (alice, charlie, dani).
   630  var bobTags = g
   631    .V("<bob>")
   632    .tag("name")
   633    .in("<follows>")
   634    .tagArray();
   635  // nameValue should be the string "<bob>"
   636  var nameValue = bobTags[0]["name"];
   637  ```
   638  
   639  ### `path.tagValue()`
   640  
   641  TagValue is the same as TagArray, but limited to one result node. Returns a tag-to-string map.
   642  
   643  ### `path.toArray(*)`
   644  
   645  ToArray executes a query and returns the results at the end of the query path as an JS array.
   646  
   647  Example:
   648  
   649  ```javascript
   650  // bobFollowers contains an Array of followers of bob (alice, charlie, dani).
   651  var bobFollowers = g
   652    .V("<bob>")
   653    .in("<follows>")
   654    .toArray();
   655  ```
   656  
   657  ### `path.toValue()`
   658  
   659  ToValue is the same as ToArray, but limited to one result node.
   660  
   661  ### `path.union(path)`
   662  
   663  Union returns the combined paths of the two queries.
   664  
   665  Notice that it's per-path, not per-node. Once again, if multiple paths reach the same destination, they might have had different ways of getting there \(and different tags\). See also: `path.Tag()`
   666  
   667  Example:
   668  
   669  ```javascript
   670  var cFollows = g.V("<charlie>").out("<follows>");
   671  var dFollows = g.V("<dani>").out("<follows>");
   672  // People followed by both charlie (bob and dani) and dani (bob and greg) -- returns bob (from charlie), dani, bob (from dani), and greg.
   673  cFollows.union(dFollows).all();
   674  ```
   675  
   676  ### `path.unique()`
   677  
   678  Unique removes duplicate values from the path.
   679  
   680  ### `path.order()`
   681  
   682  Order returns values from the path in ascending order.