foldkit-entity 0.14.0 → 0.15.0

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 (93) hide show
  1. package/README.md +139 -5
  2. package/dist/collation.d.ts +33 -2
  3. package/dist/collation.d.ts.map +1 -0
  4. package/dist/conformance/index.d.ts +4 -4
  5. package/dist/conformance/index.d.ts.map +1 -1
  6. package/dist/conformance/index.js +5 -7
  7. package/dist/conformance/index.js.map +1 -1
  8. package/dist/conformance/operator.d.ts +1 -2
  9. package/dist/conformance/operator.d.ts.map +1 -1
  10. package/dist/conformance/operator.js +2 -6
  11. package/dist/conformance/operator.js.map +1 -1
  12. package/dist/conformance/relational.d.ts +17 -17
  13. package/dist/conformance/relational.d.ts.map +1 -1
  14. package/dist/conformance/relational.js +1 -3
  15. package/dist/conformance/relational.js.map +1 -1
  16. package/dist/conformance/variants.d.ts +47 -0
  17. package/dist/conformance/variants.d.ts.map +1 -0
  18. package/dist/conformance/variants.js +126 -0
  19. package/dist/conformance/variants.js.map +1 -0
  20. package/dist/context.d.ts +60 -2
  21. package/dist/context.d.ts.map +1 -0
  22. package/dist/descriptor.d.ts +33 -0
  23. package/dist/descriptor.d.ts.map +1 -0
  24. package/dist/descriptor.js +137 -0
  25. package/dist/descriptor.js.map +1 -0
  26. package/dist/entity.d.ts +231 -0
  27. package/dist/entity.d.ts.map +1 -0
  28. package/dist/entity.js +435 -0
  29. package/dist/entity.js.map +1 -0
  30. package/dist/evaluate.d.ts +204 -2
  31. package/dist/evaluate.d.ts.map +1 -0
  32. package/dist/evaluate.js +51 -7
  33. package/dist/evaluate.js.map +1 -1
  34. package/dist/expr.d.ts +915 -2
  35. package/dist/expr.d.ts.map +1 -0
  36. package/dist/expr.js +50 -10
  37. package/dist/expr.js.map +1 -1
  38. package/dist/held.d.ts +112 -2
  39. package/dist/held.d.ts.map +1 -0
  40. package/dist/held.js +13 -2
  41. package/dist/held.js.map +1 -1
  42. package/dist/ids.d.ts +8 -0
  43. package/dist/ids.d.ts.map +1 -0
  44. package/dist/ids.js +8 -0
  45. package/dist/ids.js.map +1 -0
  46. package/dist/index.d.ts +380 -6
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +108 -12
  49. package/dist/index.js.map +1 -0
  50. package/dist/invariant.d.ts +123 -2
  51. package/dist/invariant.d.ts.map +1 -0
  52. package/dist/invariant.js +236 -1
  53. package/dist/invariant.js.map +1 -0
  54. package/dist/law.d.ts +264 -2
  55. package/dist/law.d.ts.map +1 -0
  56. package/dist/law.js +1 -1
  57. package/dist/operators.d.ts +170 -2
  58. package/dist/operators.d.ts.map +1 -0
  59. package/dist/operators.js +3 -3
  60. package/dist/readFilter.d.ts +8 -2
  61. package/dist/readFilter.d.ts.map +1 -0
  62. package/dist/sets.d.ts +18 -2
  63. package/dist/sets.d.ts.map +1 -0
  64. package/dist/sets.js +1 -1
  65. package/dist/shape.d.ts +23 -2
  66. package/dist/shape.d.ts.map +1 -0
  67. package/dist/tested.d.ts +8 -2
  68. package/dist/tested.d.ts.map +1 -0
  69. package/dist/text.d.ts +27 -2
  70. package/dist/text.d.ts.map +1 -0
  71. package/dist/text.js +1 -1
  72. package/dist/variants.d.ts +72 -0
  73. package/dist/variants.d.ts.map +1 -0
  74. package/dist/variants.js +76 -0
  75. package/dist/variants.js.map +1 -0
  76. package/dist/words.d.ts +17 -2
  77. package/dist/words.d.ts.map +1 -0
  78. package/dist/write.d.ts +312 -2
  79. package/dist/write.d.ts.map +1 -0
  80. package/dist/write.js +3 -3
  81. package/package.json +3 -3
  82. package/dist/collation-BB4YTIbY.d.ts +0 -34
  83. package/dist/collation-BB4YTIbY.d.ts.map +0 -1
  84. package/dist/index-48JeXG0A.d.ts +0 -2585
  85. package/dist/index-48JeXG0A.d.ts.map +0 -1
  86. package/dist/readFilter-CWMnFRVw.d.ts +0 -9
  87. package/dist/readFilter-CWMnFRVw.d.ts.map +0 -1
  88. package/dist/shape-LAowRf9f.d.ts +0 -23
  89. package/dist/shape-LAowRf9f.d.ts.map +0 -1
  90. package/dist/src-DKhUqLoC.js +0 -731
  91. package/dist/src-DKhUqLoC.js.map +0 -1
  92. package/dist/words-a4e9o-TK.d.ts +0 -18
  93. package/dist/words-a4e9o-TK.d.ts.map +0 -1
