@yaks/key 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.
- package/LICENSE +202 -0
- package/README.md +108 -2
- package/_dist/comp.d.ts +14 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/eid.d.ts +26 -0
- package/_dist/eid.d.ts.map +1 -0
- package/_dist/graph.d.ts +6 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/guard.d.ts +9 -0
- package/_dist/guard.d.ts.map +1 -0
- package/_dist/keywords.d.ts +8 -0
- package/_dist/keywords.d.ts.map +1 -0
- package/_dist/kinds.d.ts +14 -0
- package/_dist/kinds.d.ts.map +1 -0
- package/_dist/mod.d.ts +70 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/plugin.d.ts +23 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/resolve.d.ts +26 -0
- package/_dist/resolve.d.ts.map +1 -0
- package/_dist/say.d.ts +10 -0
- package/_dist/say.d.ts.map +1 -0
- package/_dist/vocab.d.ts +9 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/comp.js +49 -0
- package/comp.js.map +1 -0
- package/comp.ts +62 -0
- package/deno.json +1 -0
- package/eid.js +43 -0
- package/eid.js.map +1 -0
- package/eid.ts +56 -0
- package/graph.js +9 -0
- package/graph.js.map +1 -0
- package/graph.ts +11 -0
- package/guard.js +67 -0
- package/guard.js.map +1 -0
- package/guard.ts +69 -0
- package/keywords.js +34 -0
- package/keywords.js.map +1 -0
- package/keywords.ts +31 -0
- package/kinds.js +39 -0
- package/kinds.js.map +1 -0
- package/kinds.ts +46 -0
- package/meta/key.vocab.json +13 -0
- package/mod.js +70 -0
- package/mod.js.map +1 -0
- package/mod.ts +71 -0
- package/package.json +26 -4
- package/plugin.js +62 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +58 -0
- package/resolve.js +163 -0
- package/resolve.js.map +1 -0
- package/resolve.ts +155 -0
- package/say.js +39 -0
- package/say.js.map +1 -0
- package/say.ts +37 -0
- package/vocab.js +18 -0
- package/vocab.js.map +1 -0
- package/vocab.json +36 -0
- package/vocab.ts +20 -0
package/comp.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The one component this package ships: `key{of, value}`.
|
|
2
|
+
//
|
|
3
|
+
// It is to a has-many VALUE what `edge{from, to}` is to a link. An entity does
|
|
4
|
+
// not hold its identifying values in a property; each value is a key entity of
|
|
5
|
+
// its own — `key{of, value}` plus a kind tag component naming which kind of
|
|
6
|
+
// value it is — so a recipe is identified by both `lemon-cake` and
|
|
7
|
+
// `recipe:2019-07` because two rows point at it, adding one is a write, and
|
|
8
|
+
// retiring one means deleting that row. A list property would have been the
|
|
9
|
+
// other design and it is the worse one: every write would be a
|
|
10
|
+
// read-modify-write of somebody else's property, and two writers adding a value
|
|
11
|
+
// at once would lose one.
|
|
12
|
+
//
|
|
13
|
+
// `of` is a reference declared `death: release`, which is the whole of a key's
|
|
14
|
+
// lifecycle: a value for a deleted thing identifies nothing, and the row is
|
|
15
|
+
// removed rather than the entity, so the value can be claimed again.
|
|
16
|
+
//
|
|
17
|
+
// The value is unique within its kind, and nothing declares that — the key's
|
|
18
|
+
// own id is `sha256("<tag>|<value>")` (./eid.ts), so two writers giving the
|
|
19
|
+
// same value in the same kind land on one row by construction. A `unique` on
|
|
20
|
+
// the property would have been wrong anyway: two kinds may hold the same
|
|
21
|
+
// string, and only the pair is the constraint.
|
|
22
|
+
//
|
|
23
|
+
// The document itself is `./vocab.json` — plain JSON Schema, readable by
|
|
24
|
+
// anything that reads JSON. This file re-exports it under the name callers
|
|
25
|
+
// import and holds the explanation of why it is shaped the way it is.
|
|
26
|
+
|
|
27
|
+
import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
28
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
29
|
+
import doc from './vocab.json' with { type: 'json' }
|
|
30
|
+
import { KEY } from './kinds.js'
|
|
31
|
+
|
|
32
|
+
/** The property naming the entity the value identifies. */
|
|
33
|
+
export let OF = 'of'
|
|
34
|
+
|
|
35
|
+
/** The property carrying the value. */
|
|
36
|
+
export let VALUE = 'value'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The `key` component as a vocabulary document, to load beside your own:
|
|
40
|
+
* `loadVocab([keyDoc, ...mine], [keyKeywords])`. The kind tags are yours to
|
|
41
|
+
* declare — this document is only the carrier.
|
|
42
|
+
*/
|
|
43
|
+
export let keyDoc: VocabDoc = doc
|
|
44
|
+
|
|
45
|
+
/** The `key` component in a bundle, if it has one. */
|
|
46
|
+
export let keyOf = (b: Bundle): Comp | undefined => {
|
|
47
|
+
let comp = b[KEY]
|
|
48
|
+
return comp && typeof comp == 'object' ? comp as Comp : undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// One property of a bundle's key, as a non-empty string, or nothing.
|
|
52
|
+
let said = (b: Bundle | undefined, prop: string): string | undefined => {
|
|
53
|
+
let v = b && keyOf(b)?.[prop]
|
|
54
|
+
return typeof v == 'string' && v ? v : undefined
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The value in a bundle's key. An absent component, a cleared one and an
|
|
58
|
+
* empty string all mean there is no value. */
|
|
59
|
+
export let valueOf = (b: Bundle): string | undefined => said(b, VALUE)
|
|
60
|
+
|
|
61
|
+
/** The entity a bundle's key identifies, if it identifies one. */
|
|
62
|
+
export let ofOf = (b: Bundle | undefined): Eid | undefined => said(b, OF)
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts","./testing.ts"]},"name":"@yaks/key","version":"0.2.36","license":"Apache-2.0","description":"Represents an entity's identifying values as separate graph entities, with ids derived from each value and its kind to enforce uniqueness.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./graph":"./graph.ts"},"imports":{"@yaks/key":"./mod.ts","@yaks/key/vocab":"./vocab.ts","@yaks/key/graph":"./graph.ts"}}
|
package/eid.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// A key's identity: the kind and the value.
|
|
2
|
+
//
|
|
3
|
+
// A key entity is content-addressed, the way an edge's id is derived from the
|
|
4
|
+
// two entities it links and a blob's from the hash of its bytes. Here the
|
|
5
|
+
// content is the pair — kind, value — so two writers giving the same value land
|
|
6
|
+
// on one entity, a writer retiring a value can name its entity without a
|
|
7
|
+
// lookup, and the uniqueness of a value within its kind is a fact about ids
|
|
8
|
+
// rather than a constraint somebody has to remember to declare.
|
|
9
|
+
//
|
|
10
|
+
// This function is the only place the derivation is written, because an id
|
|
11
|
+
// computed two ways is two ids.
|
|
12
|
+
import { comps } from "@yaks/graph/bundle";
|
|
13
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
14
|
+
import { VALUE } from "./comp.js";
|
|
15
|
+
/**
|
|
16
|
+
* The eid a value lands on: `sha256("<kind>|<value>")` formatted as a UUID
|
|
17
|
+
* (@yaks/graph `derivedEid` — the one derivation every content-addressed
|
|
18
|
+
* component shares, so an id computed here and an id computed elsewhere are the
|
|
19
|
+
* same id).
|
|
20
|
+
*
|
|
21
|
+
* `kind` is the tag component the key carries, not the name a query uses for
|
|
22
|
+
* it: the id is derived from what the entity carries.
|
|
23
|
+
*/ export let keyEid = (kind, value)=>derivedEid(`${kind}|${value}`);
|
|
24
|
+
/**
|
|
25
|
+
* The kind tag a bundle carries, or nothing when it carries none. `tags` is the
|
|
26
|
+
* vocabulary's tag → name map ({@link names}); only a declared kind counts, so
|
|
27
|
+
* an ordinary component stored beside the key is not mistaken for one.
|
|
28
|
+
*/ export let tagOf = (bundle, tags)=>comps(bundle).find(([name, comp])=>comp && tags[name])?.[0];
|
|
29
|
+
/**
|
|
30
|
+
* How the `key` component derives its own entity's id — the {@link Derive} a
|
|
31
|
+
* graph calls when a key bundle arrives under a `$alias`, so the write that
|
|
32
|
+
* claims a value also learns the id it landed on.
|
|
33
|
+
*
|
|
34
|
+
* A key missing its kind or its value derives nothing (it returns `''`) and the
|
|
35
|
+
* entity takes an ordinary minted id, at which point the {@link stated} hook
|
|
36
|
+
* refuses the write and reports which part was missing — a much better error
|
|
37
|
+
* than a key whose id was derived from half of itself.
|
|
38
|
+
*/ export let derive = (tags)=>(comp, bundle)=>{
|
|
39
|
+
let tag = tagOf(bundle, tags);
|
|
40
|
+
let value = comp[VALUE];
|
|
41
|
+
return tag && typeof value == 'string' && value ? keyEid(tag, value) : '';
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=eid.js.map
|
package/eid.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eid.js","sources":["./eid.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,EAAE;AACF,8EAA8E;AAC9E,0EAA0E;AAC1E,gFAAgF;AAChF,yEAAyE;AACzE,4EAA4E;AAC5E,gEAAgE;AAChE,EAAE;AACF,2EAA2E;AAC3E,gCAAgC;AAGhC,SAAS,KAAK,kCAAuC;AACrD,SAAS,UAAU,oCAAyC;AAC5D,SAAS,KAAK,oBAAmB;AAEjC;;;;;;;;CAQC,GACD,OAAO,IAAI,SAAS,CAAC,MAAc,QACjC,WAAW,GAAG,KAAK,CAAC,EAAE,OAAO,EAAC;AAEhC;;;;CAIC,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;;;;;;;;;CASC,GACD,OAAO,IAAI,SACT,CAAC,OAAyC,CAAC,MAAY;IACrD,IAAI,MAAM,MAAM,QAAQ;IACxB,IAAI,QAAQ,IAAI,CAAC,MAAM;IACvB,OAAO,OAAO,OAAO,SAAS,YAAY,QAAQ,OAAO,KAAK,SAAS;EACzE,EAAC"}
|
package/eid.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// A key's identity: the kind and the value.
|
|
2
|
+
//
|
|
3
|
+
// A key entity is content-addressed, the way an edge's id is derived from the
|
|
4
|
+
// two entities it links and a blob's from the hash of its bytes. Here the
|
|
5
|
+
// content is the pair — kind, value — so two writers giving the same value land
|
|
6
|
+
// on one entity, a writer retiring a value can name its entity without a
|
|
7
|
+
// lookup, and the uniqueness of a value within its kind is a fact about ids
|
|
8
|
+
// rather than a constraint somebody has to remember to declare.
|
|
9
|
+
//
|
|
10
|
+
// This function is the only place the derivation is written, because an id
|
|
11
|
+
// computed two ways is two ids.
|
|
12
|
+
|
|
13
|
+
import type { Bundle, Comp, Derive, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import { comps } from '@yaks/graph/bundle'
|
|
15
|
+
import { derivedEid } from '@yaks/graph/identity'
|
|
16
|
+
import { VALUE } from './comp.js'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The eid a value lands on: `sha256("<kind>|<value>")` formatted as a UUID
|
|
20
|
+
* (@yaks/graph `derivedEid` — the one derivation every content-addressed
|
|
21
|
+
* component shares, so an id computed here and an id computed elsewhere are the
|
|
22
|
+
* same id).
|
|
23
|
+
*
|
|
24
|
+
* `kind` is the tag component the key carries, not the name a query uses for
|
|
25
|
+
* it: the id is derived from what the entity carries.
|
|
26
|
+
*/
|
|
27
|
+
export let keyEid = (kind: string, value: string): Eid =>
|
|
28
|
+
derivedEid(`${kind}|${value}`)
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The kind tag a bundle carries, or nothing when it carries none. `tags` is the
|
|
32
|
+
* vocabulary's tag → name map ({@link names}); only a declared kind counts, so
|
|
33
|
+
* an ordinary component stored beside the key is not mistaken for one.
|
|
34
|
+
*/
|
|
35
|
+
export let tagOf = (
|
|
36
|
+
bundle: Bundle,
|
|
37
|
+
tags: Record<string, string>,
|
|
38
|
+
): string | undefined =>
|
|
39
|
+
comps(bundle).find(([name, comp]) => comp && tags[name])?.[0]
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* How the `key` component derives its own entity's id — the {@link Derive} a
|
|
43
|
+
* graph calls when a key bundle arrives under a `$alias`, so the write that
|
|
44
|
+
* claims a value also learns the id it landed on.
|
|
45
|
+
*
|
|
46
|
+
* A key missing its kind or its value derives nothing (it returns `''`) and the
|
|
47
|
+
* entity takes an ordinary minted id, at which point the {@link stated} hook
|
|
48
|
+
* refuses the write and reports which part was missing — a much better error
|
|
49
|
+
* than a key whose id was derived from half of itself.
|
|
50
|
+
*/
|
|
51
|
+
export let derive =
|
|
52
|
+
(tags: Record<string, string>): Derive => (comp: Comp, bundle: Bundle) => {
|
|
53
|
+
let tag = tagOf(bundle, tags)
|
|
54
|
+
let value = comp[VALUE]
|
|
55
|
+
return tag && typeof value == 'string' && value ? keyEid(tag, value) : ''
|
|
56
|
+
}
|
package/graph.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The graph plugin this package contributes, exported as `@yaks/key/graph` —
|
|
2
|
+
// the entry point a server imports to install it. A key's eid is
|
|
3
|
+
// `sha256("<kind>|<value>")`, so a value is unique within its kind by
|
|
4
|
+
// construction.
|
|
5
|
+
import { keys } from "./plugin.js";
|
|
6
|
+
/** Derived key ids, over the kinds this vocabulary declares. */ export let plugins = (host)=>[
|
|
7
|
+
keys(host.vocab)
|
|
8
|
+
];
|
|
9
|
+
//# 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,iEAAiE;AACjE,sEAAsE;AACtE,gBAAgB;AAIhB,SAAS,IAAI,sBAAqB;AAElC,8DAA8D,GAC9D,OAAO,IAAI,UAAU,CAAC,OAAqC;IAAC,KAAK,KAAK,KAAK;GAAE,CAAA"}
|
package/graph.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The graph plugin this package contributes, exported as `@yaks/key/graph` —
|
|
2
|
+
// the entry point a server imports to install it. A key's eid is
|
|
3
|
+
// `sha256("<kind>|<value>")`, so a value is unique within its kind by
|
|
4
|
+
// construction.
|
|
5
|
+
|
|
6
|
+
import type { Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
7
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
8
|
+
import { keys } from './plugin.js'
|
|
9
|
+
|
|
10
|
+
/** Derived key ids, over the kinds this vocabulary declares. */
|
|
11
|
+
export let plugins = (host: { vocab: Vocab }): Plugin[] => [keys(host.vocab)]
|
package/guard.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// The refusal: an incomplete key is not a key.
|
|
2
|
+
//
|
|
3
|
+
// A key is three things — a kind, a value, and the entity the value is for —
|
|
4
|
+
// and any two of them mean nothing. A bundle with a value and no tag would land
|
|
5
|
+
// as a row nothing can read; one with no `of` would identify nobody. Both are
|
|
6
|
+
// caught here, at the `mint` phase, which runs after the graph has assigned an
|
|
7
|
+
// id to every `$alias` — so the refusal can name the entity it is talking
|
|
8
|
+
// about, and an `of` written as an alias is already the id it resolved to.
|
|
9
|
+
//
|
|
10
|
+
// A bundle with neither a value nor an `of` is a patch of a key that already
|
|
11
|
+
// exists, and is left alone: it claims nothing, so it cannot claim half of
|
|
12
|
+
// something.
|
|
13
|
+
//
|
|
14
|
+
// Every bundle in the write is read before any check, because one entity may
|
|
15
|
+
// arrive as several bundles — the key in one, its tag in another — and together
|
|
16
|
+
// they are one key.
|
|
17
|
+
import { comps } from "@yaks/graph/bundle";
|
|
18
|
+
import { Refused } from "@yaks/graph/errors";
|
|
19
|
+
import { KEY, names } from "./kinds.js";
|
|
20
|
+
import { tagOf } from "./eid.js";
|
|
21
|
+
// Every bundle in the write merged per entity, so a key spread over several of
|
|
22
|
+
// them is checked as the one key it is.
|
|
23
|
+
export let gathered = (bundles)=>{
|
|
24
|
+
let out = new Map();
|
|
25
|
+
for (let b of bundles){
|
|
26
|
+
let at = out.get(b.entity.eid) ?? {
|
|
27
|
+
entity: b.entity
|
|
28
|
+
};
|
|
29
|
+
for (let [name, comp] of comps(b)){
|
|
30
|
+
let had = at[name];
|
|
31
|
+
at[name] = comp == null ? null : {
|
|
32
|
+
...had ?? {},
|
|
33
|
+
...comp
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
out.set(b.entity.eid, at);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The `mint` hook that refuses an incomplete key, naming what is missing.
|
|
42
|
+
* Registered by {@link https://jsr.io/@yaks/key/doc/~/keys | keys}; exported on
|
|
43
|
+
* its own for a graph that wants the check without the rest.
|
|
44
|
+
*/ export let stated = (vocab)=>{
|
|
45
|
+
let tags = names(vocab);
|
|
46
|
+
let known = Object.values(tags).sort();
|
|
47
|
+
return (bundles)=>{
|
|
48
|
+
for (let [eid, b] of gathered(bundles)){
|
|
49
|
+
let key = b[KEY];
|
|
50
|
+
// nothing claimed (a patch, or a bundle about something else entirely)
|
|
51
|
+
if (!key || key.of == null && key.value == null) continue;
|
|
52
|
+
for (let prop of [
|
|
53
|
+
'of',
|
|
54
|
+
'value'
|
|
55
|
+
]){
|
|
56
|
+
if (key[prop] == null) {
|
|
57
|
+
throw new Refused(`key ${eid} has no \`${prop}\``);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!tagOf(b, tags)) {
|
|
61
|
+
throw new Refused(`key ${eid} declares no kind — a key carries a kind tag beside ` + `key{of, value}${known.length ? ` (this vocabulary knows ${known.join(', ')})` : ''}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return bundles;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=guard.js.map
|
package/guard.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.js","sources":["./guard.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,8EAA8E;AAC9E,+EAA+E;AAC/E,0EAA0E;AAC1E,2EAA2E;AAC3E,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,aAAa;AACb,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,oBAAoB;AAGpB,SAAS,KAAK,kCAAuC;AACrD,SAAS,OAAO,kCAAuC;AAEvD,SAAS,GAAG,EAAE,KAAK,qBAAoB;AACvC,SAAS,KAAK,mBAAkB;AAEhC,+EAA+E;AAC/E,wCAAwC;AACxC,OAAO,IAAI,WAAW,CAAC;EACrB,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,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,EAAC;AAED;;;;CAIC,GACD,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,OAAO,MAAM;EACjB,IAAI,QAAQ,OAAO,MAAM,CAAC,MAAM,IAAI;EACpC,OAAO,CAAC;IACN,KAAK,IAAI,CAAC,KAAK,EAAE,IAAI,SAAS,SAAU;MACtC,IAAI,MAAM,CAAC,CAAC,IAAI;MAChB,uEAAuE;MACvE,IAAI,CAAC,OAAQ,IAAI,EAAE,IAAI,QAAQ,IAAI,KAAK,IAAI,MAAO;MACnD,KAAK,IAAI,QAAQ;QAAC;QAAM;OAAQ,CAAE;QAChC,IAAI,GAAG,CAAC,KAAK,IAAI,MAAM;UACrB,MAAM,IAAI,QAAQ,CAAC,IAAI,EAAE,IAAI,UAAU,EAAE,KAAK,EAAE,CAAC;QACnD;MACF;MACA,IAAI,CAAC,MAAM,GAAG,OAAO;QACnB,MAAM,IAAI,QACR,CAAC,IAAI,EAAE,IAAI,oDAAoD,CAAC,GAC9D,CAAC,cAAc,EACb,MAAM,MAAM,GAAG,CAAC,wBAAwB,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,IAChE;MAER;IACF;IACA,OAAO;EACT;AACF,EAAC"}
|
package/guard.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// The refusal: an incomplete key is not a key.
|
|
2
|
+
//
|
|
3
|
+
// A key is three things — a kind, a value, and the entity the value is for —
|
|
4
|
+
// and any two of them mean nothing. A bundle with a value and no tag would land
|
|
5
|
+
// as a row nothing can read; one with no `of` would identify nobody. Both are
|
|
6
|
+
// caught here, at the `mint` phase, which runs after the graph has assigned an
|
|
7
|
+
// id to every `$alias` — so the refusal can name the entity it is talking
|
|
8
|
+
// about, and an `of` written as an alias is already the id it resolved to.
|
|
9
|
+
//
|
|
10
|
+
// A bundle with neither a value nor an `of` is a patch of a key that already
|
|
11
|
+
// exists, and is left alone: it claims nothing, so it cannot claim half of
|
|
12
|
+
// something.
|
|
13
|
+
//
|
|
14
|
+
// Every bundle in the write is read before any check, because one entity may
|
|
15
|
+
// arrive as several bundles — the key in one, its tag in another — and together
|
|
16
|
+
// they are one key.
|
|
17
|
+
|
|
18
|
+
import type { Bundle, Comp, Eid, Hook } from 'jsr:@yaks/graph@0.2.36'
|
|
19
|
+
import { comps } from '@yaks/graph/bundle'
|
|
20
|
+
import { Refused } from '@yaks/graph/errors'
|
|
21
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
22
|
+
import { KEY, names } from './kinds.js'
|
|
23
|
+
import { tagOf } from './eid.js'
|
|
24
|
+
|
|
25
|
+
// Every bundle in the write merged per entity, so a key spread over several of
|
|
26
|
+
// them is checked as the one key it is.
|
|
27
|
+
export let gathered = (bundles: Bundle[]): Map<Eid, Bundle> => {
|
|
28
|
+
let out = new Map<Eid, Bundle>()
|
|
29
|
+
for (let b of bundles) {
|
|
30
|
+
let at = out.get(b.entity.eid) ?? { entity: b.entity }
|
|
31
|
+
for (let [name, comp] of comps(b)) {
|
|
32
|
+
let had = at[name] as Comp | null | undefined
|
|
33
|
+
at[name] = comp == null ? null : { ...(had ?? {}), ...comp }
|
|
34
|
+
}
|
|
35
|
+
out.set(b.entity.eid, at)
|
|
36
|
+
}
|
|
37
|
+
return out
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The `mint` hook that refuses an incomplete key, naming what is missing.
|
|
42
|
+
* Registered by {@link https://jsr.io/@yaks/key/doc/~/keys | keys}; exported on
|
|
43
|
+
* its own for a graph that wants the check without the rest.
|
|
44
|
+
*/
|
|
45
|
+
export let stated = (vocab: Vocab): Hook => {
|
|
46
|
+
let tags = names(vocab)
|
|
47
|
+
let known = Object.values(tags).sort()
|
|
48
|
+
return (bundles) => {
|
|
49
|
+
for (let [eid, b] of gathered(bundles)) {
|
|
50
|
+
let key = b[KEY] as Comp | null | undefined
|
|
51
|
+
// nothing claimed (a patch, or a bundle about something else entirely)
|
|
52
|
+
if (!key || (key.of == null && key.value == null)) continue
|
|
53
|
+
for (let prop of ['of', 'value']) {
|
|
54
|
+
if (key[prop] == null) {
|
|
55
|
+
throw new Refused(`key ${eid} has no \`${prop}\``)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!tagOf(b, tags)) {
|
|
59
|
+
throw new Refused(
|
|
60
|
+
`key ${eid} declares no kind — a key carries a kind tag beside ` +
|
|
61
|
+
`key{of, value}${
|
|
62
|
+
known.length ? ` (this vocabulary knows ${known.join(', ')})` : ''
|
|
63
|
+
}`,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return bundles
|
|
68
|
+
}
|
|
69
|
+
}
|
package/keywords.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The registration: the custom JSON Schema keyword @yaks/key adds to a
|
|
2
|
+
// component vocabulary, declared in JSON Schema's own extension form.
|
|
3
|
+
// `meta/key.vocab.json` is the authored source (the keyword's schema and the
|
|
4
|
+
// prose describing it); this module gives it a name and the shape
|
|
5
|
+
// `loadVocab(docs, [keyKeywords])` expects.
|
|
6
|
+
//
|
|
7
|
+
// One keyword, because one thing is open: which kinds of value exist. The `key`
|
|
8
|
+
// component itself is fixed and ships with the package; the tags a key carries
|
|
9
|
+
// are the application's — a store identifies its recipes by a short name, a
|
|
10
|
+
// directory its people by email address — so they arrive as ordinary components
|
|
11
|
+
// declaring `key` about themselves.
|
|
12
|
+
//
|
|
13
|
+
// The keyword is named after the carrier it tags (Jeff, 2026-09-05: "why not
|
|
14
|
+
// edge:true and key:true?"). A tag that goes on an edge declares `edge`, a tag
|
|
15
|
+
// that goes on a key declares `key`, and one rule — "this tag belongs to that
|
|
16
|
+
// component" — reads them all. The two packages do not share a helper to read
|
|
17
|
+
// it: it is eight lines each, and a shared one would need a home package that
|
|
18
|
+
// neither of them is.
|
|
19
|
+
import doc from './meta/key.vocab.json' with {
|
|
20
|
+
type: 'json'
|
|
21
|
+
};
|
|
22
|
+
/** The URI a vocabulary file declares under `$vocabulary` to use `key`. */ export let KEY_URI = 'https://yak.sh/vocab/key';
|
|
23
|
+
/**
|
|
24
|
+
* The `key` keyword vocabulary, ready to register:
|
|
25
|
+
* `loadVocab(docs, [keyKeywords])` carries each component's declaration onto
|
|
26
|
+
* `v.comp(name).keywords.key`, which is where {@link kinds} reads it.
|
|
27
|
+
*/ export let keyKeywords = {
|
|
28
|
+
uri: KEY_URI,
|
|
29
|
+
comp: [
|
|
30
|
+
'key'
|
|
31
|
+
],
|
|
32
|
+
doc
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=keywords.js.map
|
package/keywords.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keywords.js","sources":["./keywords.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,sEAAsE;AACtE,6EAA6E;AAC7E,kEAAkE;AAClE,4CAA4C;AAC5C,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,gFAAgF;AAChF,oCAAoC;AACpC,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,sBAAsB;AAGtB,OAAO,SAAS,6BAA6B;EAAE,MAAM;AAAO,EAAC;AAE7D,yEAAyE,GACzE,OAAO,IAAI,UAAU,2BAA0B;AAE/C;;;;CAIC,GACD,OAAO,IAAI,cAAwB;EAAE,KAAK;EAAS,MAAM;IAAC;GAAM;EAAE;AAAI,EAAC"}
|
package/keywords.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The registration: the custom JSON Schema keyword @yaks/key adds to a
|
|
2
|
+
// component vocabulary, declared in JSON Schema's own extension form.
|
|
3
|
+
// `meta/key.vocab.json` is the authored source (the keyword's schema and the
|
|
4
|
+
// prose describing it); this module gives it a name and the shape
|
|
5
|
+
// `loadVocab(docs, [keyKeywords])` expects.
|
|
6
|
+
//
|
|
7
|
+
// One keyword, because one thing is open: which kinds of value exist. The `key`
|
|
8
|
+
// component itself is fixed and ships with the package; the tags a key carries
|
|
9
|
+
// are the application's — a store identifies its recipes by a short name, a
|
|
10
|
+
// directory its people by email address — so they arrive as ordinary components
|
|
11
|
+
// declaring `key` about themselves.
|
|
12
|
+
//
|
|
13
|
+
// The keyword is named after the carrier it tags (Jeff, 2026-09-05: "why not
|
|
14
|
+
// edge:true and key:true?"). A tag that goes on an edge declares `edge`, a tag
|
|
15
|
+
// that goes on a key declares `key`, and one rule — "this tag belongs to that
|
|
16
|
+
// component" — reads them all. The two packages do not share a helper to read
|
|
17
|
+
// it: it is eight lines each, and a shared one would need a home package that
|
|
18
|
+
// neither of them is.
|
|
19
|
+
|
|
20
|
+
import type { Keywords } from 'jsr:@yaks/vocab@0.2.36'
|
|
21
|
+
import doc from './meta/key.vocab.json' with { type: 'json' }
|
|
22
|
+
|
|
23
|
+
/** The URI a vocabulary file declares under `$vocabulary` to use `key`. */
|
|
24
|
+
export let KEY_URI = 'https://yak.sh/vocab/key'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `key` keyword vocabulary, ready to register:
|
|
28
|
+
* `loadVocab(docs, [keyKeywords])` carries each component's declaration onto
|
|
29
|
+
* `v.comp(name).keywords.key`, which is where {@link kinds} reads it.
|
|
30
|
+
*/
|
|
31
|
+
export let keyKeywords: Keywords = { uri: KEY_URI, comp: ['key'], doc }
|
package/kinds.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The `key` keyword, interpreted: which components tag a key, and what each of
|
|
2
|
+
// them is called.
|
|
3
|
+
//
|
|
4
|
+
// The kinds are not a fixed list this package ships. An application declares as
|
|
5
|
+
// many as it has — a store's `alias`, a directory's `email`, a library's `isbn`
|
|
6
|
+
// — and each is an ordinary component declaring `key` about itself. Everything
|
|
7
|
+
// here reads that declaration off a loaded vocabulary; nothing is hardcoded, so
|
|
8
|
+
// adding a kind is one component declaration, not an edit here.
|
|
9
|
+
//
|
|
10
|
+
// A kind has two names, and they may differ. The tag is the component a key
|
|
11
|
+
// entity carries; the name is what a query uses. Declaring `key: true` makes
|
|
12
|
+
// them the same, which is the common case; declaring a string names the kind
|
|
13
|
+
// separately. The key's id is derived from the tag, so the two maps below are
|
|
14
|
+
// not interchangeable.
|
|
15
|
+
/** The component every key carries: the entity it identifies, and the
|
|
16
|
+
* value. */ export let KEY = 'key';
|
|
17
|
+
/**
|
|
18
|
+
* Every kind the vocabulary declares, as name → tag component:
|
|
19
|
+
* `{ alias: 'alias', mailbox: 'email' }`. It reads the `key` keyword, so the
|
|
20
|
+
* vocabulary must have been loaded with `keyKeywords` registered — an
|
|
21
|
+
* unregistered keyword is invisible to the loader.
|
|
22
|
+
*/ export let kinds = (v)=>{
|
|
23
|
+
let out = {};
|
|
24
|
+
for (let tag of v.all){
|
|
25
|
+
let said = v.comp(tag)?.keywords.key;
|
|
26
|
+
if (said === true) out[tag] = tag;
|
|
27
|
+
else if (typeof said == 'string' && said) out[said] = tag;
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The same declarations the other way round, as tag component → name. This is
|
|
33
|
+
* what reads a key back: a bundle carrying `email` holds a `mailbox` value.
|
|
34
|
+
*/ export let names = (v)=>{
|
|
35
|
+
let out = {};
|
|
36
|
+
for (let [name, tag] of Object.entries(kinds(v)))out[tag] = name;
|
|
37
|
+
return out;
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=kinds.js.map
|
package/kinds.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kinds.js","sources":["./kinds.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kBAAkB;AAClB,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,+EAA+E;AAC/E,gFAAgF;AAChF,gEAAgE;AAChE,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,uBAAuB;AAIvB;UACU,GACV,OAAO,IAAI,MAAM,MAAK;AAEtB;;;;;CAKC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,MAA8B,CAAC;EACnC,KAAK,IAAI,OAAO,EAAE,GAAG,CAAE;IACrB,IAAI,OAAO,EAAE,IAAI,CAAC,MAAM,SAAS;IACjC,IAAI,SAAS,MAAM,GAAG,CAAC,IAAI,GAAG;SACzB,IAAI,OAAO,QAAQ,YAAY,MAAM,GAAG,CAAC,KAAK,GAAG;EACxD;EACA,OAAO;AACT,EAAC;AAED;;;CAGC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,MAA8B,CAAC;EACnC,KAAK,IAAI,CAAC,MAAM,IAAI,IAAI,OAAO,OAAO,CAAC,MAAM,IAAK,GAAG,CAAC,IAAI,GAAG;EAC7D,OAAO;AACT,EAAC"}
|
package/kinds.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The `key` keyword, interpreted: which components tag a key, and what each of
|
|
2
|
+
// them is called.
|
|
3
|
+
//
|
|
4
|
+
// The kinds are not a fixed list this package ships. An application declares as
|
|
5
|
+
// many as it has — a store's `alias`, a directory's `email`, a library's `isbn`
|
|
6
|
+
// — and each is an ordinary component declaring `key` about itself. Everything
|
|
7
|
+
// here reads that declaration off a loaded vocabulary; nothing is hardcoded, so
|
|
8
|
+
// adding a kind is one component declaration, not an edit here.
|
|
9
|
+
//
|
|
10
|
+
// A kind has two names, and they may differ. The tag is the component a key
|
|
11
|
+
// entity carries; the name is what a query uses. Declaring `key: true` makes
|
|
12
|
+
// them the same, which is the common case; declaring a string names the kind
|
|
13
|
+
// separately. The key's id is derived from the tag, so the two maps below are
|
|
14
|
+
// not interchangeable.
|
|
15
|
+
|
|
16
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
17
|
+
|
|
18
|
+
/** The component every key carries: the entity it identifies, and the
|
|
19
|
+
* value. */
|
|
20
|
+
export let KEY = 'key'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Every kind the vocabulary declares, as name → tag component:
|
|
24
|
+
* `{ alias: 'alias', mailbox: 'email' }`. It reads the `key` keyword, so the
|
|
25
|
+
* vocabulary must have been loaded with `keyKeywords` registered — an
|
|
26
|
+
* unregistered keyword is invisible to the loader.
|
|
27
|
+
*/
|
|
28
|
+
export let kinds = (v: Vocab): Record<string, string> => {
|
|
29
|
+
let out: Record<string, string> = {}
|
|
30
|
+
for (let tag of v.all) {
|
|
31
|
+
let said = v.comp(tag)?.keywords.key
|
|
32
|
+
if (said === true) out[tag] = tag
|
|
33
|
+
else if (typeof said == 'string' && said) out[said] = tag
|
|
34
|
+
}
|
|
35
|
+
return out
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The same declarations the other way round, as tag component → name. This is
|
|
40
|
+
* what reads a key back: a bundle carrying `email` holds a `mailbox` value.
|
|
41
|
+
*/
|
|
42
|
+
export let names = (v: Vocab): Record<string, string> => {
|
|
43
|
+
let out: Record<string, string> = {}
|
|
44
|
+
for (let [name, tag] of Object.entries(kinds(v))) out[tag] = name
|
|
45
|
+
return out
|
|
46
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://yak.sh/vocab/key",
|
|
4
|
+
"title": "yaks key keyword vocabulary",
|
|
5
|
+
"description": "The keyword @yaks/key adds to a component vocabulary. A component declaring `key` is a tag that goes on the `key{of, value}` carrier: a key entity carrying both holds a value unique among that tag's values, so an `email` component beside `key{of, value}` records that a person is identified by that address. The keyword is named after the carrier it tags — a tag on an edge declares `edge`, a tag on a key declares `key` — so one rule reads every carrier. Which tags exist is the application's to declare.",
|
|
6
|
+
"$comment": "A vocabulary declaration in the JSON Schema sense: a URI naming this keyword set, plus the schema of each keyword. A vocabulary file declares it under $vocabulary; a loader registers it with loadVocab(docs, [keyKeywords]).",
|
|
7
|
+
"$defs": {
|
|
8
|
+
"key": {
|
|
9
|
+
"description": "This component tags a key with the kind of value it holds. `true` names the kind after the component itself (an `email` component is the `email` kind); a string names it something else, for a vocabulary where the component written and the kind queries name differ.",
|
|
10
|
+
"type": ["boolean", "string"]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/key — the values that identify an entity, stored as entities.
|
|
3
|
+
*
|
|
4
|
+
* A value that identifies something is not a property on the thing it
|
|
5
|
+
* identifies; it is an entity of its own, carrying the `key{of, value}`
|
|
6
|
+
* component and a kind tag component naming what sort of value it is. A store
|
|
7
|
+
* identifies its recipes by a short name (`alias`), a directory its people by
|
|
8
|
+
* address (`email`), a library its books by `isbn`; the structure is the same,
|
|
9
|
+
* and the kinds are yours to declare — this package ships the carrier and the
|
|
10
|
+
* mechanism and not one kind.
|
|
11
|
+
*
|
|
12
|
+
* It is to a has-many value exactly what
|
|
13
|
+
* {@link https://jsr.io/@yaks/edge | @yaks/edge} is to a link: one generic
|
|
14
|
+
* carrier component, tagged by the application's own components, with the
|
|
15
|
+
* entity's id derived from what it holds — so an entity has as many values as
|
|
16
|
+
* you write, writing one twice writes one row, and retiring one means deleting
|
|
17
|
+
* that row.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { graph } from '@yaks/graph'
|
|
21
|
+
* import { ram } from '@yaks/ram'
|
|
22
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
23
|
+
* import { keyDoc, keyed, keyKeywords, keys } from '@yaks/key'
|
|
24
|
+
*
|
|
25
|
+
* let library = {
|
|
26
|
+
* $defs: {
|
|
27
|
+
* book: { component: true, kind: true, properties: {} },
|
|
28
|
+
* // one component, and `isbn` is a kind of value
|
|
29
|
+
* isbn: { component: true, key: true },
|
|
30
|
+
* },
|
|
31
|
+
* }
|
|
32
|
+
* let vocab = loadVocab([keyDoc, library], [keyKeywords])
|
|
33
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [keys(vocab)] })
|
|
34
|
+
* g.apply([
|
|
35
|
+
* { entity: { eid: 'b1' }, book: {} },
|
|
36
|
+
* keyed('isbn', 'b1', '9780441013593'),
|
|
37
|
+
* ])
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Four things follow from that:
|
|
41
|
+
*
|
|
42
|
+
* - **A key's id is derived from the value.** {@link keyEid} hashes the kind
|
|
43
|
+
* and the value, so two writers giving the same value land on one entity, a
|
|
44
|
+
* value is unique within its kind by construction, and reading one back is a
|
|
45
|
+
* `get` rather than a query.
|
|
46
|
+
* - **A key lives only as long as what it identifies.** `of` is a reference
|
|
47
|
+
* declared `death: release`, so deleting a book removes its isbn row and
|
|
48
|
+
* frees the value.
|
|
49
|
+
* - **An incomplete key is refused**, naming what is missing: a key with no
|
|
50
|
+
* kind, no value or no `of` never reaches storage.
|
|
51
|
+
* - **Claiming a value somebody already holds lands on the holder.** A write
|
|
52
|
+
* that mints an entity under a `$alias` and gives it a value another entity
|
|
53
|
+
* already holds patches that entity instead of creating a second one; a
|
|
54
|
+
* caller who wrote an id down rather than using an alias is refused, with the
|
|
55
|
+
* holder named. An explicit deletion of the holder in the same batch permits
|
|
56
|
+
* a transfer, guarded against ownership changing before that batch commits.
|
|
57
|
+
*
|
|
58
|
+
* It imports no platform API, so the same code runs on a server, in a worker,
|
|
59
|
+
* and in a browser tab.
|
|
60
|
+
*
|
|
61
|
+
* @module
|
|
62
|
+
*/ export * from "./keywords.js";
|
|
63
|
+
export * from "./kinds.js";
|
|
64
|
+
export * from "./comp.js";
|
|
65
|
+
export * from "./eid.js";
|
|
66
|
+
export * from "./say.js";
|
|
67
|
+
export * from "./guard.js";
|
|
68
|
+
export * from "./resolve.js";
|
|
69
|
+
export * from "./plugin.js";
|
|
70
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6DC,GAED,8BAA6B;AAC7B,2BAA0B;AAC1B,0BAAyB;AACzB,yBAAwB;AACxB,yBAAwB;AACxB,2BAA0B;AAC1B,6BAA4B;AAC5B,4BAA2B"}
|