@yaks/edge 0.0.0-stage → 0.2.36

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 (71) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +325 -2
  3. package/_dist/comp.d.ts +7 -0
  4. package/_dist/comp.d.ts.map +1 -0
  5. package/_dist/eid.d.ts +43 -0
  6. package/_dist/eid.d.ts.map +1 -0
  7. package/_dist/graph.d.ts +10 -0
  8. package/_dist/graph.d.ts.map +1 -0
  9. package/_dist/guard.d.ts +8 -0
  10. package/_dist/guard.d.ts.map +1 -0
  11. package/_dist/keywords.d.ts +13 -0
  12. package/_dist/keywords.d.ts.map +1 -0
  13. package/_dist/mod.d.ts +58 -0
  14. package/_dist/mod.d.ts.map +1 -0
  15. package/_dist/order.d.ts +7 -0
  16. package/_dist/order.d.ts.map +1 -0
  17. package/_dist/plugin.d.ts +23 -0
  18. package/_dist/plugin.d.ts.map +1 -0
  19. package/_dist/relations.d.ts +44 -0
  20. package/_dist/relations.d.ts.map +1 -0
  21. package/_dist/say.d.ts +13 -0
  22. package/_dist/say.d.ts.map +1 -0
  23. package/_dist/sql.d.ts +10 -0
  24. package/_dist/sql.d.ts.map +1 -0
  25. package/_dist/vocab.d.ts +10 -0
  26. package/_dist/vocab.d.ts.map +1 -0
  27. package/_dist/walk.d.ts +16 -0
  28. package/_dist/walk.d.ts.map +1 -0
  29. package/comp.js +25 -0
  30. package/comp.js.map +1 -0
  31. package/comp.ts +26 -0
  32. package/deno.json +1 -0
  33. package/eid.js +66 -0
  34. package/eid.js.map +1 -0
  35. package/eid.ts +109 -0
  36. package/graph.js +18 -0
  37. package/graph.js.map +1 -0
  38. package/graph.ts +23 -0
  39. package/guard.js +71 -0
  40. package/guard.js.map +1 -0
  41. package/guard.ts +75 -0
  42. package/keywords.js +32 -0
  43. package/keywords.js.map +1 -0
  44. package/keywords.ts +32 -0
  45. package/meta/edge.vocab.json +20 -0
  46. package/mod.js +58 -0
  47. package/mod.js.map +1 -0
  48. package/mod.ts +59 -0
  49. package/order.js +59 -0
  50. package/order.js.map +1 -0
  51. package/order.ts +44 -0
  52. package/package.json +28 -4
  53. package/plugin.js +47 -0
  54. package/plugin.js.map +1 -0
  55. package/plugin.ts +42 -0
  56. package/relations.js +80 -0
  57. package/relations.js.map +1 -0
  58. package/relations.ts +92 -0
  59. package/say.js +39 -0
  60. package/say.js.map +1 -0
  61. package/say.ts +41 -0
  62. package/sql.js +58 -0
  63. package/sql.js.map +1 -0
  64. package/sql.ts +68 -0
  65. package/vocab.js +19 -0
  66. package/vocab.js.map +1 -0
  67. package/vocab.json +52 -0
  68. package/vocab.ts +21 -0
  69. package/walk.js +88 -0
  70. package/walk.js.map +1 -0
  71. package/walk.ts +116 -0