package/dist/held.d.ts CHANGED
@@ -1,2 +1,112 @@
1
- import { an as Pointing, in as HeldTable, nn as HeldRow, on as idOfRef, rn as HeldRows, tn as Held } from "./index-48JeXG0A.js";
2
- export { Held, HeldRow, HeldRows, HeldTable, Pointing, idOfRef };
1
+ import { Option } from "effect";
2
+ import { AnyEntity, EntityIdentity } from "./index.js";
3
+ import { AnyQuery as AnyQuery$1, OneRelation } from "./expr.js";
4
+ /** A value a holder has, or why it does not have it. */
5
+ export type Held<A> = {
6
+ readonly _tag: 'Held';
7
+ readonly value: A;
8
+ } | {
9
+ readonly _tag: 'Unheld';
10
+ readonly reason: string;
11
+ };
12
+ /** The rows pointing at one row through a relation, as far as a holder has them. */
13
+ export interface Pointing {
14
+ readonly rows: ReadonlyArray<HeldRow>;
15
+ /**
16
+ * Why a row pointing here may be missing from `rows`; none when it holds
17
+ * every one whose fields hold the values `pinned` names.
18
+ */
19
+ readonly missing: Option.Option<string>;
20
+ }
21
+ /**
22
+ * The values an exists' filter fixes on the rows it counts, by field key (a
23
+ * link as the id it points at): a row with another value in one of them is
24
+ * not counted, so a holder holding every pointing row with these values holds
25
+ * every one that counts (`Judge.pins`).
26
+ */
27
+ export type Pinned = ReadonlyMap<string, unknown>;
28
+ /**
29
+ * One row as a holder has it. Values are **encoded**, as a store keeps them
30
+ * and as `evaluate` reads a row.
31
+ */
32
+ export interface HeldRow {
33
+ /** A field's value; unheld when the holder lacks it or may not see it. */
34
+ readonly field: (key: string) => Held<unknown>;
35
+ /**
36
+ * The row a `one` relation of this row points at: none for a null link, or
37
+ * a row the reader may not see when the holder knows it holds every row it may.
38
+ */
39
+ readonly follow: (relation: OneRelation) => Held<Option.Option<HeldRow>>;
40
+ /**
41
+ * The rows pointing at this one through `relation`, a `one` relation of
42
+ * another Entity, for an exists whose filter fixes `pinned`.
43
+ */
44
+ readonly pointing: (relation: OneRelation, pinned: Pinned) => Pointing;
45
+ }
46
+ /** One Entity's rows, as `Held.rows` takes them. */
47
+ export interface HeldTable {
48
+ readonly entity: AnyEntity;
49
+ /**
50
+ * Each row's values by key, encoded; a `one` relation's under its own key,
51
+ * as the id of the row it points at or null. A key a row lacks is unheld.
52
+ */
53
+ readonly rows: ReadonlyArray<Readonly<Record<string, unknown>>>;
54
+ /**
55
+ * Whether these are every row of the Entity the reader may see, so a row
56
+ * not among them is known absent. True when not given.
57
+ */
58
+ readonly complete?: boolean | undefined;
59
+ /** Keys the reader may not see on any of these rows. */
60
+ readonly hidden?: ReadonlyArray<string> | undefined;
61
+ }
62
+ /** The rows `Held.rows` holds, by Entity. */
63
+ export interface HeldRows {
64
+ /** The row of `entity` with this id, when it is held. */
65
+ readonly row: (entity: AnyEntity, id: unknown) => Option.Option<HeldRow>;
66
+ /** Every held row of `entity`. */
67
+ readonly rows: (entity: AnyEntity) => ReadonlyArray<HeldRow>;
68
+ /**
69
+ * The row of `entity` with this id as the holder can say: none when it
70
+ * knows there is no such row it may see, unheld when it cannot tell.
71
+ */
72
+ readonly find: (entity: AnyEntity, id: unknown) => Held<Option.Option<HeldRow>>;
73
+ }
74
+ declare const held: <A>(value: A) => Held<A>;
75
+ declare const unheld: <A = never>(reason: string) => Held<A>;
76
+ /**
77
+ * Rows held as plain records: each table complete or not, with keys hidden.
78
+ * What a test, a conformance check or a server that has the rows in hand
79
+ * judges over.
80
+ */
81
+ declare const rowsOf: (tables: ReadonlyArray<HeldTable>) => HeldRows;
82
+ /**
83
+ * A row as a Source answers it and a Remote store keeps it, each `one`
84
+ * relation as its ref key (`'User:ada'`), given as an interpreter reads it:
85
+ * each link `query` reads as the id it points at. The row itself when the
86
+ * query reads no link.
87
+ */
88
+ declare const fromRefs: (query: AnyQuery$1, row: Readonly<Record<string, unknown>>) => Readonly<Record<string, unknown>>;
89
+ /**
90
+ * Whether `key` is a `one` relation of the Entity `owner` names: what a
91
+ * ref-keyed holder reads as the id it points at on a row it knows only by
92
+ * identity, as a row pointing through `HeldRow.pointing`'s relation is.
93
+ */
94
+ declare const isLink: (owner: EntityIdentity<string>, key: string) => boolean;
95
+ /** The id a ref key (`'User:ada'`) names, refusing what is none, as `where` holds it. */
96
+ export declare const idOfRef: (ref: unknown, where: string) => string;
97
+ export declare const Held: {
98
+ /** A value the holder has. */
99
+ of: typeof held;
100
+ /** A value the holder does not have, and why. */
101
+ unheld: typeof unheld;
102
+ match: <A, B>(value: Held<A>, cases: {
103
+ readonly onHeld: (value: A) => B;
104
+ readonly onUnheld: (reason: string) => B;
105
+ }) => B;
106
+ /** Rows held as plain records; see `HeldTable`. */
107
+ rows: typeof rowsOf;
108
+ fromRefs: typeof fromRefs;
109
+ isLink: typeof isLink;
110
+ };
111
+ export {};
112
+ //# sourceMappingURL=held.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"held.d.ts","names":["AnyQuery"],"sources":["../src/held.ts"],"mappings":";;;;YAgBY,KAAK;WACF;WAAuB,OAAO;;WAC9B;WAAyB;;;iBAGvB;WACN,MAAM,cAAc;;;;;WAKpB,SAAS,OAAO;;;;;;;;YASf,SAAS;;;;;iBAMJ;;WAEN,QAAQ,gBAAgB;;;;;WAKxB,SAAS,UAAU,gBAAgB,KAAK,OAAO,OAAO;;;;;WAKtD,WAAW,UAAU,aAAa,QAAQ,WAAW;;;iBAI/C;WACN,QAAQ;;;;;WAKR,MAAM,cAAc,SAAS;;;;;WAK7B;;WAEA,SAAS;;;iBAIH;;WAEN,MAAM,QAAQ,WAAW,gBAAgB,OAAO,OAAO;;WAEvD,OAAO,QAAQ,cAAc,cAAc;;;;;WAK3C,OAAO,QAAQ,WAAW,gBAAgB,KAAK,OAAO,OAAO;;cAGlE,OAAQ,GAAC,OAAS,MAAI,KAAK;cAC3B,SAAU,WAAS,mBAAmB,KAAK;;;;;;cAO3C,SAAM,QAAY,cAAc,eAAa;;;;;;;cAwH7C,WAAQ,OACLA,YAAQ,KACV,SAAS,6BACb,SAAS;;;;;;cAiBN,SAAM,OAAW,wBAAsB;;qBAMhC,UAAO,cAAgB;qBAUvB;;;;;EAKH,QAAA,GAAG,GAAC,OACH,KAAK,IAAE;aACI,SAAS,OAAO,MAAM;aAAY,WAAW,mBAAmB;QACjF"}
package/dist/held.js CHANGED
@@ -1,6 +1,7 @@
1
+ import { Option } from "effect";
1
2
  import { own, put } from "./record.js";
3
+ import { relationsOf } from "./descriptor.js";
2
4
  import { Query } from "./expr.js";
3
- import { Option } from "effect";
4
5
  // src/held.ts
5
6
  /**
6
7
  * What a holder of rows has: the world `Judge` reads an `Expr` over.
@@ -123,6 +124,15 @@ const fromRefs = (query, row) => {
123
124
  }
124
125
  return read;
125
126
  };
127
+ /**
128
+ * Whether `key` is a `one` relation of the Entity `owner` names: what a
129
+ * ref-keyed holder reads as the id it points at on a row it knows only by
130
+ * identity, as a row pointing through `HeldRow.pointing`'s relation is.
131
+ */
132
+ const isLink = (owner, key) => {
133
+ const member = own(relationsOf(owner), key);
134
+ return member?._tag === "Relation" && member.cardinality === "one";
135
+ };
126
136
  /** The id a ref key (`'User:ada'`) names, refusing what is none, as `where` holds it. */
