@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/_dist/mod.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @yaks/edge — links between entities, as a component.
3
+ *
4
+ * A relationship in a yaks graph is not a foreign-key column; it is an entity
5
+ * of its own, carrying the `edge{from, to, ord}` component plus a second
6
+ * component naming the relation. A blog declares that a post cites a post; a
7
+ * bookstore declares that a book cites a book; the structure is the same, and
8
+ * the relations are yours to declare — this package ships the link mechanism
9
+ * and no relations of its own.
10
+ *
11
+ * ```ts
12
+ * import { loadVocab } from '@yaks/vocab'
13
+ * import { edgeDoc, edgeKeywords, link } from '@yaks/edge'
14
+ *
15
+ * let blog = {
16
+ * $defs: {
17
+ * post: { component: true, type: 'object', kind: true },
18
+ * // one component, declared as a relation
19
+ * cites: { component: true, type: 'object', edge: true },
20
+ * },
21
+ * }
22
+ * let vocab = loadVocab([edgeDoc, blog], [edgeKeywords])
23
+ * link('p1', 'cites', 'p2') // the bundle that says p1 cites p2
24
+ * ```
25
+ *
26
+ * Four things follow from that:
27
+ *
28
+ * - **A link's id is derived from the link itself.** {@link edgeEid} hashes
29
+ * `from | relation | to`, so two writers who create the same link land on one
30
+ * entity, and a writer removing a link ({@link unlink}) computes its id
31
+ * without a lookup.
32
+ * - **A link lives only while both endpoints do.** Both endpoints are
33
+ * references declared `death: cascade`, so deleting a post deletes its links.
34
+ * - **An incomplete link is rejected** by name: an edge component with no
35
+ * relation beside it, or with an endpoint missing, never reaches storage
36
+ * ({@link edges} registers the check).
37
+ * - **Walking is querying.** {@link walk} implements `out`, `in` and a bounded
38
+ * `reach` over {@link https://jsr.io/@yaks/graph | @yaks/graph}'s `Storage`
39
+ * interface, and {@link traverse} adds to
40
+ * {@link https://jsr.io/@yaks/sql | @yaks/sql} the clause it cannot compile
41
+ * on its own: `.cites[<=3]->p1`, a recursive walk over one relation. A link
42
+ * is an entity, so a query reads links as rows (`.cites&.edge.from=p1`).
43
+ *
44
+ * It imports no platform API, so the same code runs on a server, in a worker,
45
+ * and in a browser tab.
46
+ *
47
+ * @module
48
+ */ export * from "./keywords.js";
49
+ export * from "./relations.js";
50
+ export * from "./comp.js";
51
+ export * from "./eid.js";
52
+ export * from "./say.js";
53
+ export * from "./order.js";
54
+ export * from "./guard.js";
55
+ export * from "./plugin.js";
56
+ export * from "./sql.js";
57
+ export * from "./walk.js";
58
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+CC,GAED,8BAA6B;AAC7B,+BAA8B;AAC9B,0BAAyB;AACzB,yBAAwB;AACxB,yBAAwB;AACxB,2BAA0B;AAC1B,2BAA0B;AAC1B,4BAA2B;AAC3B,yBAAwB;AACxB,0BAAyB"}
@@ -0,0 +1,7 @@
1
+ import type { Bundle } from "@yaks/graph";
2
+ /**
3
+ * The bundles that put `put` at `at` among the other links of a list, given
4
+ * in the order it is drawn. `put` is one of them, moving, or a link joining
5
+ * the list (`link()`), which is written whole with its position.
6
+ */ export declare let placed: (links: Bundle[], put: Bundle, at: number) => Bundle[];
7
+ //# sourceMappingURL=order.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order.d.ts","sources":["../order.ts"],"names":[],"mappings":"AAWA,cAAc,MAAM,2BAAgC;AAQpD;;;;CAIC,GAGD,OAAO,YAAI,SAAU,OAAO,UAAU,KAAK,QAAQ,IAAI,MAAM,KAAG,SAiB/D"}
@@ -0,0 +1,23 @@
1
+ import { type Plugin } from "@yaks/graph/plugin";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ /**
4
+ * The edge plugin:
5
+ *
6
+ * ```ts
7
+ * import { loadVocab } from '@yaks/vocab'
8
+ * import { graph } from '@yaks/graph'
9
+ * import { ram } from '@yaks/ram'
10
+ * import { edgeDoc, edgeKeywords, edges } from '@yaks/edge'
11
+ *
12
+ * let cites = { component: true, type: 'object', edge: true }
13
+ * let blog = { $defs: { cites } }
14
+ * let vocab = loadVocab([edgeDoc, blog], [edgeKeywords])
15
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [edges(vocab)] })
16
+ * ```
17
+ *
18
+ * It contributes {@link edgeDoc}, derives a link's id from its endpoints and
19
+ * relation (so a link written under a `$alias` lands on the same entity every
20
+ * time), and rejects in the `mint` phase any edge missing an endpoint or a
21
+ * relation.
22
+ */ export declare let edges: (vocab: Vocab) => Plugin;
23
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sources":["../plugin.ts"],"names":[],"mappings":"AASA,SAAe,KAAK,MAAM,kCAAuC;AACjE,cAAc,KAAK,2BAAgC;AAMnD;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,YAAI,QAAS,OAAO,UAAQ,OAKjC"}
@@ -0,0 +1,44 @@
1
+ import type { Vocab } from "@yaks/vocab";
2
+ /** The component every edge carries: its two ends, and its place in a list. */ export declare let EDGE: string;
3
+ /**
4
+ * Every relation the vocabulary declares, as query name → component name:
5
+ * `{ cites: 'cites', referenced: 'references' }`. Reads the `edge` keyword,
6
+ * so the vocabulary must have been loaded with `edgeKeywords` registered — an
7
+ * unregistered keyword is invisible to the loader.
8
+ */ export declare let relations: (v: Vocab) => Record<string, string>;
9
+ /**
10
+ * The relations a link here can be kept as: every one {@link relations}
11
+ * finds, or none in a vocabulary without the {@link EDGE} component each link
12
+ * carries. A graph composing @yaks/task but not @yaks/edge declares
13
+ * `contains`, yet holds no link of it, and a read of `edge.from` there is a
14
+ * read of a property it does not have.
15
+ */ export declare let linkable: (v: Vocab) => Record<string, string>;
16
+ /**
17
+ * The same declarations the other way round, as component name → query name.
18
+ * This is what reads a stored link back: a bundle carrying `references` is a
19
+ * `referenced` link.
20
+ */ export declare let names: (v: Vocab) => Record<string, string>;
21
+ /**
22
+ * How each relation reads from its far end, as query name → phrase: what a
23
+ * page says about the links pointing at the entity it draws. A relation that
24
+ * declares no `reversed` is left out, and its reader shows the relation's name.
25
+ *
26
+ * ```ts
27
+ * import { assertEquals } from '@std/assert'
28
+ * import { loadVocab } from '@yaks/vocab'
29
+ * import { edgeKeywords, reversed } from '@yaks/edge'
30
+ *
31
+ * let v = loadVocab([{
32
+ * $defs: {
33
+ * cites: {
34
+ * component: true,
35
+ * type: 'object',
36
+ * edge: true,
37
+ * reversed: 'cited by',
38
+ * },
39
+ * },
40
+ * }], [edgeKeywords])
41
+ * assertEquals(reversed(v), { cites: 'cited by' })
42
+ * ```
43
+ */ export declare let reversed: (v: Vocab) => Record<string, string>;
44
+ //# sourceMappingURL=relations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relations.d.ts","sources":["../relations.ts"],"names":[],"mappings":"AAiBA,cAAc,KAAK,2BAAgC;AAEnD,6EAA6E,GAC7E,OAAO,YAAI,aAAa;AAExB;;;;;CAKC,GACD,OAAO,YAAI,YAAa,GAAG,UAAQ,OAAO,MAAM,EAAE,MAAM,EAUvD;AAED;;;;;;CAMC,GACD,OAAO,YAAI,WAAY,GAAG,UAAQ,OAAO,MAAM,EAAE,MAAM,EACrB;AAElC;;;;CAIC,GACD,OAAO,YAAI,QAAS,GAAG,UAAQ,OAAO,MAAM,EAAE,MAAM,EAInD;AAED;;;;;;;;;;;;;;;;;;;;;;CAsBC,GACD,OAAO,YAAI,WAAY,GAAG,UAAQ,OAAO,MAAM,EAAE,MAAM,EAOtD"}
package/_dist/say.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { Bundle, Eid } from "@yaks/graph";
2
+ import { type Relation } from "./eid.js";
3
+ /**
4
+ * The bundle that creates a link: `link('p1', CITES, 'p2')`. `rel` is the
5
+ * component stored beside `edge`, with its identity ({@link relation}). `ord`
6
+ * is optional and behaves as a patch — passing it sets the link's position in
7
+ * its list, leaving it out leaves any stored position alone.
8
+ */ export declare let link: (from: Eid, rel: Relation, to: Eid, ord?: number) => Bundle;
9
+ /**
10
+ * The bundle that deletes the link entity and its metadata. Linking the same
11
+ * endpoints and relation later recreates its derived identity.
12
+ */ export declare let unlink: (from: Eid, rel: Relation, to: Eid) => Bundle;
13
+ //# sourceMappingURL=say.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"say.d.ts","sources":["../say.ts"],"names":[],"mappings":"AAUA,cAAc,MAAM,EAAE,GAAG,2BAAgC;AACzD,SAAkB,KAAK,QAAQ,mBAAkB;AAKjD;;;;;CAKC,GACD,OAAO,YAAI,OACT,MAAM,KACN,KAAK,UACL,IAAI,KACJ,MAAM,MAAM,KACX,OAID;AAEF;;;CAGC,GACD,OAAO,YAAI,SAAU,MAAM,KAAK,KAAK,UAAU,IAAI,QAAM,OAGvD"}
package/_dist/sql.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { type Extension } from "@yaks/sql";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ /**
4
+ * The @yaks/sql extension that compiles the walk over a relation: `compile(ast,
5
+ * vocab, { extend: [traverse(vocab)] })`. It returns null when the vocabulary
6
+ * has no `edge` component, or when the walk names a relation it does not
7
+ * declare, and the walk then falls through to @yaks/sql's own
8
+ * reference-property walk.
9
+ */ export declare let traverse: (vocab: Vocab) => Extension;
10
+ //# sourceMappingURL=sql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql.d.ts","sources":["../sql.ts"],"names":[],"mappings":"AAuBA,SAIE,KAAK,SAAS,yBAMa;AAC7B,cAAc,KAAK,2BAAgC;AAcnD;;;;;;CAMC,GACD,OAAO,YAAI,WAAY,OAAO,UAAQ,UAYrC"}
@@ -0,0 +1,10 @@
1
+ import type { Keywords, VocabDoc } from "@yaks/vocab";
2
+ import { edgeDoc } from "./comp.js";
3
+ import { edgeKeywords } from "./keywords.js";
4
+ export { edgeDoc, edgeKeywords };
5
+ export { names, relations, reversed } from "./relations.js";
6
+ /** What this package is: its manifest's description, the one place it is
7
+ * written, which a host stamps on each document here. */ export declare let description: string;
8
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
9
+ /** The JSON Schema keywords those documents use. */ export declare let keywords: Keywords[];
10
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,EAAE,QAAQ,2BAAgC;AAChE,SAAS,OAAO,oBAAmB;AACnC,SAAS,YAAY,wBAAuB;AAG5C,SAAS,OAAO,EAAE,YAAY,GAAE;AAChC,SAAS,KAAK,EAAE,SAAS,EAAE,QAAQ,yBAAwB;AAE3D;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAsB;AAEvC,kDAAkD,GAClD,OAAO,YAAI,UAAU,WAA2B"}
@@ -0,0 +1,16 @@
1
+ import type { Eid } from "@yaks/graph/bundle";
2
+ import type { Storage } from "@yaks/graph/storage";
3
+ import type { Vocab } from "@yaks/vocab";
4
+ /** Which way a walk follows a link: away from the entity, or back to it. */ export type Dir = 'out' | 'in';
5
+ /** The traversal interface, bound to one storage and vocabulary. */ export type Walk = {
6
+ /** the entities this one links to through `relation` */ out: (eid: Eid, relation: string) => Eid[] | Promise<Eid[]>;
7
+ /** the entities that link to this one through `relation` */ in: (eid: Eid, relation: string) => Eid[] | Promise<Eid[]>;
8
+ /** everything within `depth` hops — the start included only if a path of at
9
+ * least one hop leads back to it, which is what `.cites->p1` selects too */ reach: (eid: Eid, relation: string, depth: number, dir?: Dir) => Eid[] | Promise<Eid[]>;
10
+ };
11
+ /**
12
+ * The traversal helpers over a storage. `vocab` is the loaded vocabulary the
13
+ * storage serves; a relation it does not declare throws, since the alternative
14
+ * is an empty result that reads as "nothing links here".
15
+ */ export declare let walk: (storage: Storage, vocab: Vocab) => Walk;
16
+ //# sourceMappingURL=walk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.d.ts","sources":["../walk.ts"],"names":[],"mappings":"AAuBA,cAAc,GAAG,kCAAuC;AACxD,cAAc,OAAO,mCAAwC;AAC7D,cAAc,KAAK,2BAAgC;AAGnD,0EAA0E,GAC1E,YAAY,MAAM,QAAQ;AAE1B,kEAAkE,GAClE,YAAY;EACV,sDAAsD,GACtD,MAAM,KAAK,KAAK,UAAU,MAAM,KAAK,QAAQ,QAAQ;EACrD,0DAA0D,GAC1D,KAAK,KAAK,KAAK,UAAU,MAAM,KAAK,QAAQ,QAAQ;EACpD;4EAC0E,GAC1E,QACE,KAAK,KACL,UAAU,MAAM,EAChB,OAAO,MAAM,EACb,MAAM,QACH,QAAQ,QAAQ;;AAKvB;;;;CAIC,GACD,OAAO,YAAI,OAAQ,SAAS,SAAS,OAAO,UAAQ,KA6DnD"}
package/comp.js ADDED
@@ -0,0 +1,25 @@
1
+ // The one component this package ships: `edge{from, to, ord}`.
2
+ //
3
+ // Both endpoints are references declared `death: cascade`, which is the whole
4
+ // of a link's lifecycle. A link exists only while both of its endpoints do — a
5
+ // post linking to a deleted post links to nothing — so the link entity is
6
+ // deleted with either endpoint and no reader ever finds a link with one end
7
+ // missing. That leaves nothing else to implement: no unlink bookkeeping, no
8
+ // orphan sweep, no nullable endpoint.
9
+ //
10
+ // `ord` is the link's position in a list, for the relations where order is part
11
+ // of the meaning (a reading list, a table of contents). It is optional; a
12
+ // relation with no order simply never writes it.
13
+ //
14
+ // The component itself is declared in `./vocab.json` — plain JSON Schema,
15
+ // readable by anything that reads JSON. This file re-exports it under the name
16
+ // callers import and keeps the explanation of why it is shaped this way.
17
+ import doc from './vocab.json' with {
18
+ type: 'json'
19
+ };
20
+ /**
21
+ * The `edge` component as a vocabulary document, to load beside your own:
22
+ * `loadVocab([edgeDoc, ...mine], [edgeKeywords])`. The relation components are
23
+ * yours to declare — this document is only the link itself.
24
+ */ export let edgeDoc = doc;
25
+ //# sourceMappingURL=comp.js.map
package/comp.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"comp.js","sources":["./comp.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,0EAA0E;AAC1E,4EAA4E;AAC5E,4EAA4E;AAC5E,sCAAsC;AACtC,EAAE;AACF,gFAAgF;AAChF,0EAA0E;AAC1E,iDAAiD;AACjD,EAAE;AACF,0EAA0E;AAC1E,+EAA+E;AAC/E,yEAAyE;AAGzE,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AAEpD;;;;CAIC,GACD,OAAO,IAAI,UAAoB,IAAG"}
package/comp.ts ADDED
@@ -0,0 +1,26 @@
1
+ // The one component this package ships: `edge{from, to, ord}`.
2
+ //
3
+ // Both endpoints are references declared `death: cascade`, which is the whole
4
+ // of a link's lifecycle. A link exists only while both of its endpoints do — a
5
+ // post linking to a deleted post links to nothing — so the link entity is
6
+ // deleted with either endpoint and no reader ever finds a link with one end
7
+ // missing. That leaves nothing else to implement: no unlink bookkeeping, no
8
+ // orphan sweep, no nullable endpoint.
9
+ //
10
+ // `ord` is the link's position in a list, for the relations where order is part
11
+ // of the meaning (a reading list, a table of contents). It is optional; a
12
+ // relation with no order simply never writes it.
13
+ //
14
+ // The component itself is declared in `./vocab.json` — plain JSON Schema,
15
+ // readable by anything that reads JSON. This file re-exports it under the name
16
+ // callers import and keeps the explanation of why it is shaped this way.
17
+
18
+ import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
19
+ import doc from './vocab.json' with { type: 'json' }
20
+
21
+ /**
22
+ * The `edge` component as a vocabulary document, to load beside your own:
23
+ * `loadVocab([edgeDoc, ...mine], [edgeKeywords])`. The relation components are
24
+ * yours to declare — this document is only the link itself.
25
+ */
26
+ export let edgeDoc: VocabDoc = doc
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts","./testing.ts"]},"name":"@yaks/edge","version":"0.2.36","license":"Apache-2.0","description":"Creates directed links as graph entities with content-derived eids, validates writes, and follows links through storage reads and SQL queries.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./graph":"./graph.ts"},"imports":{"@yaks/edge":"./mod.ts","@yaks/edge/vocab":"./vocab.ts","@yaks/edge/graph":"./graph.ts"}}
package/eid.js ADDED
@@ -0,0 +1,66 @@
1
+ // A link's identity, derived from the link itself.
2
+ //
3
+ // A link entity is content-addressed, the way a blob is named by the hash of
4
+ // its bytes. Its content is the three things a link is — from, relation, to —
5
+ // so two writers who create the same link land on one entity instead of two,
6
+ // and a writer removing a link computes its id without looking it up. Direction
7
+ // is part of that content: `a cites b` and `b cites a` are two links.
8
+ //
9
+ // The relation is hashed by its identity, the UUID its `$id` gives, never its
10
+ // label, so renaming a relation moves no link; a relation with no `$id` yet
11
+ // hashes its label, as every relation did before identities.
12
+ //
13
+ // This is the derivation. Every caller computes it here — the browser page that
14
+ // creates a link, the server that accepts it, the code that removes it —
15
+ // because an id computed two different ways is two different ids.
16
+ import { comps } from "@yaks/graph/bundle";
17
+ import { derivedEid } from "@yaks/graph/identity";
18
+ import { formerKey } from "@yaks/graph/identity";
19
+ import { identity, identityIn } from "@yaks/vocab/runtime";
20
+ /** One relation a vocabulary document declares, for {@link edgeEid},
21
+ * `link` and `unlink`: `relation(kernelDoc, 'cites')`. */ export let relation = (doc, label)=>({
22
+ label,
23
+ id: identity(doc, label)
24
+ });
25
+ /** One relation as a loaded vocabulary holds it, for a relation named by
26
+ * data: `relationIn(vocab, tag)`. */ export let relationIn = (vocab, label)=>({
27
+ label: vocab.comp(label)?.name ?? label,
28
+ id: identityIn(vocab, label)
29
+ });
30
+ /**
31
+ * The eid a link derives: `sha256("<from>|<relation>|<to>")` formatted as a
32
+ * UUID (@yaks/graph's `derivedEid` — the one derivation shared by everything
33
+ * content-addressed, so an id computed here and an id computed elsewhere are
34
+ * the same id).
35
+ *
36
+ * `relation` is the component stored beside `edge` ({@link relation}), or
37
+ * its identity (`identityIn` over a loaded vocabulary), or its label while it
38
+ * has no `$id`; never the name a query uses for it.
39
+ */ export let edgeEid = (from, relation, to)=>derivedEid(`${from}|${typeof relation == 'string' ? relation : relation.id}|${to}`);
40
+ /**
41
+ * The name of the relation component in a bundle, or `undefined` when it has
42
+ * none. `tags` is the vocabulary's component → relation-name map
43
+ * ({@link names}); only a declared relation counts, so an ordinary component
44
+ * stored beside the edge is not mistaken for one.
45
+ */ export let tagOf = (bundle, tags)=>comps(bundle).find(([name, comp])=>comp && tags[name])?.[0];
46
+ /**
47
+ * How the `edge` component derives its own entity id — the {@link Derive} the
48
+ * graph calls when an edge bundle arrives under a `$alias`, so that the write
49
+ * creating a link also learns the id it landed on. The relation is hashed by
50
+ * the identity `vocab` gives it.
51
+ *
52
+ * An incomplete link derives nothing (it returns `''`) and the entity is given
53
+ * an ordinary generated id, at which point the {@link stated} hook rejects the
54
+ * write and names the missing part — a far better error than a link quietly
55
+ * identified by half of itself.
56
+ */ export let derive = (tags, vocab)=>{
57
+ let by = (key)=>({ from, to }, bundle)=>{
58
+ let tag = tagOf(bundle, tags);
59
+ let id = tag && key(tag);
60
+ return id && from != null && to != null ? edgeEid(String(from), id, String(to)) : '';
61
+ };
62
+ return vocab ? Object.assign(by((tag)=>identityIn(vocab, tag)), {
63
+ former: by((tag)=>formerKey(vocab, tag))
64
+ }) : by((tag)=>tag);
65
+ };
66
+ //# sourceMappingURL=eid.js.map
package/eid.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eid.js","sources":["./eid.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,gFAAgF;AAChF,sEAAsE;AACtE,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,6DAA6D;AAC7D,EAAE;AACF,gFAAgF;AAChF,yEAAyE;AACzE,kEAAkE;AAGlE,SAAS,KAAK,kCAAuC;AACrD,SAAS,UAAU,oCAAyC;AAE5D,SAAS,SAAS,oCAAyC;AAC3D,SACE,QAAQ,EACR,UAAU,mCAG2B;AASvC;wDACwD,GACxD,OAAO,IAAI,WAAW,CAAC,KAAe,QAA4B,CAAC;IACjE;IACA,IAAI,SAAS,KAAK;EACpB,CAAC,EAAC;AAEF;mCACmC,GACnC,OAAO,IAAI,aAAa,CACtB,OACA,QACa,CAAC;IACd,OAAO,MAAM,IAAI,CAAC,QAAQ,QAAQ;IAClC,IAAI,WAAW,OAAO;EACxB,CAAC,EAAC;AAEF;;;;;;;;;CASC,GACD,OAAO,IAAI,UAAU,CAAC,MAAW,UAAoB,KACnD,WACE,GAAG,KAAK,CAAC,EAAE,OAAO,YAAY,WAAW,WAAW,SAAS,EAAE,CAAC,CAAC,EAAE,IAAI,EACxE;AAEH;;;;;CAKC,GACD,OAAO,IAAI,QAAQ,CACjB,QACA,OAEA,MAAM,QAAQ,IAAI,CAAC,CAAC,CAAC,MAAM,KAAK,GAAK,QAAQ,IAAI,CAAC,KAAK,GAAG,CAAC,EAAE,CAAA;AAE/D;;;;;;;;;;CAUC,GACD,OAAO,IAAI,SAAS,CAClB,MACA;EAEA,IAAI,KACF,CAAC,MACD,CAAC,EAAE,IAAI,EAAE,EAAE,EAAQ,EAAE;MACnB,IAAI,MAAM,MAAM,QAAQ;MACxB,IAAI,KAAK,OAAO,IAAI;MACpB,OAAO,MAAM,QAAQ,QAAQ,MAAM,OAC/B,QAAQ,OAAO,OAAO,IAAI,OAAO,OACjC;IACN;EACF,OAAO,QACH,OAAO,MAAM,CAAC,GAAG,CAAC,MAAQ,WAAW,OAAO,OAAO;IACnD,QAAQ,GAAG,CAAC,MAAQ,UAAU,OAAO;EACvC,KACE,GAAG,CAAC,MAAQ;AAClB,EAAC"}
package/eid.ts ADDED
@@ -0,0 +1,109 @@
1
+ // A link's identity, derived from the link itself.
2
+ //
3
+ // A link entity is content-addressed, the way a blob is named by the hash of
4
+ // its bytes. Its content is the three things a link is — from, relation, to —
5
+ // so two writers who create the same link land on one entity instead of two,
6
+ // and a writer removing a link computes its id without looking it up. Direction
7
+ // is part of that content: `a cites b` and `b cites a` are two links.
8
+ //
9
+ // The relation is hashed by its identity, the UUID its `$id` gives, never its
10
+ // label, so renaming a relation moves no link; a relation with no `$id` yet
11
+ // hashes its label, as every relation did before identities.
12
+ //
13
+ // This is the derivation. Every caller computes it here — the browser page that
14
+ // creates a link, the server that accepts it, the code that removes it —
15
+ // because an id computed two different ways is two different ids.
16
+
17
+ import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
18
+ import { comps } from '@yaks/graph/bundle'
19
+ import { derivedEid } from '@yaks/graph/identity'
20
+ import type { Derive } from 'jsr:@yaks/graph@0.2.36'
21
+ import { formerKey } from '@yaks/graph/identity'
22
+ import {
23
+ identity,
24
+ identityIn,
25
+ type Vocab,
26
+ type VocabDoc,
27
+ } from '@yaks/vocab/runtime'
28
+
29
+ /**
30
+ * A relation as a link names it: its label, the component the link carries,
31
+ * and its identity, what the link's eid hashes. A label alone is a relation
32
+ * with no `$id` yet, whose label is its identity.
33
+ */
34
+ export type Relation = string | { label: string; id: string }
35
+
36
+ /** One relation a vocabulary document declares, for {@link edgeEid},
37
+ * `link` and `unlink`: `relation(kernelDoc, 'cites')`. */
38
+ export let relation = (doc: VocabDoc, label: string): Relation => ({
39
+ label,
40
+ id: identity(doc, label),
41
+ })
42
+
43
+ /** One relation as a loaded vocabulary holds it, for a relation named by
44
+ * data: `relationIn(vocab, tag)`. */
45
+ export let relationIn = (
46
+ vocab: Pick<Vocab, 'comp'>,
47
+ label: string,
48
+ ): Relation => ({
49
+ label: vocab.comp(label)?.name ?? label,
50
+ id: identityIn(vocab, label),
51
+ })
52
+
53
+ /**
54
+ * The eid a link derives: `sha256("<from>|<relation>|<to>")` formatted as a
55
+ * UUID (@yaks/graph's `derivedEid` — the one derivation shared by everything
56
+ * content-addressed, so an id computed here and an id computed elsewhere are
57
+ * the same id).
58
+ *
59
+ * `relation` is the component stored beside `edge` ({@link relation}), or
60
+ * its identity (`identityIn` over a loaded vocabulary), or its label while it
61
+ * has no `$id`; never the name a query uses for it.
62
+ */
63
+ export let edgeEid = (from: Eid, relation: Relation, to: Eid): Eid =>
64
+ derivedEid(
65
+ `${from}|${typeof relation == 'string' ? relation : relation.id}|${to}`,
66
+ )
67
+
68
+ /**
69
+ * The name of the relation component in a bundle, or `undefined` when it has
70
+ * none. `tags` is the vocabulary's component → relation-name map
71
+ * ({@link names}); only a declared relation counts, so an ordinary component
72
+ * stored beside the edge is not mistaken for one.
73
+ */
74
+ export let tagOf = (
75
+ bundle: Bundle,
76
+ tags: Record<string, string>,
77
+ ): string | undefined =>
78
+ comps(bundle).find(([name, comp]) => comp && tags[name])?.[0]
79
+
80
+ /**
81
+ * How the `edge` component derives its own entity id — the {@link Derive} the
82
+ * graph calls when an edge bundle arrives under a `$alias`, so that the write
83
+ * creating a link also learns the id it landed on. The relation is hashed by
84
+ * the identity `vocab` gives it.
85
+ *
86
+ * An incomplete link derives nothing (it returns `''`) and the entity is given
87
+ * an ordinary generated id, at which point the {@link stated} hook rejects the
88
+ * write and names the missing part — a far better error than a link quietly
89
+ * identified by half of itself.
90
+ */
91
+ export let derive = (
92
+ tags: Record<string, string>,
93
+ vocab?: Pick<Vocab, 'comp'>,
94
+ ): Derive => {
95
+ let by =
96
+ (key: (tag: string) => string | undefined): Derive =>
97
+ ({ from, to }: Comp, bundle: Bundle) => {
98
+ let tag = tagOf(bundle, tags)
99
+ let id = tag && key(tag)
100
+ return id && from != null && to != null
101
+ ? edgeEid(String(from), id, String(to))
102
+ : ''
103
+ }
104
+ return vocab
105
+ ? Object.assign(by((tag) => identityIn(vocab, tag)), {
106
+ former: by((tag) => formerKey(vocab, tag)),
107
+ })
108
+ : by((tag) => tag)
109
+ }
package/graph.js ADDED
@@ -0,0 +1,18 @@
1
+ // The graph plugins this package contributes: the module a server imports at
2
+ // `@yaks/edge/graph` to get the behaviour a write to the graph gets. A link's
3
+ // eid is derived from its endpoints and relation, so writing the same link
4
+ // twice writes one row and removing it clears the components of that same
5
+ // entity — behaviour applied at write time, not a component declaration.
6
+ //
7
+ // The same subpath carries the query half, because a host gathers every read
8
+ // path's clause compilers from here (@yaks/cli `GraphFacet`): without it, a
9
+ // host that composes this package refuses a walk over a relation.
10
+ import { edges } from "./plugin.js";
11
+ import { traverse } from "./sql.js";
12
+ /** Derived edge ids, and the relations this vocabulary declares. */ export let plugins = (host)=>[
13
+ edges(host.vocab)
14
+ ];
15
+ /** The walk over a relation, compiled (./sql.ts). */ export let extend = (host)=>[
16
+ traverse(host.vocab)
17
+ ];
18
+ //# sourceMappingURL=graph.js.map
package/graph.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,2EAA2E;AAC3E,0EAA0E;AAC1E,yEAAyE;AACzE,EAAE;AACF,6EAA6E;AAC7E,4EAA4E;AAC5E,kEAAkE;AAKlE,SAAS,KAAK,sBAAqB;AACnC,SAAS,QAAQ,mBAAkB;AAEnC,kEAAkE,GAClE,OAAO,IAAI,UAAU,CAAC,OAAqC;IAAC,MAAM,KAAK,KAAK;GAAE,CAAA;AAE9E,mDAAmD,GACnD,OAAO,IAAI,SAAS,CAAC,OAAwC;IAC3D,SAAS,KAAK,KAAK;GACpB,CAAA"}
package/graph.ts ADDED
@@ -0,0 +1,23 @@
1
+ // The graph plugins this package contributes: the module a server imports at
2
+ // `@yaks/edge/graph` to get the behaviour a write to the graph gets. A link's
3
+ // eid is derived from its endpoints and relation, so writing the same link
4
+ // twice writes one row and removing it clears the components of that same
5
+ // entity — behaviour applied at write time, not a component declaration.
6
+ //
7
+ // The same subpath carries the query half, because a host gathers every read
8
+ // path's clause compilers from here (@yaks/cli `GraphFacet`): without it, a
9
+ // host that composes this package refuses a walk over a relation.
10
+
11
+ import type { Plugin } from 'jsr:@yaks/graph@0.2.36'
12
+ import type { Extension } from 'jsr:@yaks/sql@0.2.36'
13
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
14
+ import { edges } from './plugin.js'
15
+ import { traverse } from './sql.js'
16
+
17
+ /** Derived edge ids, and the relations this vocabulary declares. */
18
+ export let plugins = (host: { vocab: Vocab }): Plugin[] => [edges(host.vocab)]
19
+
20
+ /** The walk over a relation, compiled (./sql.ts). */
21
+ export let extend = (host: { vocab: Vocab }): Extension[] => [
22
+ traverse(host.vocab),
23
+ ]
package/guard.js ADDED
@@ -0,0 +1,71 @@
1
+ // The rejection: an incomplete link is not a link.
2
+ //
3
+ // A link is three things — an endpoint, a relation, an endpoint — and any two
4
+ // of them mean nothing. A bundle holding an edge component with no relation
5
+ // beside it would be stored as a row nothing can interpret; one missing an
6
+ // endpoint would point nowhere. Both are caught here, in the `mint` phase,
7
+ // which runs after the graph has resolved every `$alias`, so an endpoint
8
+ // written as an alias is already the id it resolved to; an edge the batch
9
+ // minted is named by its alias, the name its writer knows.
10
+ //
11
+ // A bundle naming neither endpoint is a patch of a link that already exists
12
+ // (setting `ord`, say) and is left alone: it does not create a link, so it
13
+ // cannot create half of one.
14
+ //
15
+ // Every bundle in the write is read before any check, because one entity may
16
+ // arrive as several bundles — the edge component in one, its relation component
17
+ // in another — and together they are one link.
18
+ import { comps } from "@yaks/graph/bundle";
19
+ import { Refused } from "@yaks/graph/errors";
20
+ import { EDGE, names } from "./relations.js";
21
+ import { tagOf } from "./eid.js";
22
+ // Every bundle in the write merged per entity, so a link spread over several
23
+ // of them is checked as the one link it is.
24
+ let gathered = (bundles)=>{
25
+ let out = new Map();
26
+ for (let b of bundles){
27
+ let at = out.get(b.entity.eid) ?? {
28
+ entity: b.entity
29
+ };
30
+ if (b.$alias) at.$alias = b.$alias;
31
+ for (let [name, comp] of comps(b)){
32
+ let had = at[name];
33
+ at[name] = comp == null ? null : {
34
+ ...had ?? {},
35
+ ...comp
36
+ };
37
+ }
38
+ out.set(b.entity.eid, at);
39
+ }
40
+ return out;
41
+ };
42
+ /**
43
+ * The `mint` hook that rejects an incomplete link, naming what is missing.
44
+ * Registered by the {@link plugin}; exported on its own for a graph that wants
45
+ * the check without the rest.
46
+ */ export let stated = (vocab)=>{
47
+ let tags = names(vocab);
48
+ let known = Object.values(tags).sort();
49
+ return (bundles)=>{
50
+ for (let [id, b] of gathered(bundles)){
51
+ // An edge the batch minted is named as the batch named it.
52
+ let eid = b.$alias ?? id;
53
+ let edge = b[EDGE];
54
+ // no link created here (a patch, or a bundle about something else)
55
+ if (!edge || edge.from == null && edge.to == null) continue;
56
+ for (let end of [
57
+ 'from',
58
+ 'to'
59
+ ]){
60
+ if (edge[end] == null) {
61
+ throw new Refused(`edge ${eid} has no \`${end}\` end`);
62
+ }
63
+ }
64
+ if (!tagOf(b, tags)) {
65
+ throw new Refused(`edge ${eid} declares no relation — an edge carries a relation tag ` + `beside edge{from, to}${known.length ? ` (this vocabulary knows ${known.join(', ')})` : ''}`);
66
+ }
67
+ }
68
+ return bundles;
69
+ };
70
+ };
71
+ //# sourceMappingURL=guard.js.map
package/guard.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guard.js","sources":["./guard.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,2EAA2E;AAC3E,2EAA2E;AAC3E,yEAAyE;AACzE,0EAA0E;AAC1E,2DAA2D;AAC3D,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,6BAA6B;AAC7B,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,+CAA+C;AAG/C,SAAS,KAAK,kCAAuC;AACrD,SAAS,OAAO,kCAAuC;AAEvD,SAAS,IAAI,EAAE,KAAK,yBAAwB;AAC5C,SAAS,KAAK,mBAAkB;AAEhC,6EAA6E;AAC7E,4CAA4C;AAC5C,IAAI,WAAW,CAAC;EACd,IAAI,MAAM,IAAI;EACd,KAAK,IAAI,KAAK,QAAS;IACrB,IAAI,KAAK,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,KAAK;MAAE,QAAQ,EAAE,MAAM;IAAC;IACrD,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,EAAE,MAAM;IAClC,KAAK,IAAI,CAAC,MAAM,KAAK,IAAI,MAAM,GAAI;MACjC,IAAI,MAAM,EAAE,CAAC,KAAK;MAClB,EAAE,CAAC,KAAK,GAAG,QAAQ,OAAO,OAAO;QAAE,GAAI,OAAO,CAAC,CAAC;QAAG,GAAG,IAAI;MAAC;IAC7D;IACA,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,EAAE;EACxB;EACA,OAAO;AACT;AAEA;;;;CAIC,GACD,OAAO,IAAI,SAAS,CAClB;EAEA,IAAI,OAAO,MAAM;EACjB,IAAI,QAAQ,OAAO,MAAM,CAAC,MAAM,IAAI;EACpC,OAAO,CAAC;IACN,KAAK,IAAI,CAAC,IAAI,EAAE,IAAI,SAAS,SAAU;MACrC,2DAA2D;MAC3D,IAAI,MAAM,EAAE,MAAM,IAAI;MACtB,IAAI,OAAO,CAAC,CAAC,KAAK;MAClB,mEAAmE;MACnE,IAAI,CAAC,QAAS,KAAK,IAAI,IAAI,QAAQ,KAAK,EAAE,IAAI,MAAO;MACrD,KAAK,IAAI,OAAO;QAAC;QAAQ;OAAK,CAAE;QAC9B,IAAI,IAAI,CAAC,IAAI,IAAI,MAAM;UACrB,MAAM,IAAI,QAAQ,CAAC,KAAK,EAAE,IAAI,UAAU,EAAE,IAAI,MAAM,CAAC;QACvD;MACF;MACA,IAAI,CAAC,MAAM,GAAG,OAAO;QACnB,MAAM,IAAI,QACR,CAAC,KAAK,EAAE,IAAI,uDAAuD,CAAC,GAClE,CAAC,qBAAqB,EACpB,MAAM,MAAM,GAAG,CAAC,wBAAwB,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,IAChE;MAER;IACF;IACA,OAAO;EACT;AACF,EAAC"}