package/relations.ts ADDED
@@ -0,0 +1,92 @@
1
+ // The `edge` keywords, interpreted: which components name a relation, what
2
+ // each of them is called, and how each reads from its far end.
3
+ //
4
+ // The set of relations is not a fixed list this package ships. An application
5
+ // declares as many as it has — a blog's `links`, a bookstore's `cites`, a task
6
+ // board's `requires` — and each is an ordinary component declaring `edge`
7
+ // about itself. Everything here reads that declaration off a loaded vocabulary;
8
+ // nothing is hardcoded, so adding a relation means adding one component, not
9
+ // editing this file.
10
+ //
11
+ // A relation has two names, and they may differ. The component name is the
12
+ // component stored on the link entity (`references`); the query name is what a
13
+ // query uses (`referenced`). Declaring `edge: true` makes them identical,
14
+ // which is the common case; declaring a string gives the query name. The link's
15
+ // id is derived from the component name, so the two maps below are not
16
+ // interchangeable.
17
+
18
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
19
+
20
+ /** The component every edge carries: its two ends, and its place in a list. */
21
+ export let EDGE = 'edge'
22
+
23
+ /**
24
+ * Every relation the vocabulary declares, as query name → component name:
25
+ * `{ cites: 'cites', referenced: 'references' }`. Reads the `edge` keyword,
26
+ * so the vocabulary must have been loaded with `edgeKeywords` registered — an
27
+ * unregistered keyword is invisible to the loader.
28
+ */
29
+ export let relations = (v: Vocab): Record<string, string> => {
30
+ let out: Record<string, string> = {}
31
+ for (let tag of v.all) {
32
+ // The keyword is named after the component it is stored beside, so one
33
+ // rule covers them all (@yaks/key's components declare `key`).
34
+ let said = v.comp(tag)?.keywords?.edge
35
+ if (said === true) out[tag] = tag
36
+ else if (typeof said == 'string' && said) out[said] = tag
37
+ }
38
+ return out
39
+ }
40
+
41
+ /**
42
+ * The relations a link here can be kept as: every one {@link relations}
43
+ * finds, or none in a vocabulary without the {@link EDGE} component each link
44
+ * carries. A graph composing @yaks/task but not @yaks/edge declares
45
+ * `contains`, yet holds no link of it, and a read of `edge.from` there is a
46
+ * read of a property it does not have.
47
+ */
48
+ export let linkable = (v: Vocab): Record<string, string> =>
49
+ v.comp(EDGE) ? relations(v) : {}
50
+
51
+ /**
52
+ * The same declarations the other way round, as component name → query name.
53
+ * This is what reads a stored link back: a bundle carrying `references` is a
54
+ * `referenced` link.
55
+ */
56
+ export let names = (v: Vocab): Record<string, string> => {
57
+ let out: Record<string, string> = {}
58
+ for (let [name, tag] of Object.entries(relations(v))) out[tag] = name
59
+ return out
60
+ }
61
+
62
+ /**
63
+ * How each relation reads from its far end, as query name → phrase: what a
64
+ * page says about the links pointing at the entity it draws. A relation that
65
+ * declares no `reversed` is left out, and its reader shows the relation's name.
66
+ *
67
+ * ```ts
68
+ * import { assertEquals } from '@std/assert'
69
+ * import { loadVocab } from '@yaks/vocab'
70
+ * import { edgeKeywords, reversed } from '@yaks/edge'
71
+ *
72
+ * let v = loadVocab([{
73
+ * $defs: {
74
+ * cites: {
75
+ * component: true,
76
+ * type: 'object',
77
+ * edge: true,
78
+ * reversed: 'cited by',
79
+ * },
80
+ * },
81
+ * }], [edgeKeywords])
82
+ * assertEquals(reversed(v), { cites: 'cited by' })
83
+ * ```
84
+ */
85
+ export let reversed = (v: Vocab): Record<string, string> => {
86
+ let out: Record<string, string> = {}
87
+ for (let [name, tag] of Object.entries(relations(v))) {
88
+ let said = v.comp(tag)?.keywords?.reversed
89
+ if (typeof said == 'string' && said) out[name] = said
90
+ }
91
+ return out
92
+ }
package/say.js ADDED
@@ -0,0 +1,39 @@
1
+ // Creating a link, and removing it.
2
+ //
3
+ // A link is written the way everything else is — as a bundle. Its id comes from
4
+ // its own endpoints and relation, so it needs no id from anywhere else:
5
+ // `link()` derives it, and a write containing the same link twice writes one
6
+ // entity.
7
+ //
8
+ // Removing a link deletes its entity, including any facts attached to it.
9
+ // Saying the same link again recreates it at the same derived identity.
10
+ import { edgeEid } from "./eid.js";
11
+ import { EDGE } from "./relations.js";
12
+ let labelOf = (r)=>typeof r == 'string' ? r : r.label;
13
+ /**
14
+ * The bundle that creates a link: `link('p1', CITES, 'p2')`. `rel` is the
15
+ * component stored beside `edge`, with its identity ({@link relation}). `ord`
16
+ * is optional and behaves as a patch — passing it sets the link's position in
17
+ * its list, leaving it out leaves any stored position alone.
18
+ */ export let link = (from, rel, to, ord)=>({
19
+ entity: {
20
+ eid: edgeEid(from, rel, to)
21
+ },
22
+ [EDGE]: ord === undefined ? {
23
+ from,
24
+ to
25
+ } : {
26
+ from,
27
+ to,
28
+ ord
29
+ },
30
+ [labelOf(rel)]: {}
31
+ });
32
+ /**
33
+ * The bundle that deletes the link entity and its metadata. Linking the same
34
+ * endpoints and relation later recreates its derived identity.
35
+ */ export let unlink = (from, rel, to)=>({
36
+ ...link(from, rel, to),
37
+ $delete: true
38
+ });
39
+ //# sourceMappingURL=say.js.map
package/say.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"say.js","sources":["./say.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,EAAE;AACF,gFAAgF;AAChF,wEAAwE;AACxE,6EAA6E;AAC7E,UAAU;AACV,EAAE;AACF,0EAA0E;AAC1E,wEAAwE;AAGxE,SAAS,OAAO,mBAAiC;AACjD,SAAS,IAAI,yBAAwB;AAErC,IAAI,UAAU,CAAC,IAAwB,OAAO,KAAK,WAAW,IAAI,EAAE,KAAK;AAEzE;;;;;CAKC,GACD,OAAO,IAAI,OAAO,CAChB,MACA,KACA,IACA,MACW,CAAC;IACZ,QAAQ;MAAE,KAAK,QAAQ,MAAM,KAAK;IAAI;IACtC,CAAC,KAAK,EAAE,QAAQ,YAAY;MAAE;MAAM;IAAG,IAAI;MAAE;MAAM;MAAI;IAAI;IAC3D,CAAC,QAAQ,KAAK,EAAE,CAAC;EACnB,CAAC,EAAC;AAEF;;;CAGC,GACD,OAAO,IAAI,SAAS,CAAC,MAAW,KAAe,KAAoB,CAAC;IAClE,GAAG,KAAK,MAAM,KAAK,GAAG;IACtB,SAAS;EACX,CAAC,EAAC"}
package/say.ts ADDED
@@ -0,0 +1,41 @@
1
+ // Creating a link, and removing it.
2
+ //
3
+ // A link is written the way everything else is — as a bundle. Its id comes from
4
+ // its own endpoints and relation, so it needs no id from anywhere else:
5
+ // `link()` derives it, and a write containing the same link twice writes one
6
+ // entity.
7
+ //
8
+ // Removing a link deletes its entity, including any facts attached to it.
9
+ // Saying the same link again recreates it at the same derived identity.
10
+
11
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
12
+ import { edgeEid, type Relation } from './eid.js'
13
+ import { EDGE } from './relations.js'
14
+
15
+ let labelOf = (r: Relation): string => typeof r == 'string' ? r : r.label
16
+
17
+ /**
18
+ * The bundle that creates a link: `link('p1', CITES, 'p2')`. `rel` is the
19
+ * component stored beside `edge`, with its identity ({@link relation}). `ord`
20
+ * is optional and behaves as a patch — passing it sets the link's position in
21
+ * its list, leaving it out leaves any stored position alone.
22
+ */
23
+ export let link = (
24
+ from: Eid,
25
+ rel: Relation,
26
+ to: Eid,
27
+ ord?: number,
28
+ ): Bundle => ({
29
+ entity: { eid: edgeEid(from, rel, to) },
30
+ [EDGE]: ord === undefined ? { from, to } : { from, to, ord },
31
+ [labelOf(rel)]: {},
32
+ })
33
+
34
+ /**
35
+ * The bundle that deletes the link entity and its metadata. Linking the same
36
+ * endpoints and relation later recreates its derived identity.
37
+ */
38
+ export let unlink = (from: Eid, rel: Relation, to: Eid): Bundle => ({
39
+ ...link(from, rel, to),
40
+ $delete: true,
41
+ })
package/sql.js ADDED
@@ -0,0 +1,58 @@
1
+ // The query half: the walk over a relation, which @yaks/sql cannot compile on
2
+ // its own because it follows links, supplied here as an
3
+ // {@link https://jsr.io/@yaks/sql | @yaks/sql} Extension.
4
+ //
5
+ // `.cites[<=3]->p1` the posts that reach p1 through at most 3 cites
6
+ // `.cites<-p1` what p1 reaches: everything it cites, transitively
7
+ //
8
+ // The walk is a filter, and @yaks/sql compiles it — one recursive CTE, seeded
9
+ // at the target and stepped along the arrow (`walk`). What that package
10
+ // cannot know is the step for a relation: which rows of the edge table carry
11
+ // the component a relation name refers to. Only the vocabulary knows that, so
12
+ // this extension supplies the step when the walk's path names a relation and
13
+ // returns null when it does not, leaving the binder to try the path as a
14
+ // reference property instead.
15
+ //
16
+ // A link is an entity, so a query reads links as rows (`.cites&.edge.from=p1`)
17
+ // with nothing from this module.
18
+ //
19
+ // This module assumes the storage layout @yaks/sql's SQLite dialect reads and
20
+ // @yaks/sqlite creates: an `entity` table of integer ids, one table per
21
+ // component keyed by an `entity` owner column, and a reference column holding
22
+ // the referenced entity's integer id.
23
+ import { as, at, eq, join, select, walk } from "@yaks/sql";
24
+ import { EDGE, linkable } from "./relations.js";
25
+ // The links of one relation, as the pairs of integer ids they join. Joining
26
+ // against the relation's own table is the filter, so there is no union and
27
+ // nothing for the planner to prefer over the endpoint seek.
28
+ let l = at('l');
29
+ let linked = (tag, from)=>select({
30
+ cols: [
31
+ as(l('from'), 'from'),
32
+ as(l('to'), 'to')
33
+ ],
34
+ from: from(EDGE, 'l'),
35
+ joins: [
36
+ join(from(tag, 't'), eq(at('t')('entity'), l('entity')))
37
+ ]
38
+ });
39
+ /**
40
+ * The @yaks/sql extension that compiles the walk over a relation: `compile(ast,
41
+ * vocab, { extend: [traverse(vocab)] })`. It returns null when the vocabulary
42
+ * has no `edge` component, or when the walk names a relation it does not
43
+ * declare, and the walk then falls through to @yaks/sql's own
44
+ * reference-property walk.
45
+ */ export let traverse = (vocab)=>{
46
+ let rels = linkable(vocab);
47
+ return {
48
+ name: '@yaks/edge',
49
+ compile: {
50
+ walk: (clause, site)=>{
51
+ if (clause.kind != 'walk' || clause.path.length != 1) return null;
52
+ let t = rels[clause.path[0]];
53
+ return t ? walk(site.owner, clause, linked(t, site.from)) : null;
54
+ }
55
+ }
56
+ };
57
+ };
58
+ //# sourceMappingURL=sql.js.map
package/sql.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql.js","sources":["./sql.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,wDAAwD;AACxD,0DAA0D;AAC1D,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AACxE,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,yEAAyE;AACzE,8BAA8B;AAC9B,EAAE;AACF,+EAA+E;AAC/E,iCAAiC;AACjC,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AACxE,8EAA8E;AAC9E,sCAAsC;AAEtC,SACE,EAAE,EACF,EAAE,EACF,EAAE,EAEF,IAAI,EAEJ,MAAM,EAEN,IAAI,yBACuB;AAE7B,SAAS,IAAI,EAAE,QAAQ,yBAAwB;AAE/C,4EAA4E;AAC5E,2EAA2E;AAC3E,4DAA4D;AAC5D,IAAI,IAAI,GAAG;AACX,IAAI,SAAS,CAAC,KAAa,OACzB,OAAO;IACL,MAAM;MAAC,GAAG,EAAE,SAAS;MAAS,GAAG,EAAE,OAAO;KAAM;IAChD,MAAM,KAAK,MAAM;IACjB,OAAO;MAAC,KAAK,KAAK,KAAK,MAAM,GAAG,GAAG,KAAK,WAAW,EAAE;KAAY;EACnE;AAEF;;;;;;CAMC,GACD,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,OAAO,SAAS;EACpB,OAAO;IACL,MAAM;IACN,SAAS;MACP,MAAM,CAAC,QAAQ;QACb,IAAI,OAAO,IAAI,IAAI,UAAU,OAAO,IAAI,CAAC,MAAM,IAAI,GAAG,OAAO;QAC7D,IAAI,IAAI,IAAI,CAAC,OAAO,IAAI,CAAC,EAAE,CAAC;QAC5B,OAAO,IAAI,KAAK,KAAK,KAAK,EAAE,QAAQ,OAAO,GAAG,KAAK,IAAI,KAAK;MAC9D;IACF;EACF;AACF,EAAC"}
package/sql.ts ADDED
@@ -0,0 +1,68 @@
1
+ // The query half: the walk over a relation, which @yaks/sql cannot compile on
2
+ // its own because it follows links, supplied here as an
3
+ // {@link https://jsr.io/@yaks/sql | @yaks/sql} Extension.
4
+ //
5
+ // `.cites[<=3]->p1` the posts that reach p1 through at most 3 cites
6
+ // `.cites<-p1` what p1 reaches: everything it cites, transitively
7
+ //
8
+ // The walk is a filter, and @yaks/sql compiles it — one recursive CTE, seeded
9
+ // at the target and stepped along the arrow (`walk`). What that package
10
+ // cannot know is the step for a relation: which rows of the edge table carry
11
+ // the component a relation name refers to. Only the vocabulary knows that, so
12
+ // this extension supplies the step when the walk's path names a relation and
13
+ // returns null when it does not, leaving the binder to try the path as a
14
+ // reference property instead.
15
+ //
16
+ // A link is an entity, so a query reads links as rows (`.cites&.edge.from=p1`)
17
+ // with nothing from this module.
18
+ //
19
+ // This module assumes the storage layout @yaks/sql's SQLite dialect reads and
20
+ // @yaks/sqlite creates: an `entity` table of integer ids, one table per
21
+ // component keyed by an `entity` owner column, and a reference column holding
22
+ // the referenced entity's integer id.
23
+
24
+ import {
25
+ as,
26
+ at,
27
+ eq,
28
+ type Extension,
29
+ join,
30
+ type Query,
31
+ select,
32
+ type Source,
33
+ walk,
34
+ } from '@yaks/sql'
35
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
36
+ import { EDGE, linkable } from './relations.js'
37
+
38
+ // The links of one relation, as the pairs of integer ids they join. Joining
39
+ // against the relation's own table is the filter, so there is no union and
40
+ // nothing for the planner to prefer over the endpoint seek.
41
+ let l = at('l')
42
+ let linked = (tag: string, from: (comp: string, as: string) => Source): Query =>
43
+ select({
44
+ cols: [as(l('from'), 'from'), as(l('to'), 'to')],
45
+ from: from(EDGE, 'l'),
46
+ joins: [join(from(tag, 't'), eq(at('t')('entity'), l('entity')))],
47
+ })
48
+
49
+ /**
50
+ * The @yaks/sql extension that compiles the walk over a relation: `compile(ast,
51
+ * vocab, { extend: [traverse(vocab)] })`. It returns null when the vocabulary
52
+ * has no `edge` component, or when the walk names a relation it does not
53
+ * declare, and the walk then falls through to @yaks/sql's own
54
+ * reference-property walk.
55
+ */
56
+ export let traverse = (vocab: Vocab): Extension => {
57
+ let rels = linkable(vocab)
58
+ return {
59
+ name: '@yaks/edge',
60
+ compile: {
61
+ walk: (clause, site) => {
62
+ if (clause.kind != 'walk' || clause.path.length != 1) return null
63
+ let t = rels[clause.path[0]]
64
+ return t ? walk(site.owner, clause, linked(t, site.from)) : null
65
+ },
66
+ },
67
+ }
68
+ }
package/vocab.js ADDED
@@ -0,0 +1,19 @@
1
+ // The component declarations and how to read them: the module a server or a
2
+ // browser page imports at `@yaks/edge/vocab`. It reaches no storage, no SQL and
3
+ // no runtime, so a browser tab loading this vocabulary loads nothing else.
4
+ import { edgeDoc } from "./comp.js";
5
+ import { edgeKeywords } from "./keywords.js";
6
+ import manifest from './package.json' with {
7
+ type: 'json'
8
+ };
9
+ export { edgeDoc, edgeKeywords };
10
+ export { names, relations, reversed } from "./relations.js";
11
+ /** What this package is: its manifest's description, the one place it is
12
+ * written, which a host stamps on each document here. */ export let description = manifest.description;
13
+ /** Every document this plugin declares. */ export let docs = [
14
+ edgeDoc
15
+ ];
16
+ /** The JSON Schema keywords those documents use. */ export let keywords = [
17
+ edgeKeywords
18
+ ];
19
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,gFAAgF;AAChF,2EAA2E;AAG3E,SAAS,OAAO,oBAAmB;AACnC,SAAS,YAAY,wBAAuB;AAC5C,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,SAAS,OAAO,EAAE,YAAY,GAAE;AAChC,SAAS,KAAK,EAAE,SAAS,EAAE,QAAQ,yBAAwB;AAE3D;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,yCAAyC,GACzC,OAAO,IAAI,OAAmB;EAAC;CAAQ,CAAA;AAEvC,kDAAkD,GAClD,OAAO,IAAI,WAAuB;EAAC;CAAa,CAAA"}
package/vocab.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "$id": "urn:uuid:802dfb29-7101-4805-9719-f3b051c704cb",
3
+ "$vocabulary": {
4
+ "https://yak.sh/vocab/core": true,
5
+ "https://yak.sh/vocab/edge": true
6
+ },
7
+ "title": "edge",
8
+ "$defs": {
9
+ "edge": {
10
+ "$id": "urn:uuid:acec806f-e164-4277-95d1-46894e1ea6c1",
11
+ "component": true,
12
+ "type": "object",
13
+ "description": "a link between two entities; the kind of link is named by a second component stored beside this one",
14
+ "properties": {
15
+ "from": {
16
+ "$id": "urn:uuid:61876dbd-d690-456c-ab79-19bfa4fc9e69",
17
+ "type": "string",
18
+ "ref": "entity",
19
+ "death": "cascade",
20
+ "description": "the entity the link leads away from"
21
+ },
22
+ "to": {
23
+ "$id": "urn:uuid:a985900f-e909-4b7d-b005-e0891632d321",
24
+ "type": "string",
25
+ "ref": "entity",
26
+ "death": "cascade",
27
+ "description": "the entity the link arrives at"
28
+ },
29
+ "ord": {
30
+ "$id": "urn:uuid:ba369cc2-0053-4d4a-ac6e-93768da5b1f2",
31
+ "type": "number",
32
+ "description": "the link's position among the other links of its relation, for relations where order matters"
33
+ }
34
+ }
35
+ }
36
+ },
37
+ "kinds": {
38
+ "edge": {
39
+ "$id": "urn:uuid:acec806f-e164-4277-95d1-46894e1ea6c1",
40
+ "description": "a link between two entities; the kind of link is named by a second component stored beside this one",
41
+ "examples": [
42
+ {
43
+ "edge": {
44
+ "from": "7c1e1f0a-3b5d-4c2e-9a41-2f6d8b0e5a13",
45
+ "to": "2e4c6a8b-0d1f-4a3c-b5e7-9f1b3d5a7c9e"
46
+ },
47
+ "cites": {}
48
+ }
49
+ ]
50
+ }
51
+ }
52
+ }
package/vocab.ts ADDED
@@ -0,0 +1,21 @@
1
+ // The component declarations and how to read them: the module a server or a
2
+ // browser page imports at `@yaks/edge/vocab`. It reaches no storage, no SQL and
3
+ // no runtime, so a browser tab loading this vocabulary loads nothing else.
4
+
5
+ import type { Keywords, VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
6
+ import { edgeDoc } from './comp.js'
7
+ import { edgeKeywords } from './keywords.js'
8
+ import manifest from './package.json' with { type: 'json' }
9
+
10
+ export { edgeDoc, edgeKeywords }
11
+ export { names, relations, reversed } from './relations.js'
12
+
13
+ /** What this package is: its manifest's description, the one place it is
14
+ * written, which a host stamps on each document here. */
15
+ export let description: string = manifest.description
16
+
17
+ /** Every document this plugin declares. */
18
+ export let docs: VocabDoc[] = [edgeDoc]
19
+
20
+ /** The JSON Schema keywords those documents use. */
21
+ export let keywords: Keywords[] = [edgeKeywords]
package/walk.js ADDED
@@ -0,0 +1,88 @@
1
+ // Following links: the three questions anyone asks of a graph, implemented as
2
+ // ordinary queries against a {@link Storage}.
3
+ //
4
+ // out(p, 'cites') what does this post cite?
5
+ // in(p, 'cites') who cites it?
6
+ // reach(p, 'cites', 3) everything within three hops of it
7
+ //
8
+ // Each is a read, not a new mechanism: an edge is a component, so "the links
9
+ // out of p" is the query `.edge.from=p` narrowed to entities that also carry
10
+ // the relation's component. `reach` runs one such query per level, which is why
11
+ // its depth argument is required — an uncapped walk is a full graph scan under
12
+ // a friendlier name.
13
+ //
14
+ // Each returns a set of entity ids rather than links: the far endpoints,
15
+ // deduplicated, which is what a caller almost always wants. To read the links
16
+ // themselves, call `storage.read` with the same query.
17
+ //
18
+ // Synchronous in, synchronous out: a storage over an embedded database returns
19
+ // immediately and so does this, while an asynchronous storage makes the walk
20
+ // return a promise. No caller in between has to know which.
21
+ import { and, eq, list, present } from "@yaks/query";
22
+ import { after, each } from "@yaks/fp";
23
+ import { EDGE, relations } from "./relations.js";
24
+ let value = (eids)=>eids.length == 1 ? eids[0] : list(...eids);
25
+ /**
26
+ * The traversal helpers over a storage. `vocab` is the loaded vocabulary the
27
+ * storage serves; a relation it does not declare throws, since the alternative
28
+ * is an empty result that reads as "nothing links here".
29
+ */ export let walk = (storage, vocab)=>{
30
+ let rels = relations(vocab);
31
+ let tagOf = (relation)=>{
32
+ let tag = rels[relation];
33
+ if (!tag) {
34
+ throw new Error(`no such relation: ${relation}${Object.keys(rels).length ? ` (this vocabulary knows ${Object.keys(rels).sort().join(', ')})` : ''}`);
35
+ }
36
+ return tag;
37
+ };
38
+ // One level: the far endpoints of every link of this relation touching
39
+ // `eids`.
40
+ let hop = (eids, relation, dir)=>{
41
+ let tag = tagOf(relation);
42
+ let [here, there] = dir == 'out' ? [
43
+ 'from',
44
+ 'to'
45
+ ] : [
46
+ 'to',
47
+ 'from'
48
+ ];
49
+ if (!eids.length) return [];
50
+ let query = and(eq(`${EDGE}.${here}`, value(eids)), present(tag));
51
+ return after(storage.read(query), (bundles)=>bundles.flatMap((b)=>{
52
+ let far = b[EDGE]?.[there];
53
+ return typeof far == 'string' ? [
54
+ far
55
+ ] : [];
56
+ }));
57
+ };
58
+ let reach = (eid, relation, depth, dir = 'out')=>{
59
+ let seen = new Set();
60
+ let level = [
61
+ eid
62
+ ];
63
+ return after(each(Array.from({
64
+ length: Math.max(0, depth)
65
+ }, (_, i)=>i), null, ()=>after(hop(level, relation, dir), (far)=>{
66
+ // A cycle comes back to somewhere already walked; the seen set is
67
+ // what stops it, and the depth limit is what stops everything
68
+ // else. The starting entity is not included for being the start —
69
+ // but it is included if a path of at least one hop leads back to
70
+ // it.
71
+ level = far.filter((e)=>!seen.has(e));
72
+ for (let e of level)seen.add(e);
73
+ return null;
74
+ })), ()=>[
75
+ ...seen
76
+ ]);
77
+ };
78
+ return {
79
+ out: (eid, relation)=>hop([
80
+ eid
81
+ ], relation, 'out'),
82
+ in: (eid, relation)=>hop([
83
+ eid
84
+ ], relation, 'in'),
85
+ reach
86
+ };
87
+ };
88
+ //# sourceMappingURL=walk.js.map
package/walk.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.js","sources":["./walk.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8CAA8C;AAC9C,EAAE;AACF,oDAAoD;AACpD,wCAAwC;AACxC,6DAA6D;AAC7D,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,gFAAgF;AAChF,+EAA+E;AAC/E,qBAAqB;AACrB,EAAE;AACF,yEAAyE;AACzE,8EAA8E;AAC9E,uDAAuD;AACvD,EAAE;AACF,+EAA+E;AAC/E,6EAA6E;AAC7E,4DAA4D;AAE5D,SAAS,GAAG,EAAE,EAAE,EAAc,IAAI,EAAE,OAAO,2BAAgC;AAC3E,SAAS,KAAK,EAAE,IAAI,wBAA6B;AAIjD,SAAS,IAAI,EAAE,SAAS,yBAAwB;AAqBhD,IAAI,QAAQ,CAAC,OAAuB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,EAAE,GAAG,QAAQ;AAEzE;;;;CAIC,GACD,OAAO,IAAI,OAAO,CAAC,SAAkB;EACnC,IAAI,OAAO,UAAU;EACrB,IAAI,QAAQ,CAAC;IACX,IAAI,MAAM,IAAI,CAAC,SAAS;IACxB,IAAI,CAAC,KAAK;MACR,MAAM,IAAI,MACR,CAAC,kBAAkB,EAAE,WACnB,OAAO,IAAI,CAAC,MAAM,MAAM,GACpB,CAAC,wBAAwB,EAAE,OAAO,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GACjE,IACJ;IAEN;IACA,OAAO;EACT;EAEA,uEAAuE;EACvE,UAAU;EACV,IAAI,MAAM,CAAC,MAAa,UAAkB;IACxC,IAAI,MAAM,MAAM;IAChB,IAAI,CAAC,MAAM,MAAM,GAAG,OAAO,QAAQ;MAAC;MAAQ;KAAK,GAAG;MAAC;MAAM;KAAO;IAClE,IAAI,CAAC,KAAK,MAAM,EAAE,OAAO,EAAE;IAC3B,IAAI,QAAQ,IAAI,GAAG,GAAG,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,QAAQ,QAAQ;IAC5D,OAAO,MACL,QAAQ,IAAI,CAAC,QACb,CAAC,UACC,QAAQ,OAAO,CAAC,CAAC;QACf,IAAI,MAAO,CAAC,CAAC,KAAK,EAA0C,CAAC,MAAM;QACnE,OAAO,OAAO,OAAO,WAAW;UAAC;SAAI,GAAG,EAAE;MAC5C;EAEN;EAEA,IAAI,QAAQ,CAAC,KAAU,UAAkB,OAAe,MAAW,KAAK;IACtE,IAAI,OAAO,IAAI;IACf,IAAI,QAAQ;MAAC;KAAI;IACjB,OAAO,MACL,KACE,MAAM,IAAI,CAAC;MAAE,QAAQ,KAAK,GAAG,CAAC,GAAG;IAAO,GAAG,CAAC,GAAG,IAAM,IACrD,MACA,IACE,MAAM,IAAI,OAAO,UAAU,MAAM,CAAC;QAChC,kEAAkE;QAClE,8DAA8D;QAC9D,kEAAkE;QAClE,iEAAiE;QACjE,MAAM;QACN,QAAQ,IAAI,MAAM,CAAC,CAAC,IAAM,CAAC,KAAK,GAAG,CAAC;QACpC,KAAK,IAAI,KAAK,MAAO,KAAK,GAAG,CAAC;QAC9B,OAAO;MACT,KAEJ,IAAM;WAAI;OAAK;EAEnB;EAEA,OAAO;IACL,KAAK,CAAC,KAAK,WAAa,IAAI;QAAC;OAAI,EAAE,UAAU;IAC7C,IAAI,CAAC,KAAK,WAAa,IAAI;QAAC;OAAI,EAAE,UAAU;IAC5C;EACF;AACF,EAAC"}
package/walk.ts ADDED
@@ -0,0 +1,116 @@
1
+ // Following links: the three questions anyone asks of a graph, implemented as
2
+ // ordinary queries against a {@link Storage}.
3
+ //
4
+ // out(p, 'cites') what does this post cite?
5
+ // in(p, 'cites') who cites it?
6
+ // reach(p, 'cites', 3) everything within three hops of it
7
+ //
8
+ // Each is a read, not a new mechanism: an edge is a component, so "the links
9
+ // out of p" is the query `.edge.from=p` narrowed to entities that also carry
10
+ // the relation's component. `reach` runs one such query per level, which is why
11
+ // its depth argument is required — an uncapped walk is a full graph scan under
12
+ // a friendlier name.
13
+ //
14
+ // Each returns a set of entity ids rather than links: the far endpoints,
15
+ // deduplicated, which is what a caller almost always wants. To read the links
16
+ // themselves, call `storage.read` with the same query.
17
+ //
18
+ // Synchronous in, synchronous out: a storage over an embedded database returns
19
+ // immediately and so does this, while an asynchronous storage makes the walk
20
+ // return a promise. No caller in between has to know which.
21
+
22
+ import { and, eq, type Input, list, present } from '@yaks/query'
23
+ import { after, each } from '@yaks/fp'
24
+ import type { Eid } from 'jsr:@yaks/graph@0.2.36/bundle'
25
+ import type { Storage } from 'jsr:@yaks/graph@0.2.36/storage'
26
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
27
+ import { EDGE, relations } from './relations.js'
28
+
29
+ /** Which way a walk follows a link: away from the entity, or back to it. */
30
+ export type Dir = 'out' | 'in'
31
+
32
+ /** The traversal interface, bound to one storage and vocabulary. */
33
+ export type Walk = {
34
+ /** the entities this one links to through `relation` */
35
+ out: (eid: Eid, relation: string) => Eid[] | Promise<Eid[]>
36
+ /** the entities that link to this one through `relation` */
37
+ in: (eid: Eid, relation: string) => Eid[] | Promise<Eid[]>
38
+ /** everything within `depth` hops — the start included only if a path of at
39
+ * least one hop leads back to it, which is what `.cites->p1` selects too */
40
+ reach: (
41
+ eid: Eid,
42
+ relation: string,
43
+ depth: number,
44
+ dir?: Dir,
45
+ ) => Eid[] | Promise<Eid[]>
46
+ }
47
+
48
+ let value = (eids: Eid[]): Input => eids.length == 1 ? eids[0] : list(...eids)
49
+
50
+ /**
51
+ * The traversal helpers over a storage. `vocab` is the loaded vocabulary the
52
+ * storage serves; a relation it does not declare throws, since the alternative
53
+ * is an empty result that reads as "nothing links here".
54
+ */
55
+ export let walk = (storage: Storage, vocab: Vocab): Walk => {
56
+ let rels = relations(vocab)
57
+ let tagOf = (relation: string): string => {
58
+ let tag = rels[relation]
59
+ if (!tag) {
60
+ throw new Error(
61
+ `no such relation: ${relation}${
62
+ Object.keys(rels).length
63
+ ? ` (this vocabulary knows ${Object.keys(rels).sort().join(', ')})`
64
+ : ''
65
+ }`,
66
+ )
67
+ }
68
+ return tag
69
+ }
70
+
71
+ // One level: the far endpoints of every link of this relation touching
72
+ // `eids`.
73
+ let hop = (eids: Eid[], relation: string, dir: Dir) => {
74
+ let tag = tagOf(relation)
75
+ let [here, there] = dir == 'out' ? ['from', 'to'] : ['to', 'from']
76
+ if (!eids.length) return []
77
+ let query = and(eq(`${EDGE}.${here}`, value(eids)), present(tag))
78
+ return after(
79
+ storage.read(query),
80
+ (bundles) =>
81
+ bundles.flatMap((b) => {
82
+ let far = (b[EDGE] as Record<string, unknown> | undefined)?.[there]
83
+ return typeof far == 'string' ? [far] : []
84
+ }),
85
+ )
86
+ }
87
+
88
+ let reach = (eid: Eid, relation: string, depth: number, dir: Dir = 'out') => {
89
+ let seen = new Set<Eid>()
90
+ let level = [eid]
91
+ return after(
92
+ each(
93
+ Array.from({ length: Math.max(0, depth) }, (_, i) => i),
94
+ null,
95
+ () =>
96
+ after(hop(level, relation, dir), (far) => {
97
+ // A cycle comes back to somewhere already walked; the seen set is
98
+ // what stops it, and the depth limit is what stops everything
99
+ // else. The starting entity is not included for being the start —
100
+ // but it is included if a path of at least one hop leads back to
101
+ // it.
102
+ level = far.filter((e) => !seen.has(e))
103
+ for (let e of level) seen.add(e)
104
+ return null
105
+ }),
106
+ ),
107
+ () => [...seen],
108
+ )
109
+ }
110
+
111
+ return {
112
+ out: (eid, relation) => hop([eid], relation, 'out'),
113
+ in: (eid, relation) => hop([eid], relation, 'in'),
114
+ reach,
115
+ }
116
+ }