@graffy/link 0.15.12-alpha.2 → 0.15.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/Readme.md +38 -0
  2. package/index.cjs +14 -24
  3. package/index.mjs +15 -25
  4. package/package.json +2 -2
package/Readme.md CHANGED
@@ -2,4 +2,42 @@
2
2
 
3
3
  Graffy module for constructing links using an intuitive, declarative notation.
4
4
 
5
+ ## Example
6
+
7
+ Consider a blog data model, with posts and users. Every post has an `authorId`
8
+ property, and we would like a property `author` on posts (which link to the
9
+ author), and a property `posts` on users, which link to an array of posts
10
+ authored by that user.
11
+
12
+ This is accomplished using:
13
+
14
+ ```js
15
+ import Graffy from '@graffy/core';
16
+ import link from '@graffy/link';
17
+
18
+ const store = new Graffy();
19
+ store.use(link({
20
+ 'posts.$key.author': ['users', '$$posts.$key.authorId'],
21
+ 'users.$key.posts': ['posts', { $all: true, authorId: '$$users.$key.id' }]
22
+ }));
23
+
24
+ // Add downstream providers to the store
25
+ ```
26
+
27
+ ## Link definitions
28
+
29
+ The link module must be initialized with an object containing a set of link
30
+ definitions.
31
+
32
+ - Each link definition consists of two paths, the _source_ and the _target_.
33
+ The source path must be a dot-separated string. The target path must be an
34
+ array of strings or argument objects.
35
+ - The source may contain _wildcard_ segments starting with `$` and followed by
36
+ an arbitrary name. Wildcards that were _defined_ in a source path may be
37
+ included in the target path, where they are replaced with matched keys from
38
+ the query.
39
+ - The target may include _lookup_ strings starting with `$$` and followed by
40
+ dot-separated string paths (possibly including wildcards). These are replaced
41
+ with values returned by downstream providers.
42
+
5
43
  See [Graffy documentation](https://aravindet.github.io/graffy/) for more.
package/index.cjs CHANGED
@@ -23,38 +23,28 @@ function linkGraph(rootGraph, defs) {
23
23
  for (const { path, def } of defs)
24
24
  linkGraphDef(rootGraph, path, def);
25
25
  return rootGraph;
26
- function linkGraphDef(graph, path, def, vars = {}, version = 0) {
26
+ function linkGraphDef(graph, path, def, version = 0) {
27
27
  const [key, ...rest] = path;
28
28
  if (rest.length === 0) {
29
29
  const ref = makeRef(def);
30
30
  const [range] = common.splitRef(def);
31
- const node = { key, path: common.encodePath(ref), version };
31
+ const node2 = { key, path: common.encodePath(ref), version };
32
32
  if (range)
33
- node.prefix = true;
34
- common.merge(graph, [node]);
33
+ node2.prefix = true;
34
+ common.merge(graph, [node2]);
35
35
  return;
36
36
  }
37
- if (key[0] !== "$") {
38
- let node = graph[common.findFirst(graph, key)];
39
- if (!node || node.key !== key || node.end) {
40
- node = { key, version, value: 1 };
41
- common.merge(graph, [node]);
42
- delete node.value;
43
- node.children = [];
44
- }
45
- if (!node.children) {
46
- throw Error("linkGraph.unexpected_leaf " + key);
47
- }
48
- linkGraphDef(node.children, rest, def, vars, node.version);
49
- } else {
50
- for (const node of graph) {
51
- if (!common.isBranch(node))
52
- continue;
53
- linkGraphDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
54
- [key.slice(1)]: node.key
55
- }), node.version);
56
- }
37
+ let node = graph[common.findFirst(graph, key)];
38
+ if (!node || node.key !== key || node.end) {
39
+ node = { key, version, value: 1 };
40
+ common.merge(graph, [node]);
41
+ delete node.value;
42
+ node.children = [];
43
+ }
44
+ if (!node.children) {
45
+ throw Error("linkGraph.unexpected_leaf " + key);
57
46
  }
47
+ linkGraphDef(node.children, rest, def, node.version);
58
48
  }
59
49
  function makeRef(def) {
60
50
  function replacePlaceholders(key) {
package/index.mjs CHANGED
@@ -17,43 +17,33 @@ var __spreadValues = (a, b) => {
17
17
  return a;
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- import { splitRef, encodePath, merge, findFirst, isBranch, unwrap, add, decodeArgs, wrapValue, wrap } from "@graffy/common";
20
+ import { splitRef, encodePath, merge, findFirst, unwrap, add, decodeArgs, isBranch, wrapValue, wrap } from "@graffy/common";
21
21
  function linkGraph(rootGraph, defs) {
22
22
  for (const { path, def } of defs)
23
23
  linkGraphDef(rootGraph, path, def);
24
24
  return rootGraph;
25
- function linkGraphDef(graph, path, def, vars = {}, version = 0) {
25
+ function linkGraphDef(graph, path, def, version = 0) {
26
26
  const [key, ...rest] = path;
27
27
  if (rest.length === 0) {
28
28
  const ref = makeRef(def);
29
29
  const [range] = splitRef(def);
30
- const node = { key, path: encodePath(ref), version };
30
+ const node2 = { key, path: encodePath(ref), version };
31
31
  if (range)
32
- node.prefix = true;
33
- merge(graph, [node]);
32
+ node2.prefix = true;
33
+ merge(graph, [node2]);
34
34
  return;
35
35
  }
36
- if (key[0] !== "$") {
37
- let node = graph[findFirst(graph, key)];
38
- if (!node || node.key !== key || node.end) {
39
- node = { key, version, value: 1 };
40
- merge(graph, [node]);
41
- delete node.value;
42
- node.children = [];
43
- }
44
- if (!node.children) {
45
- throw Error("linkGraph.unexpected_leaf " + key);
46
- }
47
- linkGraphDef(node.children, rest, def, vars, node.version);
48
- } else {
49
- for (const node of graph) {
50
- if (!isBranch(node))
51
- continue;
52
- linkGraphDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
53
- [key.slice(1)]: node.key
54
- }), node.version);
55
- }
36
+ let node = graph[findFirst(graph, key)];
37
+ if (!node || node.key !== key || node.end) {
38
+ node = { key, version, value: 1 };
39
+ merge(graph, [node]);
40
+ delete node.value;
41
+ node.children = [];
42
+ }
43
+ if (!node.children) {
44
+ throw Error("linkGraph.unexpected_leaf " + key);
56
45
  }
46
+ linkGraphDef(node.children, rest, def, node.version);
57
47
  }
58
48
  function makeRef(def) {
59
49
  function replacePlaceholders(key) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/link",
3
3
  "description": "Graffy module for constructing links using an intuitive, declarative notation.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.15.12-alpha.2",
5
+ "version": "0.15.13",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,6 +16,6 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.15.12-alpha.2"
19
+ "@graffy/common": "0.15.13"
20
20
  }
21
21
  }