127
137
  const idOfRef = (ref, where) => {
128
138
  const separator = typeof ref === "string" ? ref.indexOf(":") : -1;
@@ -137,7 +147,8 @@ const Held = {
137
147
  match: (value, cases) => value._tag === "Held" ? cases.onHeld(value.value) : cases.onUnheld(value.reason),
138
148
  /** Rows held as plain records; see `HeldTable`. */
139
149
  rows: rowsOf,
140
- fromRefs
150
+ fromRefs,
151
+ isLink
141
152
  };
142
153
  export { Held, idOfRef };
143
154
 
package/dist/held.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"held.js","names":[],"sources":["../src/held.ts"],"sourcesContent":["/**\n * What a holder of rows has: the world `Judge` reads an `Expr` over.\n *\n * A server's database holds every row, so whatever an expression asks has an\n * answer there. A client holds what it was sent: some rows, some fields of\n * them, and none it may not see. `HeldRow` is how a holder says which, so an\n * interpreter can tell \"this field is null\" from \"I do not have this field\",\n * which a plain record cannot: both read as a missing key.\n */\nimport { Option } from 'effect'\nimport { Query, type AnyQuery, type OneRelation } from './expr.js'\nimport type { AnyEntity, EntityIdentity, EntityMember } from './index.js'\nimport { own, put } from './record.js'\n\n/** A value a holder has, or why it does not have it. */\nexport type Held<A> =\n | { readonly _tag: 'Held'; readonly value: A }\n | { readonly _tag: 'Unheld'; readonly reason: string }\n\n/** The rows pointing at one row through a relation, as far as a holder has them. */\nexport interface Pointing {\n readonly rows: ReadonlyArray<HeldRow>\n /** Why a row pointing here may be missing from `rows`; none when it holds every one. */\n readonly missing: Option.Option<string>\n}\n\n/**\n * One row as a holder has it. Values are **encoded**, as a store keeps them\n * and as `evaluate` reads a row.\n */\nexport interface HeldRow {\n /** A field's value; unheld when the holder lacks it or may not see it. */\n readonly field: (key: string) => Held<unknown>\n /**\n * The row a `one` relation of this row points at: none for a null link, or\n * a row the reader may not see when the holder knows it holds every row it may.\n */\n readonly follow: (relation: OneRelation) => Held<Option.Option<HeldRow>>\n /** The rows pointing at this one through `relation`, a `one` relation of another Entity. */\n readonly pointing: (relation: OneRelation) => Pointing\n}\n\n/** One Entity's rows, as `Held.rows` takes them. */\nexport interface HeldTable {\n readonly entity: AnyEntity\n /**\n * Each row's values by key, encoded; a `one` relation's under its own key,\n * as the id of the row it points at or null. A key a row lacks is unheld.\n */\n readonly rows: ReadonlyArray<Readonly<Record<string, unknown>>>\n /**\n * Whether these are every row of the Entity the reader may see, so a row\n * not among them is known absent. True when not given.\n */\n readonly complete?: boolean | undefined\n /** Keys the reader may not see on any of these rows. */\n readonly hidden?: ReadonlyArray<string> | undefined\n}\n\n/** The rows `Held.rows` holds, by Entity. */\nexport interface HeldRows {\n /** The row of `entity` with this id, when it is held. */\n readonly row: (entity: AnyEntity, id: unknown) => Option.Option<HeldRow>\n /** Every held row of `entity`. */\n readonly rows: (entity: AnyEntity) => ReadonlyArray<HeldRow>\n /**\n * The row of `entity` with this id as the holder can say: none when it\n * knows there is no such row it may see, unheld when it cannot tell.\n */\n readonly find: (entity: AnyEntity, id: unknown) => Held<Option.Option<HeldRow>>\n}\n\nconst held = <A>(value: A): Held<A> => ({ _tag: 'Held', value })\nconst unheld = <A = never>(reason: string): Held<A> => ({ _tag: 'Unheld', reason })\n\n/**\n * Rows held as plain records: each table complete or not, with keys hidden.\n * What a test, a conformance check or a server that has the rows in hand\n * judges over.\n */\nconst rowsOf = (tables: ReadonlyArray<HeldTable>): HeldRows => {\n interface Table {\n readonly entity: AnyEntity\n readonly complete: boolean\n readonly hidden: ReadonlySet<string>\n readonly byId: ReadonlyMap<unknown, HeldRow>\n readonly views: ReadonlyArray<HeldRow>\n }\n const byToken = new Map<symbol, Table>()\n const tableOf = (identity: EntityIdentity<string>) =>\n Option.fromUndefinedOr(byToken.get(identity.token))\n const find = (target: AnyEntity, id: unknown): Held<Option.Option<HeldRow>> =>\n Option.match(tableOf(target.identity), {\n onNone: () => unheld(`no ${target.name} is held`),\n onSome: reached =>\n Option.match(Option.fromUndefinedOr(reached.byId.get(id)), {\n onSome: row => held(Option.some(row)),\n onNone: () =>\n reached.complete\n ? held(Option.none<HeldRow>())\n : unheld(`${target.name} ${String(id)} is not held`),\n }),\n })\n\n const view = (table: Table, record: Readonly<Record<string, unknown>>): HeldRow => {\n const read = (key: string): Held<unknown> =>\n table.hidden.has(key)\n ? unheld(`${table.entity.name}.${key} is hidden from this reader`)\n : Object.hasOwn(record, key)\n ? held(record[key])\n : unheld(`${table.entity.name}.${key} is not held`)\n return {\n field: read,\n follow: relation => {\n const link = read(relation.key)\n if (link._tag === 'Unheld') return link\n if (link.value === null || link.value === undefined) return held(Option.none())\n return find(relation.target(), link.value)\n },\n pointing: relation =>\n Option.match(tableOf(relation.owner), {\n onNone: () => ({\n rows: [],\n missing: Option.some(`no ${relation.owner.name} is held`),\n }),\n onSome: from => {\n const id = record.id\n const named = `${relation.owner.name}.${relation.key}`\n const rows: Array<HeldRow> = []\n let missing = from.complete\n ? Option.none<string>()\n : Option.some(`not every ${relation.owner.name} is held`)\n for (const each of from.views) {\n const link = each.field(relation.key)\n if (link._tag === 'Unheld') {\n missing = Option.orElse(missing, () => Option.some(link.reason))\n } else if (link.value === id) {\n rows.push(each)\n }\n }\n return { rows, missing: Option.map(missing, reason => `${named}: ${reason}`) }\n },\n }),\n }\n }\n\n for (const given of tables) {\n const views: Array<HeldRow> = []\n const byId = new Map<unknown, HeldRow>()\n const table: Table = {\n entity: given.entity,\n complete: given.complete ?? true,\n hidden: new Set(given.hidden ?? []),\n byId,\n views,\n }\n for (const record of given.rows) {\n const each = view(table, record)\n views.push(each)\n byId.set(record.id, each)\n }\n byToken.set(given.entity.identity.token, table)\n }\n\n return {\n row: (entity, id) =>\n Option.flatMap(tableOf(entity.identity), table => Option.fromUndefinedOr(table.byId.get(id))),\n rows: entity =>\n Option.match(tableOf(entity.identity), { onNone: () => [], onSome: table => table.views }),\n find,\n }\n}\n\nconst linksRead = new WeakMap<AnyQuery, ReadonlyArray<string>>()\n\n/**\n * The links a query reads on its own row: each relation of its Entity it\n * reads (a body reads only a `one` relation as a value). A relation it reads\n * existed when it was built, so what it reads is known once.\n */\nconst linksOf = (query: AnyQuery): ReadonlyArray<string> => {\n const known = linksRead.get(query)\n if (known !== undefined) return known\n const relations: Readonly<Record<string, EntityMember>> = query.entity.relations\n const links = Query.dependencies(query)\n .fields.filter(\n field =>\n field.owner.token === query.entity.identity.token && Object.hasOwn(relations, field.key),\n )\n .map(field => field.key)\n linksRead.set(query, links)\n return links\n}\n\n/**\n * A row as a Source answers it and a Remote store keeps it, each `one`\n * relation as its ref key (`'User:ada'`), given as an interpreter reads it:\n * each link `query` reads as the id it points at. The row itself when the\n * query reads no link.\n */\nconst fromRefs = (\n query: AnyQuery,\n row: Readonly<Record<string, unknown>>,\n): Readonly<Record<string, unknown>> => {\n const links = linksOf(query)\n if (links.length === 0) return row\n const read: Record<string, unknown> = { ...row }\n for (const key of links) {\n const ref = own(row, key)\n if (ref !== null && ref !== undefined)\n put(read, key, idOfRef(ref, `${query.entity.name}.${key}`))\n }\n return read\n}\n\n/** The id a ref key (`'User:ada'`) names, refusing what is none, as `where` holds it. */\nexport const idOfRef = (ref: unknown, where: string): string => {\n const separator = typeof ref === 'string' ? ref.indexOf(':') : -1\n if (separator === -1) {\n throw new Error(\n `[foldkit-entity] ${where} holds ${JSON.stringify(ref)}, which is no ref key (\"Entity:id\")`,\n )\n }\n return (ref as string).slice(separator + 1)\n}\n\nexport const Held = {\n /** A value the holder has. */\n of: held,\n /** A value the holder does not have, and why. */\n unheld,\n match: <A, B>(\n value: Held<A>,\n cases: { readonly onHeld: (value: A) => B; readonly onUnheld: (reason: string) => B },\n ): B => (value._tag === 'Held' ? cases.onHeld(value.value) : cases.onUnheld(value.reason)),\n /** Rows held as plain records; see `HeldTable`. */\n rows: rowsOf,\n fromRefs,\n}\n"],"mappings":";;;;;;;;;;;;;AAwEA,MAAM,QAAW,WAAuB;CAAE,MAAM;CAAQ;AAAM;AAC9D,MAAM,UAAqB,YAA6B;CAAE,MAAM;CAAU;AAAO;;;;;;AAOjF,MAAM,UAAU,WAA+C;CAQ7D,MAAM,0BAAU,IAAI,IAAmB;CACvC,MAAM,WAAW,aACf,OAAO,gBAAgB,QAAQ,IAAI,SAAS,KAAK,CAAC;CACpD,MAAM,QAAQ,QAAmB,OAC/B,OAAO,MAAM,QAAQ,OAAO,QAAQ,GAAG;EACrC,cAAc,OAAO,MAAM,OAAO,KAAK,SAAS;EAChD,SAAQ,YACN,OAAO,MAAM,OAAO,gBAAgB,QAAQ,KAAK,IAAI,EAAE,CAAC,GAAG;GACzD,SAAQ,QAAO,KAAK,OAAO,KAAK,GAAG,CAAC;GACpC,cACE,QAAQ,WACJ,KAAK,OAAO,KAAc,CAAC,IAC3B,OAAO,GAAG,OAAO,KAAK,GAAG,OAA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K,MAAM,OAAO,OAAO;EACvB,MAAM,MAAM,IAAI,KAAK,GAAG;EACxB,IAAI,QAAQ,QAAQ,QAAQ,KAAA,GAC1B,IAAI,MAAM,KAAK,QAAQ,KAAK,GAAG,MAAM,OAAO,KAAK,GAAG,KAAK,CAAC;CAC9D;CACA,OAAO;AACT;;AAGA,MAAa,WAAW,KAAc,UAA0B;CAC9D,MAAM,YAAY,OAAO,QAAQ,WAAW,IAAI,QAAQ,GAAG,IAAI;CAC/D,IAAI,cAAc,IAChB,MAAM,IAAI,MACR,oBAAoB,MAAM,SAAS,KAAK,UAAU,GAAG,EAAE,oCACzD;CAEF,OAAQ,IAAe,MAAM,YAAY,CAAC;AAC5C;AAEA,MAAa,OAAO;;CAElB,IAAI;;CAEJ;CACA,QACE,OACA,UACO,MAAM,SAAS,SAAS,MAAM,OAAO,MAAM,KAAK,IAAI,MAAM,SAAS,MAAM,MAAM;;CAExF,MAAM;CACN;AACF"}
1
+ {"version":3,"file":"held.js","names":[],"sources":["../src/held.ts"],"sourcesContent":["/**\n * What a holder of rows has: the world `Judge` reads an `Expr` over.\n *\n * A server's database holds every row, so whatever an expression asks has an\n * answer there. A client holds what it was sent: some rows, some fields of\n * them, and none it may not see. `HeldRow` is how a holder says which, so an\n * interpreter can tell \"this field is null\" from \"I do not have this field\",\n * which a plain record cannot: both read as a missing key.\n */\nimport { Option } from 'effect'\nimport { relationsOf } from './descriptor.js'\nimport { Query, type AnyQuery, type OneRelation } from './expr.js'\nimport type { AnyEntity, EntityIdentity, EntityMember } from './index.js'\nimport { own, put } from './record.js'\n\n/** A value a holder has, or why it does not have it. */\nexport type Held<A> =\n | { readonly _tag: 'Held'; readonly value: A }\n | { readonly _tag: 'Unheld'; readonly reason: string }\n\n/** The rows pointing at one row through a relation, as far as a holder has them. */\nexport interface Pointing {\n readonly rows: ReadonlyArray<HeldRow>\n /**\n * Why a row pointing here may be missing from `rows`; none when it holds\n * every one whose fields hold the values `pinned` names.\n */\n readonly missing: Option.Option<string>\n}\n\n/**\n * The values an exists' filter fixes on the rows it counts, by field key (a\n * link as the id it points at): a row with another value in one of them is\n * not counted, so a holder holding every pointing row with these values holds\n * every one that counts (`Judge.pins`).\n */\nexport type Pinned = ReadonlyMap<string, unknown>\n\n/**\n * One row as a holder has it. Values are **encoded**, as a store keeps them\n * and as `evaluate` reads a row.\n */\nexport interface HeldRow {\n /** A field's value; unheld when the holder lacks it or may not see it. */\n readonly field: (key: string) => Held<unknown>\n /**\n * The row a `one` relation of this row points at: none for a null link, or\n * a row the reader may not see when the holder knows it holds every row it may.\n */\n readonly follow: (relation: OneRelation) => Held<Option.Option<HeldRow>>\n /**\n * The rows pointing at this one through `relation`, a `one` relation of\n * another Entity, for an exists whose filter fixes `pinned`.\n */\n readonly pointing: (relation: OneRelation, pinned: Pinned) => Pointing\n}\n\n/** One Entity's rows, as `Held.rows` takes them. */\nexport interface HeldTable {\n readonly entity: AnyEntity\n /**\n * Each row's values by key, encoded; a `one` relation's under its own key,\n * as the id of the row it points at or null. A key a row lacks is unheld.\n */\n readonly rows: ReadonlyArray<Readonly<Record<string, unknown>>>\n /**\n * Whether these are every row of the Entity the reader may see, so a row\n * not among them is known absent. True when not given.\n */\n readonly complete?: boolean | undefined\n /** Keys the reader may not see on any of these rows. */\n readonly hidden?: ReadonlyArray<string> | undefined\n}\n\n/** The rows `Held.rows` holds, by Entity. */\nexport interface HeldRows {\n /** The row of `entity` with this id, when it is held. */\n readonly row: (entity: AnyEntity, id: unknown) => Option.Option<HeldRow>\n /** Every held row of `entity`. */\n readonly rows: (entity: AnyEntity) => ReadonlyArray<HeldRow>\n /**\n * The row of `entity` with this id as the holder can say: none when it\n * knows there is no such row it may see, unheld when it cannot tell.\n */\n readonly find: (entity: AnyEntity, id: unknown) => Held<Option.Option<HeldRow>>\n}\n\nconst held = <A>(value: A): Held<A> => ({ _tag: 'Held', value })\nconst unheld = <A = never>(reason: string): Held<A> => ({ _tag: 'Unheld', reason })\n\n/**\n * Rows held as plain records: each table complete or not, with keys hidden.\n * What a test, a conformance check or a server that has the rows in hand\n * judges over.\n */\nconst rowsOf = (tables: ReadonlyArray<HeldTable>): HeldRows => {\n interface Table {\n readonly entity: AnyEntity\n readonly complete: boolean\n readonly hidden: ReadonlySet<string>\n readonly byId: ReadonlyMap<unknown, HeldRow>\n readonly views: ReadonlyArray<HeldRow>\n }\n const byToken = new Map<symbol, Table>()\n const tableOf = (identity: EntityIdentity<string>) =>\n Option.fromUndefinedOr(byToken.get(identity.token))\n const find = (target: AnyEntity, id: unknown): Held<Option.Option<HeldRow>> =>\n Option.match(tableOf(target.identity), {\n onNone: () => unheld(`no ${target.name} is held`),\n onSome: reached =>\n Option.match(Option.fromUndefinedOr(reached.byId.get(id)), {\n onSome: row => held(Option.some(row)),\n onNone: () =>\n reached.complete\n ? held(Option.none<HeldRow>())\n : unheld(`${target.name} ${String(id)} is not held`),\n }),\n })\n\n const view = (table: Table, record: Readonly<Record<string, unknown>>): HeldRow => {\n const read = (key: string): Held<unknown> =>\n table.hidden.has(key)\n ? unheld(`${table.entity.name}.${key} is hidden from this reader`)\n : Object.hasOwn(record, key)\n ? held(record[key])\n : unheld(`${table.entity.name}.${key} is not held`)\n return {\n field: read,\n follow: relation => {\n const link = read(relation.key)\n if (link._tag === 'Unheld') return link\n if (link.value === null || link.value === undefined) return held(Option.none())\n return find(relation.target(), link.value)\n },\n pointing: relation =>\n Option.match(tableOf(relation.owner), {\n onNone: () => ({\n rows: [],\n missing: Option.some(`no ${relation.owner.name} is held`),\n }),\n onSome: from => {\n const id = record.id\n const named = `${relation.owner.name}.${relation.key}`\n const rows: Array<HeldRow> = []\n let missing = from.complete\n ? Option.none<string>()\n : Option.some(`not every ${relation.owner.name} is held`)\n for (const each of from.views) {\n const link = each.field(relation.key)\n if (link._tag === 'Unheld') {\n missing = Option.orElse(missing, () => Option.some(link.reason))\n } else if (link.value === id) {\n rows.push(each)\n }\n }\n return { rows, missing: Option.map(missing, reason => `${named}: ${reason}`) }\n },\n }),\n }\n }\n\n for (const given of tables) {\n const views: Array<HeldRow> = []\n const byId = new Map<unknown, HeldRow>()\n const table: Table = {\n entity: given.entity,\n complete: given.complete ?? true,\n hidden: new Set(given.hidden ?? []),\n byId,\n views,\n }\n for (const record of given.rows) {\n const each = view(table, record)\n views.push(each)\n byId.set(record.id, each)\n }\n byToken.set(given.entity.identity.token, table)\n }\n\n return {\n row: (entity, id) =>\n Option.flatMap(tableOf(entity.identity), table => Option.fromUndefinedOr(table.byId.get(id))),\n rows: entity =>\n Option.match(tableOf(entity.identity), { onNone: () => [], onSome: table => table.views }),\n find,\n }\n}\n\nconst linksRead = new WeakMap<AnyQuery, ReadonlyArray<string>>()\n\n/**\n * The links a query reads on its own row: each relation of its Entity it\n * reads (a body reads only a `one` relation as a value). A relation it reads\n * existed when it was built, so what it reads is known once.\n */\nconst linksOf = (query: AnyQuery): ReadonlyArray<string> => {\n const known = linksRead.get(query)\n if (known !== undefined) return known\n const relations: Readonly<Record<string, EntityMember>> = query.entity.relations\n const links = Query.dependencies(query)\n .fields.filter(\n field =>\n field.owner.token === query.entity.identity.token && Object.hasOwn(relations, field.key),\n )\n .map(field => field.key)\n linksRead.set(query, links)\n return links\n}\n\n/**\n * A row as a Source answers it and a Remote store keeps it, each `one`\n * relation as its ref key (`'User:ada'`), given as an interpreter reads it:\n * each link `query` reads as the id it points at. The row itself when the\n * query reads no link.\n */\nconst fromRefs = (\n query: AnyQuery,\n row: Readonly<Record<string, unknown>>,\n): Readonly<Record<string, unknown>> => {\n const links = linksOf(query)\n if (links.length === 0) return row\n const read: Record<string, unknown> = { ...row }\n for (const key of links) {\n const ref = own(row, key)\n if (ref !== null && ref !== undefined)\n put(read, key, idOfRef(ref, `${query.entity.name}.${key}`))\n }\n return read\n}\n\n/**\n * Whether `key` is a `one` relation of the Entity `owner` names: what a\n * ref-keyed holder reads as the id it points at on a row it knows only by\n * identity, as a row pointing through `HeldRow.pointing`'s relation is.\n */\nconst isLink = (owner: EntityIdentity<string>, key: string): boolean => {\n const member = own(relationsOf(owner), key)\n return member?._tag === 'Relation' && member.cardinality === 'one'\n}\n\n/** The id a ref key (`'User:ada'`) names, refusing what is none, as `where` holds it. */\nexport const idOfRef = (ref: unknown, where: string): string => {\n const separator = typeof ref === 'string' ? ref.indexOf(':') : -1\n if (separator === -1) {\n throw new Error(\n `[foldkit-entity] ${where} holds ${JSON.stringify(ref)}, which is no ref key (\"Entity:id\")`,\n )\n }\n return (ref as string).slice(separator + 1)\n}\n\nexport const Held = {\n /** A value the holder has. */\n of: held,\n /** A value the holder does not have, and why. */\n unheld,\n match: <A, B>(\n value: Held<A>,\n cases: { readonly onHeld: (value: A) => B; readonly onUnheld: (reason: string) => B },\n ): B => (value._tag === 'Held' ? cases.onHeld(value.value) : cases.onUnheld(value.reason)),\n /** Rows held as plain records; see `HeldTable`. */\n rows: rowsOf,\n fromRefs,\n isLink,\n}\n"],"mappings":";;;;;;;;;;;;;;AAuFA,MAAM,QAAW,WAAuB;CAAE,MAAM;CAAQ;AAAM;AAC9D,MAAM,UAAqB,YAA6B;CAAE,MAAM;CAAU;AAAO;;;;;;AAOjF,MAAM,UAAU,WAA+C;CAQ7D,MAAM,0BAAU,IAAI,IAAmB;CACvC,MAAM,WAAW,aACf,OAAO,gBAAgB,QAAQ,IAAI,SAAS,KAAK,CAAC;CACpD,MAAM,QAAQ,QAAmB,OAC/B,OAAO,MAAM,QAAQ,OAAO,QAAQ,GAAG;EACrC,cAAc,OAAO,MAAM,OAAO,KAAK,SAAS;EAChD,SAAQ,YACN,OAAO,MAAM,OAAO,gBAAgB,QAAQ,KAAK,IAAI,EAAE,CAAC,GAAG;GACzD,SAAQ,QAAO,KAAK,OAAO,KAAK,GAAG,CAAC;GACpC,cACE,QAAQ,WACJ,KAAK,OAAO,KAAc,CAAC,IAC3B,OAAO,GAAG,OAAO,KAAK,GAAG,OAAO,EAAE,EAAE,aAAa;EACzD,CAAC;CACL,CAAC;CAEH,MAAM,QAAQ,OAAc,WAA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Q,QAAQ,QAAQ,KAAA,GAC1B,IAAI,MAAM,KAAK,QAAQ,KAAK,GAAG,MAAM,OAAO,KAAK,GAAG,KAAK,CAAC;CAC9D;CACA,OAAO;AACT;;;;;;AAOA,MAAM,UAAU,OAA+B,QAAyB;CACtE,MAAM,SAAS,IAAI,YAAY,KAAK,GAAG,GAAG;CAC1C,OAAO,QAAQ,SAAS,cAAc,OAAO,gBAAgB;AAC/D;;AAGA,MAAa,WAAW,KAAc,UAA0B;CAC9D,MAAM,YAAY,OAAO,QAAQ,WAAW,IAAI,QAAQ,GAAG,IAAI;CAC/D,IAAI,cAAc,IAChB,MAAM,IAAI,MACR,oBAAoB,MAAM,SAAS,KAAK,UAAU,GAAG,EAAE,oCACzD;CAEF,OAAQ,IAAe,MAAM,YAAY,CAAC;AAC5C;AAEA,MAAa,OAAO;;CAElB,IAAI;;CAEJ;CACA,QACE,OACA,UACO,MAAM,SAAS,SAAS,MAAM,OAAO,MAAM,KAAK,IAAI,MAAM,SAAS,MAAM,MAAM;;CAExF,MAAM;CACN;CACA;AACF"}
package/dist/ids.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /** The brands an Entity, a Selection and a Selection page carry, apart so each module can read them. */
2
+ export declare const EntityTypeId: unique symbol;
3
+ export type EntityTypeId = typeof EntityTypeId;
4
+ export declare const SelectionTypeId: unique symbol;
5
+ export type SelectionTypeId = typeof SelectionTypeId;
6
+ export declare const SelectionPageTypeId: unique symbol;
7
+ export type SelectionPageTypeId = typeof SelectionPageTypeId;
8
+ //# sourceMappingURL=ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.d.ts","names":[],"sources":["../src/ids.ts"],"mappings":";qBACa;YACD,sBAAsB;qBAErB;YACD,yBAAyB;qBAExB;YACD,6BAA6B"}
package/dist/ids.js ADDED
@@ -0,0 +1,8 @@
1
+ // src/ids.ts
2
+ /** The brands an Entity, a Selection and a Selection page carry, apart so each module can read them. */
3
+ const EntityTypeId = Symbol.for("foldkit-entity/Entity");
4
+ const SelectionTypeId = Symbol.for("foldkit-entity/Selection");
5
+ const SelectionPageTypeId = Symbol.for("foldkit-entity/SelectionPage");
6
+ export { EntityTypeId, SelectionPageTypeId, SelectionTypeId };
7
+
8
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.js","names":[],"sources":["../src/ids.ts"],"sourcesContent":["/** The brands an Entity, a Selection and a Selection page carry, apart so each module can read them. */\nexport const EntityTypeId: unique symbol = Symbol.for('foldkit-entity/Entity')\nexport type EntityTypeId = typeof EntityTypeId\n\nexport const SelectionTypeId: unique symbol = Symbol.for('foldkit-entity/Selection')\nexport type SelectionTypeId = typeof SelectionTypeId\n\nexport const SelectionPageTypeId: unique symbol = Symbol.for('foldkit-entity/SelectionPage')\nexport type SelectionPageTypeId = typeof SelectionPageTypeId\n"],"mappings":";;AACA,MAAa,eAA8B,OAAO,IAAI,uBAAuB;AAG7E,MAAa,kBAAiC,OAAO,IAAI,0BAA0B;AAGnF,MAAa,sBAAqC,OAAO,IAAI,8BAA8B"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,380 @@
1
- import { t as Collation } from "./collation-BB4YTIbY.js";
2
- import { $ as WriteAssignment, $n as TextOperand, A as RelationInput, An as NullPredicate, At as QueryEvaluateError, B as Unmapped, Bn as OrderedOperand, Bt as supported, C as NestedInput, Cn as FieldExpr, D as Related, Dn as LiteralExpr, Dt as Judge, E as Predicting, En as Invalid, Et as Evaluated, F as Selection, Fn as OperatorDescriptor, Ft as assertSupported, G as AnyWrite, Gn as Placement, Gt as MemoryImplementation, H as WrittenAs, Hn as ParameterOf, Ht as valueOfOperand, I as SelectionPage, In as OperatorTable, It as compareRows, J as ConditionedWrite, Jn as QueryDependencies, Jt as OperatorDescription, K as BoundWrite, Kn as Predicate, Kt as MemoryOperators, L as SelectionPageTypeId, Ln as OrPredicate, Lt as decided, M as RelationSpec, Mn as Operandish, Mt as RowsMissingError, N as RelationSpecs, Nn as Operation, Nt as Verdict, O as RelatedSpecs, On as NextExpr, Ot as OperatorMissingError, P as Selected, Pn as Operator, Pt as applyOperator, Q as Write, Qn as TestPredicate, R as SelectionTypeId, Rn as Order, Rt as evaluate, S as InputMembers, Sn as FieldDependency, T as Predict, Tn as InputExpr, Tt as ContextMissingError, U as WrittenKey, Un as PathEnd, Ut as Evidence, V as WriteSurface, Vn as Ordering, Vt as truthOver, W as WrittenSpec, Wn as PathExpr, Wt as ImplementedOperator, X as ContextSet, Xn as ReadsOf, Xt as TestedEvidence, Y as ContextRead, Yn as Reads, Yt as Operators, Z as ContextValues, Zn as TermOptions, _ as EntityTypeId, _n as ContainsPredicate, _r as Side, _t as Judgement, a as DerivedSpec, an as Pointing, ar as missingName, at as WriteLink, b as IdSchemaOf, bn as ExistsPredicate, c as Entity, cn as AnyExpr, cr as NowExpr, ct as ContextValue, d as EntityIdentity, dn as CallExpr, dr as contextValue, er as ValueOf, et as WriteComputations, f as EntityInput, fn as CallOptions, ft as AfterRow, g as EntitySets, gn as ComparePredicate, gr as LawDescription, gt as Invariant, h as EntityRelation, hn as CompareOperator, hr as Law, ht as DeclaredInvariant, i as Derived, in as HeldTable, ir as maxQueryNodes, it as WriteKind, j as RelationMap, jn as OneRelation, jt as Row, k as Relation, kn as NotPredicate, kt as PredicateTruth, l as EntityDerived, ln as AnyQuery, lr as ViewerExpr, lt as EntitySet, m as EntityRef, mn as ChosenSort, mr as viewerIdOf, mt as Change, n as Cardinality, nn as HeldRow, nr as isPredicate, nt as WriteIncrement, o as DerivedSpecs, on as idOfRef, or as noOperators, ot as WriteSet, p as EntityMember, pn as ChosenOrder, pt as BeforeRow, q as Computation, qn as Query, qt as OperatorContext, r as Derivation, rn as HeldRows, rr as maxPathLength, s as Entities, sn as AndPredicate, sr as ExprContext, st as Text, t as AnyEntity, tn as Held, tr as dependenciesOf, tt as WriteCondition, u as EntityField, un as ArgumentOf, ur as ViewerPathExpr, ut as SetOn, v as Forbids, vn as Dependencies, w as PageWindow, wn as InPredicate, x as InputMember, xn as Expr, y as IdOf, yn as EqPredicate, z as TargetKey, zn as OrderTerm, zt as evaluateEach } from "./index-48JeXG0A.js";
3
- import { t as ReadFilter } from "./readFilter-CWMnFRVw.js";
4
- import { n as Words, t as SchemaWords } from "./words-a4e9o-TK.js";
5
- import { t as SchemaShape } from "./shape-LAowRf9f.js";
6
- export { type AfterRow, AndPredicate, AnyEntity, AnyExpr, AnyQuery, type AnyWrite, ArgumentOf, type BeforeRow, type BoundWrite, CallExpr, CallOptions, Cardinality, type Change, ChosenOrder, ChosenSort, Collation, CompareOperator, ComparePredicate, Computation, type ConditionedWrite, ContainsPredicate, ContextMissingError, type ContextRead, type ContextSet, type ContextValue, type ContextValues, type DeclaredInvariant, Dependencies, Derivation, Derived, DerivedSpec, DerivedSpecs, Entities, Entity, EntityDerived, EntityField, EntityIdentity, EntityInput, EntityMember, EntityRef, EntityRelation, type EntitySet, EntitySets, EntityTypeId, EqPredicate, Evaluated, Evidence, ExistsPredicate, Expr, type ExprContext, FieldDependency, FieldExpr, Forbids, Held, HeldRow, HeldRows, HeldTable, IdOf, IdSchemaOf, ImplementedOperator, InPredicate, InputExpr, InputMember, InputMembers, Invalid, type Invariant, Judge, type Judgement, Law, LawDescription, LiteralExpr, MemoryImplementation, MemoryOperators, NestedInput, NextExpr, NotPredicate, type NowExpr, NullPredicate, OneRelation, Operandish, Operation, Operator, OperatorContext, OperatorDescription, OperatorDescriptor, OperatorMissingError, OperatorTable, Operators, OrPredicate, Order, OrderTerm, OrderedOperand, Ordering, PageWindow, ParameterOf, PathEnd, PathExpr, Placement, Pointing, Predicate, PredicateTruth, Predict, Predicting, Query, QueryDependencies, QueryEvaluateError, ReadFilter, Reads, ReadsOf, Related, RelatedSpecs, Relation, RelationInput, RelationMap, RelationSpec, RelationSpecs, Row, RowsMissingError, SchemaShape, SchemaWords, Selected, Selection, SelectionPage, SelectionPageTypeId, SelectionTypeId, type SetOn, Side, TargetKey, TermOptions, TestPredicate, TestedEvidence, Text, TextOperand, Unmapped, ValueOf, Verdict, type ViewerExpr, type ViewerPathExpr, Words, Write, type WriteAssignment, type WriteComputations, type WriteCondition, type WriteIncrement, type WriteKind, type WriteLink, type WriteSet, WriteSurface, WrittenAs, WrittenKey, WrittenSpec, applyOperator, assertSupported, compareRows, contextValue, decided, dependenciesOf, evaluate, evaluateEach, idOfRef, isPredicate, maxPathLength, maxQueryNodes, missingName, noOperators, supported, truthOver, valueOfOperand, viewerIdOf };
1
+ import * as EntityMembers from "./entity.js";
2
+ import { Data, Option, Pipeable, Schema } from "effect";
3
+ import { Metadata } from "foldkit-metadata";
4
+ import { EntityTypeId, EntityTypeId as EntityTypeId$1, SelectionPageTypeId, SelectionPageTypeId as SelectionPageTypeId$1, SelectionTypeId, SelectionTypeId as SelectionTypeId$1 } from "./ids.js";
5
+ import { AnyVariantUnion, AnyVariantUnion as AnyVariantUnion$1, CheckCases, EntityVariants, StoredFields, VariantCases, VariantUnion } from "./variants.js";
6
+ import { ContextValue, EntitySet, EntitySet as EntitySet$1, SetOn } from "./sets.js";
7
+ import { AfterRow, BeforeRow, Change, DeclaredInvariant, Invariant, Judgement } from "./invariant.js";
8
+ import { HeldRow, HeldRows } from "./held.js";
9
+ import { ExprContext, ExprContext as ExprContext$1, NowExpr, NowExpr as NowExpr$1, ViewerExpr, ViewerExpr as ViewerExpr$1, ViewerPathExpr, ViewerPathExpr as ViewerPathExpr$1, contextValue, viewerIdOf } from "./context.js";
10
+ import { InputMembers, WrittenAs, WrittenKey, WrittenSpec } from "./entity.js";
11
+ import { AnyWrite, BoundWrite, Computation, ConditionedWrite, ContextRead, ContextSet, ContextValues, Write, WriteAssignment, WriteComputations, WriteCondition, WriteIncrement, WriteKind, WriteLink, WriteSet } from "./write.js";
12
+ export * from "./shape.js";
13
+ export * from "./collation.js";
14
+ export * from "./expr.js";
15
+ export * from "./held.js";
16
+ export * from "./operators.js";
17
+ export * from "./law.js";
18
+ export * from "./text.js";
19
+ export * from "./readFilter.js";
20
+ export * from "./words.js";
21
+ export * from "./evaluate.js";
22
+ /**
23
+ * What makes two descriptors the same Entity. A pipe step returns a new
24
+ * descriptor with the same identity, so compare `identity`, never the objects.
25
+ */
26
+ export interface EntityIdentity<Name extends string> {
27
+ readonly name: Name;
28
+ /** Distinguishes two Entities that were defined with the same name. */
29
+ readonly token: symbol;
30
+ }
31
+ /** An intrinsic value of the Entity: one property of `entity.schema`. */
32
+ export interface EntityField<Name extends string, Key extends string, S extends Schema.Constraint> {
33
+ readonly _tag: 'Field';
34
+ readonly owner: EntityIdentity<Name>;
35
+ readonly key: Key;
36
+ readonly schema: S;
37
+ readonly metadata: Metadata;
38
+ }
39
+ export type Cardinality = 'one' | 'many';
40
+ /**
41
+ * What happens to a row when the row its `one` relation points at is deleted
42
+ * (`Relation.one(Post, { onDelete: 'cascade' })`): `cascade`, it is deleted
43
+ * with it, by the server in the write that deletes, and foreseen by a client.
44
+ */
45
+ export type OnDelete = 'cascade';
46
+ /** What `Relation.one` / `Relation.many` return, before `Entity.relate` binds it to an owner. */
47
+ export interface RelationSpec<Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
48
+ readonly target: Target;
49
+ readonly cardinality: C;
50
+ readonly optional: Optional;
51
+ readonly onDelete: Option.Option<OnDelete>;
52
+ }
53
+ /**
54
+ * A navigation edge as its owner sees it: one or many of `target`. How the edge
55
+ * is stored (a foreign key, a join table, a nested document) is the
56
+ * interpreter's concern.
57
+ */
58
+ export interface EntityRelation<Name extends string, Key extends string, Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
59
+ readonly _tag: 'Relation';
60
+ readonly owner: EntityIdentity<Name>;
61
+ readonly key: Key;
62
+ /** The target as `Entity.relate` returned it, so its own relations can be followed. */
63
+ readonly target: () => Target;
64
+ readonly cardinality: C;
65
+ readonly optional: Optional;
66
+ /**
67
+ * What happens to this row when the row a `one` relation points at is
68
+ * deleted; none for a `many`, and for a `one` declared with nothing.
69
+ */
70
+ readonly onDelete: Option.Option<OnDelete>;
71
+ readonly metadata: Metadata;
72
+ }
73
+ type OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>;
74
+ /**
75
+ * What a derived member is. `Supplied` (`Derived.make`): an interpreter
76
+ * supplies it, and nothing else can say what it is. `Count`
77
+ * (`Derived.count`): how many rows point at this one through `relation`, a
78
+ * `one` relation of the counted Entity. `Exists` (`Derived.exists`): the id of
79
+ * a row pointing here through `relation` whose `viewer` relation points at
80
+ * whoever reads it, or none. A client predicts both from the rows it writes,
81
+ * and a server compiles both from the relations' storage.
82
+ */
83
+ export type Derivation = Data.TaggedEnum<{
84
+ /**
85
+ * `predict`: how a client foresees it for a row it writes; none, it waits
86
+ * for the server. `forbids`: which write to the row its value refuses, so a
87
+ * client holding it refuses that write where it is made.
88
+ */
89
+ Supplied: {
90
+ readonly predict: Option.Option<Predict>;
91
+ readonly forbids: Option.Option<Forbids>;
92
+ };
93
+ Count: {
94
+ readonly relation: OneRelation;
95
+ };
96
+ Exists: {
97
+ readonly relation: OneRelation;
98
+ readonly viewer: OneRelation;
99
+ };
100
+ }>;
101
+ export declare const Derivation: Data.TaggedEnum.Constructor<{
102
+ readonly _tag: "Count";
103
+ readonly relation: OneRelation;
104
+ } | {
105
+ readonly _tag: "Exists";
106
+ readonly relation: OneRelation;
107
+ readonly viewer: OneRelation;
108
+ } | {
109
+ readonly _tag: "Supplied";
110
+ readonly predict: Option.Option<Predict>;
111
+ readonly forbids: Option.Option<Forbids>;
112
+ }>;
113
+ /** What a supplied member's prediction is given: the row a write leaves, as its holder has it. */
114
+ export interface Predicting {
115
+ /** The Entity written, as the write declared it. */
116
+ readonly entity: AnyEntity;
117
+ readonly row: HeldRow;
118
+ /** The holder's other rows, where a rule's path reads. */
119
+ readonly rows: HeldRows;
120
+ /** The operation's context: who asks, and the write's time. */
121
+ readonly context: ExprContext$1;
122
+ }
123
+ /**
124
+ * A supplied member foreseen: its value **encoded**, as the store holds it, or
125
+ * none where the holder cannot know it, so nothing is shown before the answer.
126
+ */
127
+ export type Predict = (predicting: Predicting) => Option.Option<unknown>;
128
+ /**
129
+ * What a write does to a row it finds, as a supplied member's `forbids` is
130
+ * asked: an update's members and the relations it points, or a delete.
131
+ */
132
+ export interface WriteSurface {
133
+ readonly kind: 'Update' | 'Delete';
134
+ /** The members it sets, moves or computes, relations not among them. */
135
+ readonly members: ReadonlyArray<string>;
136
+ /** Each relation it points: at a row (`Link`), or at none (`Unlink`). */
137
+ readonly relations: ReadonlyArray<{
138
+ readonly relation: string;
139
+ readonly interaction: 'Link' | 'Unlink';
140
+ }>;
141
+ }
142
+ /**
143
+ * Whether a supplied member's value, **encoded** as the store holds it,
144
+ * refuses `surface`, with the reader's words for why when there are any; none
145
+ * where it does not say so for certain. Only a definite refusal: a client
146
+ * refuses the write unseen, and its server would refuse it too.
147
+ */
148
+ export type Forbids = (value: unknown, surface: WriteSurface) => Option.Option<{
149
+ readonly reason: Option.Option<string>;
150
+ }>;
151
+ /** What `Derived.make`, `Derived.count` or `Derived.exists` returns, before an Entity owns it. */
152
+ export interface DerivedSpec<S extends Schema.Constraint, D extends Derivation = Derivation> {
153
+ readonly schema: S;
154
+ readonly derives: D;
155
+ }
156
+ /** A value consumers may read that is not part of `entity.schema`, and what it derives. */
157
+ export interface EntityDerived<Name extends string, Key extends string, S extends Schema.Constraint, D extends Derivation = Derivation> {
158
+ readonly _tag: 'Derived';
159
+ readonly owner: EntityIdentity<Name>;
160
+ readonly key: Key;
161
+ readonly schema: S;
162
+ readonly derives: D;
163
+ readonly metadata: Metadata;
164
+ }
165
+ export type EntityMember = EntityField<string, string, Schema.Constraint> | EntityRelation<string, string, AnyEntity, Cardinality, boolean> | EntityDerived<string, string, Schema.Constraint>;
166
+ export type RelationSpecs = Readonly<Record<string, RelationSpec<AnyEntity, Cardinality, boolean>>>;
167
+ export type DerivedSpecs = Readonly<Record<string, DerivedSpec<Schema.Constraint>>>;
168
+ type FieldsOf<Name extends string, Fields extends Schema.Struct.Fields> = { readonly [K in keyof Fields & string]: EntityField<Name, K, Fields[K]>; };
169
+ type RelationsOf<Name extends string, Relations extends RelationSpecs> = { readonly [K in keyof Relations & string]: Relations[K] extends RelationSpec<infer Target, infer C, infer Optional> ? EntityRelation<Name, K, Target, C, Optional> : never; };
170
+ type DerivedOf<Name extends string, Derived extends DerivedSpecs> = { readonly [K in keyof Derived & string]: EntityDerived<Name, K, Derived[K]['schema'], Derived[K]['derives']>; };
171
+ export interface Entity<Name extends string, Fields extends Schema.Struct.Fields, Relations extends RelationSpecs = {}, Derived extends DerivedSpecs = {}> extends Pipeable.Pipeable {
172
+ readonly [EntityTypeId$1]: EntityTypeId$1;
173
+ readonly identity: EntityIdentity<Name>;
174
+ readonly name: Name;
175
+ /** The intrinsic value only: no relations, no derived members. */
176
+ readonly schema: Schema.Struct<Fields>;
177
+ readonly fields: FieldsOf<Name, Fields>;
178
+ readonly relations: RelationsOf<Name, Relations>;
179
+ readonly derived: DerivedOf<Name, Derived>;
180
+ /** Every readable member under one namespace; keys never collide. */
181
+ readonly members: FieldsOf<Name, Fields> & RelationsOf<Name, Relations> & DerivedOf<Name, Derived>;
182
+ /** The members whose value is the write's context, never its input (`Entity.sets`), by key. */
183
+ readonly sets: Readonly<Record<string, EntitySet$1>>;
184
+ readonly metadata: Metadata;
185
+ }
186
+ export type AnyEntity = Entity<string, any, any, any>;
187
+ /**
188
+ * An Entity whose rows are one of several shapes (`Entity.defineVariants`).
189
+ * `variants` is Effect's tagged union of them, read with its own `match`;
190
+ * `fields` and `schema` are the stored row, which every case shares.
191
+ */
192
+ export interface VariantEntity<Name extends string, Fields extends Schema.Struct.Fields, Relations extends RelationSpecs = {}, Derived extends DerivedSpecs = {}, Union extends AnyVariantUnion$1 = AnyVariantUnion$1> extends Entity<Name, Fields, Relations, Derived> {
193
+ readonly variants: Union;
194
+ }
195
+ export type AnyVariantEntity = VariantEntity<string, any, any, any, any>;
196
+ /** The keys of an Entity's `one` relations. */
197
+ type OneRelationKeys<E extends AnyEntity> = { [K in keyof E['relations'] & string]: E['relations'][K]['cardinality'] extends 'one' ? K : never; }[keyof E['relations'] & string];
198
+ /**
199
+ * What a field holding `T` may be set to: the time where it holds a number,
200
+ * the viewer's id where text, and what a path from the viewer reads where it
201
+ * holds that.
202
+ */
203
+ type ContextValueFor<T> = (number extends T ? NowExpr$1<number> : never) | (T extends string ? ViewerExpr$1<T> : never) | ViewerPathExpr$1<T | null>;
204
+ /** The name of the Entity the `one` relation `K` points at. */
205
+ type TargetName<E extends AnyEntity, K extends keyof E['relations']> = ReturnType<E['relations'][K]['target']>['name'];
206
+ /**
207
+ * What `Entity.sets` takes, by member: a field the value of the write's
208
+ * context it can hold, a `one` relation `Expr.viewer` of its target.
209
+ */
210
+ export type EntitySets<E extends AnyEntity> = { readonly [K in Exclude<keyof E['fields'] & string, 'id'>]?: ContextValueFor<Schema.Schema.Type<E['fields'][K]['schema']>>; } & { readonly [K in OneRelationKeys<E>]?: ViewerExpr$1<unknown, TargetName<E, K>>; };
211
+ export type Entities = Readonly<Record<string, AnyEntity>>;
212
+ export type RelationMap<Es extends Entities> = { readonly [K in keyof Es]?: RelationSpecs; };
213
+ /** The key in `Es` of the Entity a spec targets, matched by name. */
214
+ export type TargetKey<Es extends Entities, Target extends AnyEntity> = { [K in keyof Es]: [Es[K]['name'], Target['name']] extends [Target['name'], Es[K]['name']] ? K : never; }[keyof Es];
215
+ export type RelatedSpecs<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = { readonly [R in keyof Rs[K]]: Rs[K][R] extends RelationSpec<infer Target, infer C, infer Optional> ? RelationSpec<Related<Es, Rs, TargetKey<Es, Target>>, C, Optional> : never; };
216
+ /**
217
+ * One Entity of an `Entity.relate` result. A relation's target is again a
218
+ * `Related`, so relations can be followed through a cycle; the recursion goes
219
+ * through type arguments, which TypeScript resolves only when asked.
220
+ */
221
+ export type Related<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = Es[K] extends VariantEntity<infer Name, infer Fields, infer R, infer D, infer U> ? VariantEntity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D, U> : Es[K] extends Entity<infer Name, infer Fields, infer R, infer D> ? Entity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D> : never;
222
+ /**
223
+ * The schema that identifies one of an Entity: its `id` field when that is text
224
+ * (a plain string, a brand, a pattern), and plain text otherwise. An id is text
225
+ * because a ref travels and is stored as text; an Entity keyed by a number still
226
+ * has refs, and their ids are the number as text.
227
+ */
228
+ export type IdSchemaOf<E> = E extends {
229
+ readonly fields: {
230
+ readonly id: {
231
+ readonly schema: infer S;
232
+ };
233
+ };
234
+ } ? S extends Schema.Constraint ? [S['Type']] extends [string] ? S : Schema.String : Schema.String : Schema.String;
235
+ /** What identifies one of an Entity: `IdOf<typeof Blog.Post>` is `PostId` when its `id` field is one. */
236
+ export type IdOf<E> = IdSchemaOf<E>['Type'];
237
+ /**
238
+ * What a relation selected with `true` yields: which Entity, and which one. `Id`
239
+ * is the target's own id type, so a ref to an Author cannot be used as a Post's.
240
+ */
241
+ export interface EntityRef<Name extends string = string, Id extends string = string> {
242
+ readonly entity: Name;
243
+ readonly id: Id;
244
+ }
245
+ /**
246
+ * A view of the Entity graph: which members to read, and the schema of the
247
+ * value that results. It says nothing about where the value comes from.
248
+ */
249
+ export interface Selection<Name extends string, Members, S extends Schema.Constraint, Id extends string = string> {
250
+ readonly [SelectionTypeId$1]: SelectionTypeId$1;
251
+ /** Type-only: the id type of the Entity selected, so a reader can ask for the right one. Never set. */
252
+ readonly Id?: Id;
253
+ readonly entity: Entity<Name, any, any, any>;
254
+ /** What was selected, by member key: `true`, or a relation's nested Selection. */
255
+ readonly members: Members;
256
+ readonly schema: S;
257
+ }
258
+ /** The value a Selection reads: `Selected<typeof PostCard>` is one card's fields. */
259
+ export type Selected<S extends Selection<string, unknown, Schema.Constraint>> = S['schema']['Type'];
260
+ /**
261
+ * Which part of a `many` relation to read: the first or last so many, from a
262
+ * cursor or from the end. A cursor is whatever the interpreter handed out with an
263
+ * earlier page; nothing here looks inside it.
264
+ */
265
+ export interface PageWindow {
266
+ readonly first?: number | undefined;
267
+ readonly last?: number | undefined;
268
+ readonly after?: string | undefined;
269
+ readonly before?: string | undefined;
270
+ }
271
+ /** A Selection of a `many` relation's target, read a page at a time. */
272
+ export interface SelectionPage<Name extends string, S extends Schema.Constraint> {
273
+ readonly [SelectionPageTypeId$1]: SelectionPageTypeId$1;
274
+ readonly selection: Selection<Name, any, S>;
275
+ readonly window: PageWindow;
276
+ }
277
+ /** An input key that carries a relation's target: the id of a `one`, the ids of a `many`. */
278
+ export interface RelationInput<R extends EntityRelation<any, any, any, any, any>> {
279
+ readonly _tag: 'RelationInput';
280
+ readonly relation: R;
281
+ }
282
+ /**
283
+ * An input key that carries the relation's target itself, written through an
284
+ * input of its own: a post created with a new author, an order with its lines.
285
+ * A `one` holds that input's value, a `many` a list of them.
286
+ */
287
+ export interface NestedInput<R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>> {
288
+ readonly _tag: 'NestedInput';
289
+ readonly relation: R;
290
+ readonly input: I;
291
+ }
292
+ /** An input key that is about the operation, not the Entity: a reason, a flag, a confirmation. */
293
+ export interface Unmapped {
294
+ readonly _tag: 'Unmapped';
295
+ }
296
+ /** What one key of an operation's input means in terms of the Entity. */
297
+ export type InputMember = EntityField<string, string, Schema.Constraint> | RelationInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>> | NestedInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>, EntityInput<AnyEntity, any, any>> | Unmapped;
298
+ /**
299
+ * An operation's input read against an Entity: which member each input key
300
+ * writes. The operation decides what may be submitted; the Entity only says
301
+ * what each submitted key means.
302
+ */
303
+ export interface EntityInput<E extends AnyEntity, Fields extends Schema.Struct.Fields, Members> {
304
+ readonly entity: E;
305
+ readonly schema: Schema.Struct<Fields>;
306
+ readonly members: Members;
307
+ }
308
+ /** How a `one` relation is declared: whether it may point nowhere, and what a delete of its target does. */
309
+ export interface OneOptions<Optional extends boolean> {
310
+ readonly optional?: Optional;
311
+ /** `cascade`: a row pointing here is deleted with the row it points at. */
312
+ readonly onDelete?: OnDelete;
313
+ }
314
+ declare function one<Target extends AnyEntity>(target: Target): RelationSpec<Target, 'one', false>;
315
+ declare function one<Target extends AnyEntity, const Optional extends boolean>(target: Target, options: OneOptions<Optional> & {
316
+ readonly optional: Optional;
317
+ }): RelationSpec<Target, 'one', Optional>;
318
+ declare function one<Target extends AnyEntity>(target: Target, options: OneOptions<false>): RelationSpec<Target, 'one', false>;
319
+ /** What the owner sees: one or many of `target`. Used inside `Entity.relate`. */
320
+ export declare const Relation: {
321
+ one: typeof one;
322
+ /** For `Entity.input`: the input key holds this relation's target id, or ids for a `many`. */
323
+ input: <R extends EntityRelation<any, any, any, any, any>>(relation: R) => RelationInput<R>;
324
+ /**
325
+ * For `Entity.input`: the input key holds the relation's target itself, written
326
+ * through `input`, an `Entity.input` of the target. A `many` holds a list.
327
+ */
328
+ nested: <R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>>(relation: R, input: I) => NestedInput<R, I>;
329
+ many: <Target extends AnyEntity>(target: Target) => RelationSpec<Target, 'many', false>;
330
+ };
331
+ /** A count: never negative, never a fraction. */
332
+ declare const CountSchema: Schema.Number;
333
+ /** The id of the row an exists found, as every Remote id is held: text, or none. */
334
+ declare const WitnessSchema: Schema.OptionFromNullOr<Schema.String>;
335
+ type OwnerName<R> = R extends EntityRelation<infer Name, any, any, any, any> ? Name : never;
336
+ export declare const Derived: {
337
+ /**
338
+ * A member only an interpreter can compute, of type `schema`: it supplies the
339
+ * value. `predict` says how a client foresees it for a row it writes (Guard's
340
+ * `may`); without it the row reads as missing the member until the answer.
341
+ * `forbids` says which write to the row its value refuses (Guard's `may`
342
+ * again), so a client holding it refuses that write where it is made.
343
+ */
344
+ make: <S extends Schema.Constraint>(schema: S, options?: {
345
+ readonly predict?: Predict | undefined;
346
+ readonly forbids?: Forbids | undefined;
347
+ }) => DerivedSpec<S, Extract<Derivation, {
348
+ readonly _tag: 'Supplied';
349
+ }>>;
350
+ /**
351
+ * How many rows point at this one through `relation`, a `one` relation of
352
+ * the counted Entity: `likeCount: Derived.count(Like.relations.post)` on
353
+ * Post. Added after `Entity.relate`, which makes the relation. A server
354
+ * compiles it from the relation's storage, and a client predicts it from
355
+ * the rows a pending write inserts, deletes or points elsewhere.
356
+ */
357
+ count: <R extends EntityRelation<string, string, AnyEntity, 'one', boolean>>(relation: R) => DerivedSpec<typeof CountSchema, {
358
+ readonly _tag: 'Count';
359
+ readonly relation: R;
360
+ }>;
361
+ /**
362
+ * Whether a row points at this one through `relation` and at whoever reads
363
+ * it through `viewer`, another `one` relation of the same pointing Entity:
364
+ * `liked: Derived.exists(Like.relations.post, { viewer: Like.relations.user })`
365
+ * on Post. Its value is that row's id as the store holds it, or none, so the
366
+ * reader can take it back (`Write.delete` by that id). It depends on who
367
+ * reads it: a server answers it for the principal it reads as, and a client
368
+ * predicts it for the viewer it is told of.
369
+ */
370
+ exists: <R extends OneRelation, V extends EntityRelation<OwnerName<R>, string, AnyEntity, 'one', boolean>>(relation: R, options: {
371
+ readonly viewer: V;
372
+ }) => DerivedSpec<typeof WitnessSchema, {
373
+ readonly _tag: 'Exists';
374
+ readonly relation: R;
375
+ readonly viewer: V;
376
+ }>;
377
+ };
378
+ export { type AfterRow, type AnyVariantUnion, type AnyWrite, type BeforeRow, type BoundWrite, type Change, type CheckCases, Computation, type ConditionedWrite, type ContextRead, type ContextSet, type ContextValue, type ContextValues, type DeclaredInvariant, type EntitySet, EntityTypeId, type EntityVariants, type ExprContext, type InputMembers, type Invariant, type Judgement, type NowExpr, SelectionPageTypeId, SelectionTypeId, type SetOn, type StoredFields, type VariantCases, type VariantUnion, type ViewerExpr, type ViewerPathExpr, Write, type WriteAssignment, type WriteComputations, type WriteCondition, type WriteIncrement, type WriteKind, type WriteLink, type WriteSet, type WrittenAs, type WrittenKey, type WrittenSpec, contextValue, viewerIdOf };
379
+ export { EntityMembers as Entity };
380
+ //# sourceMappingURL=index.d.ts.map