foldkit-entity 0.13.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.
- package/README.md +962 -512
- package/dist/collation.d.ts +33 -0
- package/dist/collation.d.ts.map +1 -0
- package/dist/collation.js +46 -0
- package/dist/collation.js.map +1 -0
- package/dist/conformance/index.d.ts +66 -0
- package/dist/conformance/index.d.ts.map +1 -0
- package/dist/conformance/{index.mjs → index.js} +22 -360
- package/dist/conformance/index.js.map +1 -0
- package/dist/conformance/operator.d.ts +115 -0
- package/dist/conformance/operator.d.ts.map +1 -0
- package/dist/conformance/operator.js +354 -0
- package/dist/conformance/operator.js.map +1 -0
- package/dist/conformance/relational.d.ts +240 -0
- package/dist/conformance/relational.d.ts.map +1 -0
- package/dist/conformance/relational.js +425 -0
- package/dist/conformance/relational.js.map +1 -0
- package/dist/conformance/tuples.d.ts +8 -0
- package/dist/conformance/tuples.d.ts.map +1 -0
- package/dist/conformance/tuples.js +43 -0
- package/dist/conformance/tuples.js.map +1 -0
- package/dist/conformance/variants.d.ts +47 -0
- package/dist/conformance/variants.d.ts.map +1 -0
- package/dist/conformance/variants.js +126 -0
- package/dist/conformance/variants.js.map +1 -0
- package/dist/context.d.ts +60 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +36 -0
- package/dist/context.js.map +1 -0
- package/dist/descriptor.d.ts +33 -0
- package/dist/descriptor.d.ts.map +1 -0
- package/dist/descriptor.js +137 -0
- package/dist/descriptor.js.map +1 -0
- package/dist/entity.d.ts +231 -0
- package/dist/entity.d.ts.map +1 -0
- package/dist/entity.js +435 -0
- package/dist/entity.js.map +1 -0
- package/dist/evaluate.d.ts +204 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +765 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/expr.d.ts +915 -0
- package/dist/expr.d.ts.map +1 -0
- package/dist/expr.js +1630 -0
- package/dist/expr.js.map +1 -0
- package/dist/held.d.ts +112 -0
- package/dist/held.d.ts.map +1 -0
- package/dist/held.js +155 -0
- package/dist/held.js.map +1 -0
- package/dist/ids.d.ts +8 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +8 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +380 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +110 -0
- package/dist/index.js.map +1 -0
- package/dist/invariant.d.ts +123 -0
- package/dist/invariant.d.ts.map +1 -0
- package/dist/invariant.js +236 -0
- package/dist/invariant.js.map +1 -0
- package/dist/jsonSchema.d.ts +3 -0
- package/dist/jsonSchema.d.ts.map +1 -0
- package/dist/jsonSchema.js +17 -0
- package/dist/jsonSchema.js.map +1 -0
- package/dist/law.d.ts +264 -0
- package/dist/law.d.ts.map +1 -0
- package/dist/law.js +111 -0
- package/dist/law.js.map +1 -0
- package/dist/operators.d.ts +170 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +207 -0
- package/dist/operators.js.map +1 -0
- package/dist/readFilter.d.ts +8 -0
- package/dist/readFilter.d.ts.map +1 -0
- package/dist/readFilter.js +23 -0
- package/dist/readFilter.js.map +1 -0
- package/dist/record.d.ts +11 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +21 -0
- package/dist/record.js.map +1 -0
- package/dist/sets.d.ts +18 -0
- package/dist/sets.d.ts.map +1 -0
- package/dist/sets.js +57 -0
- package/dist/sets.js.map +1 -0
- package/dist/shape.d.ts +23 -0
- package/dist/shape.d.ts.map +1 -0
- package/dist/shape.js +30 -0
- package/dist/shape.js.map +1 -0
- package/dist/tested.d.ts +8 -0
- package/dist/tested.d.ts.map +1 -0
- package/dist/tested.js +25 -0
- package/dist/tested.js.map +1 -0
- package/dist/text.d.ts +27 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +39 -0
- package/dist/text.js.map +1 -0
- package/dist/variants.d.ts +72 -0
- package/dist/variants.d.ts.map +1 -0
- package/dist/variants.js +76 -0
- package/dist/variants.js.map +1 -0
- package/dist/words.d.ts +17 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +22 -0
- package/dist/words.js.map +1 -0
- package/dist/write.d.ts +312 -0
- package/dist/write.d.ts.map +1 -0
- package/dist/write.js +520 -0
- package/dist/write.js.map +1 -0
- package/package.json +8 -8
- package/dist/conformance/index.d.mts +0 -274
- package/dist/conformance/index.d.mts.map +0 -1
- package/dist/conformance/index.mjs.map +0 -1
- package/dist/index-hu8IYlNc.d.mts +0 -2228
- package/dist/index-hu8IYlNc.d.mts.map +0 -1
- package/dist/index.d.mts +0 -2
- package/dist/index.mjs +0 -1
- package/dist/src-DBAilaDV.mjs +0 -3061
- package/dist/src-DBAilaDV.mjs.map +0 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { Data, Equivalence, Option, Schema } from "effect";
|
|
2
|
+
import { Collation } from "./collation.js";
|
|
3
|
+
import { Law, LawDescription } from "./law.js";
|
|
4
|
+
import { Text } from "./text.js";
|
|
5
|
+
import { jsonSchemaOf } from "./jsonSchema.js";
|
|
6
|
+
import { tableTested, testedByRunner } from "./tested.js";
|
|
7
|
+
// src/operators.ts
|
|
8
|
+
/**
|
|
9
|
+
* How the in-memory interpreter runs primitive operators: a table of
|
|
10
|
+
* implementations, passed to `evaluate` and every judge as a value. A table is
|
|
11
|
+
* immutable and implements each operator once; nothing registers anything
|
|
12
|
+
* globally. See `docs/design/expr-operations-DESIGN.md` §5.4.
|
|
13
|
+
*/
|
|
14
|
+
const Evidence = Data.taggedEnum();
|
|
15
|
+
/** A JSON Schema document's root: an object (`$defs` holds what it refers to). */
|
|
16
|
+
const JsonSchema = Schema.Record(Schema.String, Schema.Unknown);
|
|
17
|
+
/**
|
|
18
|
+
* An operator as JSON, by its signature and never its body (a defined
|
|
19
|
+
* operator says `defined`, not what it means): what a server can publish of
|
|
20
|
+
* what a body may call, and a client compare with what it runs. Its schemas
|
|
21
|
+
* are each argument's and the result's stored side, as JSON Schemas.
|
|
22
|
+
*/
|
|
23
|
+
const OperatorDescription = Schema.Struct({
|
|
24
|
+
/** By id, and `@version` past the first: what tables, evidence and refusals name. */
|
|
25
|
+
name: Schema.String,
|
|
26
|
+
id: Schema.String,
|
|
27
|
+
version: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),
|
|
28
|
+
description: Schema.NullOr(Schema.String),
|
|
29
|
+
input: Schema.Array(JsonSchema),
|
|
30
|
+
output: JsonSchema,
|
|
31
|
+
nulls: Schema.Literals(["strict", "handled"]),
|
|
32
|
+
collation: Schema.Literals([
|
|
33
|
+
"none",
|
|
34
|
+
"operands",
|
|
35
|
+
"required"
|
|
36
|
+
]),
|
|
37
|
+
laws: Schema.Array(LawDescription),
|
|
38
|
+
defined: Schema.Boolean
|
|
39
|
+
});
|
|
40
|
+
const described = (operator) => ({
|
|
41
|
+
name: nameOf(operator),
|
|
42
|
+
id: operator.id,
|
|
43
|
+
version: operator.version,
|
|
44
|
+
description: Option.getOrNull(operator.description),
|
|
45
|
+
input: operator.input.map(jsonSchemaOf),
|
|
46
|
+
output: jsonSchemaOf(operator.output),
|
|
47
|
+
nulls: operator.nulls,
|
|
48
|
+
collation: operator.collation,
|
|
49
|
+
laws: operator.laws.map(Law.describe),
|
|
50
|
+
defined: Option.isSome(operator.definedAs)
|
|
51
|
+
});
|
|
52
|
+
/** An operator by its id, and its version when that is not the first. */
|
|
53
|
+
const nameOf = (operator) => operator.version === 1 ? operator.id : `${operator.id}@${operator.version}`;
|
|
54
|
+
const fail = (what) => {
|
|
55
|
+
throw new Error(`[foldkit-entity] Operators: ${what}`);
|
|
56
|
+
};
|
|
57
|
+
const table = (implementations, checked, evidence) => {
|
|
58
|
+
const byOperator = /* @__PURE__ */ new Map();
|
|
59
|
+
const byName = /* @__PURE__ */ new Map();
|
|
60
|
+
for (const { operator, run } of implementations) {
|
|
61
|
+
if (byOperator.has(operator)) fail(`${nameOf(operator)} is implemented twice; replace one with Operators.override`);
|
|
62
|
+
const named = byName.get(nameOf(operator));
|
|
63
|
+
if (named !== void 0 && named !== operator) fail(`two operators are named ${nameOf(operator)}; give one another id or version`);
|
|
64
|
+
byName.set(nameOf(operator), operator);
|
|
65
|
+
byOperator.set(operator, run);
|
|
66
|
+
}
|
|
67
|
+
return Object.freeze({
|
|
68
|
+
_tag: "MemoryOperators",
|
|
69
|
+
has: (operator) => byOperator.has(operator),
|
|
70
|
+
versions: (id) => implementations.filter(({ operator }) => operator.id === id).map(({ operator }) => operator.version),
|
|
71
|
+
get: (operator) => Option.fromUndefinedOr(byOperator.get(operator)),
|
|
72
|
+
named: (name) => Option.fromUndefinedOr(byName.get(name)),
|
|
73
|
+
checked,
|
|
74
|
+
implementations: Object.freeze([...implementations]),
|
|
75
|
+
evidence: Object.freeze([...evidence])
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
/** Whether two laws, as JSON, are the same law. */
|
|
79
|
+
const sameLaw = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
80
|
+
/** What a table runs for the operator under `name`: none when it implements none. */
|
|
81
|
+
const runNamed = (operators, name) => Option.flatMap(operators.named(name), operators.get);
|
|
82
|
+
const sameRun = Option.makeEquivalence(Equivalence.strictEqual());
|
|
83
|
+
/** An implementation, its operator's null rule checked against the function chosen for it. */
|
|
84
|
+
const implementation = (operator, nulls, run) => {
|
|
85
|
+
if (Option.isSome(operator.definedAs)) fail(`${nameOf(operator)} is defined by an expression, which every interpreter runs; it takes no implementation`);
|
|
86
|
+
if (operator.nulls !== nulls) fail(nulls === "strict" ? `${nameOf(operator)} handles null itself; implement it with Operators.implementHandlingNulls` : `${nameOf(operator)} is strict about null; implement it with Operators.implement`);
|
|
87
|
+
return Object.freeze({
|
|
88
|
+
operator,
|
|
89
|
+
run: (args, context) => run(...args, context)
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const REPLACEMENT = String.fromCharCode(65533);
|
|
93
|
+
/**
|
|
94
|
+
* Text's characters as a database holds them: a lone surrogate, which UTF-8
|
|
95
|
+
* cannot encode, as U+FFFD, which SQLite's and Postgres's drivers send instead.
|
|
96
|
+
*/
|
|
97
|
+
const charactersOf = (text) => Array.from(text, (each) => {
|
|
98
|
+
const code = each.codePointAt(0);
|
|
99
|
+
return code >= 55296 && code <= 57343 ? REPLACEMENT : each;
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* Whether `text` begins with `prefix` by character, as SQL's `substr` and
|
|
103
|
+
* `left` count: `String.prototype.startsWith` compares UTF-16 units, so half
|
|
104
|
+
* of an astral character would begin the text that holds all of it.
|
|
105
|
+
*/
|
|
106
|
+
const beginsWith = (text, prefix) => {
|
|
107
|
+
const [characters, start] = [charactersOf(text), charactersOf(prefix)];
|
|
108
|
+
return start.length <= characters.length && start.every((each, i) => each === characters[i]);
|
|
109
|
+
};
|
|
110
|
+
const startsWith = implementation(Text.startsWith.descriptor, "strict", (...given) => {
|
|
111
|
+
const [text, prefix, { collation }] = given;
|
|
112
|
+
return Option.exists(collation, (each) => each._tag === "AsciiFold") ? beginsWith(Collation.foldAscii(text), Collation.foldAscii(prefix)) : beginsWith(text, prefix);
|
|
113
|
+
});
|
|
114
|
+
const Operators = {
|
|
115
|
+
/**
|
|
116
|
+
* An implementation of a strict operator: a pure function of its arguments'
|
|
117
|
+
* stored values, never given a null (the kernel answers null, or unknown, for
|
|
118
|
+
* one first), and told the call's collation last.
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* Operators.implement(startsWith, (text, prefix, { collation }) => text.startsWith(prefix))
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
implement: (operator, run) => implementation(operator.descriptor, "strict", run),
|
|
125
|
+
/**
|
|
126
|
+
* An implementation of an operator declared `nulls: 'handled'`: given nulls
|
|
127
|
+
* as they are, it answers for them; a predicate answers `null` for unknown.
|
|
128
|
+
*/
|
|
129
|
+
implementHandlingNulls: (operator, run) => implementation(operator.descriptor, "handled", run),
|
|
130
|
+
/** A table of implementations. One operator implemented twice, or two under one name, is refused. */
|
|
131
|
+
memory: (...implementations) => table(implementations, false, []),
|
|
132
|
+
/** Both tables' implementations. An operator both implement is refused: say which wins with `override`. */
|
|
133
|
+
merge: (left, right) => {
|
|
134
|
+
const both = right.implementations.filter(({ operator }) => left.has(operator));
|
|
135
|
+
if (both.length > 0) fail(`both tables implement ${both.map(({ operator }) => nameOf(operator)).join(", ")}; replace with Operators.override`);
|
|
136
|
+
return table([...left.implementations, ...right.implementations], left.checked || right.checked, [...left.evidence, ...right.evidence]);
|
|
137
|
+
},
|
|
138
|
+
/**
|
|
139
|
+
* The table with these implementations in place of its own for the same
|
|
140
|
+
* operators. Evidence of a replaced implementation goes with it: it was
|
|
141
|
+
* that implementation's.
|
|
142
|
+
*/
|
|
143
|
+
override: (base, ...implementations) => {
|
|
144
|
+
const replaced = new Set(implementations.map(({ operator }) => operator));
|
|
145
|
+
const names = new Set([...replaced].map(nameOf));
|
|
146
|
+
return table([...base.implementations.filter(({ operator }) => !replaced.has(operator)), ...implementations], base.checked, base.evidence.filter(({ operator, law }) => !names.has(operator) && !Law.witnessesNamed(law).some((name) => names.has(name))));
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* The table with what is known of its implementations' laws recorded: each
|
|
150
|
+
* record of an operator it implements and a law that operator declares.
|
|
151
|
+
* `Conformance.laws` gives `Tested` records, each of the implementation it
|
|
152
|
+
* ran, and a table takes only those of its own; `Evidence.Proven` says a
|
|
153
|
+
* proof elsewhere covers one.
|
|
154
|
+
*/
|
|
155
|
+
withEvidence: (base, evidence) => {
|
|
156
|
+
const own = new Map(base.implementations.map((each) => [nameOf(each.operator), each]));
|
|
157
|
+
for (const record of evidence) {
|
|
158
|
+
const implemented = own.get(record.operator) ?? fail(`evidence names ${record.operator}, which this table does not implement`);
|
|
159
|
+
if (!implemented.operator.laws.map(Law.describe).some((law) => sameLaw(law, record.law))) fail(`evidence names a law of ${record.operator} it does not declare (${record.law.kind})`);
|
|
160
|
+
if (record._tag !== "Tested") continue;
|
|
161
|
+
const tested = tableTested(record);
|
|
162
|
+
if (!Option.exists(tested, (table) => table.implementations.includes(implemented))) fail(`evidence of ${record.operator}'s ${record.law.kind} law was not tested on this table's implementation; test it with Conformance.laws(op, table)`);
|
|
163
|
+
const other = Law.witnessesNamed(record.law).find((name) => !Option.exists(tested, (table) => sameRun(runNamed(table, name), runNamed(base, name))));
|
|
164
|
+
if (other !== void 0) fail(`evidence of ${record.operator}'s ${record.law.kind} law was tested with another implementation of ${other}, which it calls; test it with Conformance.laws(op, table)`);
|
|
165
|
+
}
|
|
166
|
+
return table(base.implementations, base.checked, [...base.evidence, ...evidence]);
|
|
167
|
+
},
|
|
168
|
+
/**
|
|
169
|
+
* The memory implementation `Conformance.laws` tested a record on; none for
|
|
170
|
+
* a record of a runner (`Conformance.lawsBy`) or a proof.
|
|
171
|
+
*/
|
|
172
|
+
testedOn: (record) => Option.flatMap(tableTested(record), (table) => Option.fromUndefinedOr(table.implementations.find(({ operator }) => nameOf(operator) === record.operator))),
|
|
173
|
+
/**
|
|
174
|
+
* Whether `Conformance.lawsBy` made a record, through a runner: what an SQL
|
|
175
|
+
* table takes. Which runner it was, the database's or another, is the
|
|
176
|
+
* caller's word; a record built by hand, copied, or tested in memory is not.
|
|
177
|
+
*/
|
|
178
|
+
testedByRunner,
|
|
179
|
+
/**
|
|
180
|
+
* Each operator the table implements, by name, with the laws it declares and
|
|
181
|
+
* the evidence the table records of them: what a server can publish of how
|
|
182
|
+
* it runs a body. JSON as it is.
|
|
183
|
+
*/
|
|
184
|
+
describeTable: (base) => base.implementations.map(({ operator }) => ({
|
|
185
|
+
...described(operator),
|
|
186
|
+
evidence: base.evidence.filter((record) => record.operator === nameOf(operator))
|
|
187
|
+
})),
|
|
188
|
+
/**
|
|
189
|
+
* An operator as JSON (`OperatorDescription`): its name, version and
|
|
190
|
+
* description, each argument's and its result's stored side as a JSON
|
|
191
|
+
* Schema (as JSON carries it: a number also admits the strings `"Infinity"`,
|
|
192
|
+
* `"-Infinity"` and `"NaN"`), its null and collation rules, its laws, and
|
|
193
|
+
* whether it is defined. Its signature, never its body.
|
|
194
|
+
*/
|
|
195
|
+
describe: (operator) => described(operator.descriptor),
|
|
196
|
+
/**
|
|
197
|
+
* The table, checking every answer against its operator's output schema, so
|
|
198
|
+
* an implementation that answers the wrong kind fails where it answered. For
|
|
199
|
+
* tests: it costs a decode per call.
|
|
200
|
+
*/
|
|
201
|
+
checked: (base) => table(base.implementations, true, base.evidence),
|
|
202
|
+
/** The kernel's own operators (`Text.startsWith`): every memory interpreter's default. */
|
|
203
|
+
kernel: table([startsWith], false, [])
|
|
204
|
+
};
|
|
205
|
+
export { Evidence, OperatorDescription, Operators };
|
|
206
|
+
|
|
207
|
+
//# sourceMappingURL=operators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operators.js","names":[],"sources":["../src/operators.ts"],"sourcesContent":["/**\n * How the in-memory interpreter runs primitive operators: a table of\n * implementations, passed to `evaluate` and every judge as a value. A table is\n * immutable and implements each operator once; nothing registers anything\n * globally. See `docs/design/expr-operations-DESIGN.md` §5.4.\n */\nimport { Data, Equivalence, Option, Schema } from 'effect'\nimport { Collation } from './collation.js'\nimport type { Operator, OperatorDescriptor, OperatorTable } from './expr.js'\nimport { Law, LawDescription } from './law.js'\nimport { jsonSchemaOf } from './jsonSchema.js'\nimport { tableTested, testedByRunner } from './tested.js'\nimport { Text } from './text.js'\n\n/** What an implementation is told besides its arguments. */\nexport interface OperatorContext {\n /** The collation the call compares text under, from its operator's `collation` rule. */\n readonly collation: Option.Option<Collation>\n}\n\n/** One operator's in-memory implementation: a pure function of stored values. */\nexport interface MemoryImplementation {\n readonly operator: OperatorDescriptor\n readonly run: (args: ReadonlyArray<unknown>, context: OperatorContext) => unknown\n}\n\n/**\n * What is known of a law of an implementation, past its operator claiming it\n * (expr-operations-DESIGN §10.3): `Tested`, it passed `Conformance.laws`'\n * cases; `Proven`, a proof elsewhere says so under its assumptions, recorded\n * and never checked here.\n */\nexport type Evidence = Data.TaggedEnum<{\n Tested: {\n readonly operator: string\n readonly law: LawDescription\n readonly implementation: string\n readonly cases: number\n readonly seed: number\n }\n Proven: {\n readonly operator: string\n readonly law: LawDescription\n readonly reference: string\n readonly assumptions: ReadonlyArray<string>\n }\n}>\nexport const Evidence = Data.taggedEnum<Evidence>()\n\n/** A record of a law passing `Conformance.laws`' cases. */\nexport type TestedEvidence = Extract<Evidence, { readonly _tag: 'Tested' }>\n\n/** A JSON Schema document's root: an object (`$defs` holds what it refers to). */\nconst JsonSchema = Schema.Record(Schema.String, Schema.Unknown)\n\n/**\n * An operator as JSON, by its signature and never its body (a defined\n * operator says `defined`, not what it means): what a server can publish of\n * what a body may call, and a client compare with what it runs. Its schemas\n * are each argument's and the result's stored side, as JSON Schemas.\n */\nexport const OperatorDescription = Schema.Struct({\n /** By id, and `@version` past the first: what tables, evidence and refusals name. */\n name: Schema.String,\n id: Schema.String,\n version: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),\n description: Schema.NullOr(Schema.String),\n input: Schema.Array(JsonSchema),\n output: JsonSchema,\n nulls: Schema.Literals(['strict', 'handled']),\n collation: Schema.Literals(['none', 'operands', 'required']),\n laws: Schema.Array(LawDescription),\n defined: Schema.Boolean,\n})\nexport type OperatorDescription = typeof OperatorDescription.Type\n\n/** One operator a table implements, as `Operators.describeTable` lists it: described, with its evidence. */\nexport interface ImplementedOperator extends OperatorDescription {\n readonly evidence: ReadonlyArray<Evidence>\n}\n\nconst described = (operator: OperatorDescriptor): OperatorDescription => ({\n name: nameOf(operator),\n id: operator.id,\n version: operator.version,\n description: Option.getOrNull(operator.description),\n input: operator.input.map(jsonSchemaOf),\n output: jsonSchemaOf(operator.output),\n nulls: operator.nulls,\n collation: operator.collation,\n laws: operator.laws.map(Law.describe),\n defined: Option.isSome(operator.definedAs),\n})\n\n/** The in-memory interpreter's implementations, by operator. */\nexport interface MemoryOperators extends OperatorTable {\n readonly _tag: 'MemoryOperators'\n readonly get: (operator: OperatorDescriptor) => Option.Option<MemoryImplementation['run']>\n /**\n * The operator it implements under a name (`id`, or `id@version` past the\n * first): how a computed assignment carried as data finds what to run.\n */\n readonly named: (name: string) => Option.Option<OperatorDescriptor>\n /** Whether each answer is checked against its operator's output schema: for tests. */\n readonly checked: boolean\n readonly implementations: ReadonlyArray<MemoryImplementation>\n /** What is known of its implementations' laws: `Operators.withEvidence`. */\n readonly evidence: ReadonlyArray<Evidence>\n}\n\ntype StoredOf<S extends Schema.Top> = Schema.Codec.Encoded<S>\n\n/** An operator by its id, and its version when that is not the first. */\nconst nameOf = (operator: OperatorDescriptor): string =>\n operator.version === 1 ? operator.id : `${operator.id}@${operator.version}`\n\nconst fail = (what: string): never => {\n throw new Error(`[foldkit-entity] Operators: ${what}`)\n}\n\nconst table = (\n implementations: ReadonlyArray<MemoryImplementation>,\n checked: boolean,\n evidence: ReadonlyArray<Evidence>,\n): MemoryOperators => {\n const byOperator = new Map<OperatorDescriptor, MemoryImplementation['run']>()\n const byName = new Map<string, OperatorDescriptor>()\n for (const { operator, run } of implementations) {\n if (byOperator.has(operator)) {\n fail(`${nameOf(operator)} is implemented twice; replace one with Operators.override`)\n }\n // Two descriptors under one name would leave a body's call ambiguous.\n const named = byName.get(nameOf(operator))\n if (named !== undefined && named !== operator) {\n fail(`two operators are named ${nameOf(operator)}; give one another id or version`)\n }\n byName.set(nameOf(operator), operator)\n byOperator.set(operator, run)\n }\n return Object.freeze({\n _tag: 'MemoryOperators',\n has: (operator: OperatorDescriptor) => byOperator.has(operator),\n versions: (id: string) =>\n implementations\n .filter(({ operator }) => operator.id === id)\n .map(({ operator }) => operator.version),\n get: (operator: OperatorDescriptor) => Option.fromUndefinedOr(byOperator.get(operator)),\n named: (name: string) => Option.fromUndefinedOr(byName.get(name)),\n checked,\n implementations: Object.freeze([...implementations]),\n evidence: Object.freeze([...evidence]),\n })\n}\n\n/** Whether two laws, as JSON, are the same law. */\nconst sameLaw = (left: LawDescription, right: LawDescription): boolean =>\n JSON.stringify(left) === JSON.stringify(right)\n\n/** What a table runs for the operator under `name`: none when it implements none. */\nconst runNamed = (operators: MemoryOperators, name: string) =>\n Option.flatMap(operators.named(name), operators.get)\n\nconst sameRun = Option.makeEquivalence(Equivalence.strictEqual<MemoryImplementation['run']>())\n\n/** An implementation, its operator's null rule checked against the function chosen for it. */\nconst implementation = (\n operator: OperatorDescriptor,\n nulls: OperatorDescriptor['nulls'],\n run: (...args: ReadonlyArray<unknown>) => unknown,\n): MemoryImplementation => {\n // SQL compiles what a defined operator means, so an implementation here would\n // be a second meaning that only memory runs.\n if (Option.isSome(operator.definedAs)) {\n fail(\n `${nameOf(operator)} is defined by an expression, which every interpreter runs; it takes no implementation`,\n )\n }\n if (operator.nulls !== nulls) {\n fail(\n nulls === 'strict'\n ? `${nameOf(operator)} handles null itself; implement it with Operators.implementHandlingNulls`\n : `${nameOf(operator)} is strict about null; implement it with Operators.implement`,\n )\n }\n return Object.freeze({\n operator,\n run: (args: ReadonlyArray<unknown>, context: OperatorContext) => run(...args, context),\n })\n}\n\nconst REPLACEMENT = String.fromCharCode(0xfffd)\n\n/**\n * Text's characters as a database holds them: a lone surrogate, which UTF-8\n * cannot encode, as U+FFFD, which SQLite's and Postgres's drivers send instead.\n */\nconst charactersOf = (text: string): ReadonlyArray<string> =>\n Array.from(text, each => {\n const code = each.codePointAt(0)!\n return code >= 0xd800 && code <= 0xdfff ? REPLACEMENT : each\n })\n\n/**\n * Whether `text` begins with `prefix` by character, as SQL's `substr` and\n * `left` count: `String.prototype.startsWith` compares UTF-16 units, so half\n * of an astral character would begin the text that holds all of it.\n */\nconst beginsWith = (text: string, prefix: string): boolean => {\n const [characters, start] = [charactersOf(text), charactersOf(prefix)]\n return start.length <= characters.length && start.every((each, i) => each === characters[i])\n}\n\nconst startsWith = implementation(Text.startsWith.descriptor, 'strict', (...given) => {\n const [text, prefix, { collation }] = given as [string, string, OperatorContext]\n const folded = Option.exists(collation, each => each._tag === 'AsciiFold')\n return folded\n ? beginsWith(Collation.foldAscii(text), Collation.foldAscii(prefix))\n : beginsWith(text, prefix)\n})\n\nexport const Operators = {\n /**\n * An implementation of a strict operator: a pure function of its arguments'\n * stored values, never given a null (the kernel answers null, or unknown, for\n * one first), and told the call's collation last.\n *\n * ```ts\n * Operators.implement(startsWith, (text, prefix, { collation }) => text.startsWith(prefix))\n * ```\n */\n implement: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(\n operator: Operator<In, Out>,\n run: (\n ...args: [...{ readonly [K in keyof In]: NonNullable<StoredOf<In[K]>> }, OperatorContext]\n ) => StoredOf<Out>,\n ): MemoryImplementation =>\n implementation(\n operator.descriptor,\n 'strict',\n run as (...args: ReadonlyArray<unknown>) => unknown,\n ),\n\n /**\n * An implementation of an operator declared `nulls: 'handled'`: given nulls\n * as they are, it answers for them; a predicate answers `null` for unknown.\n */\n implementHandlingNulls: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(\n operator: Operator<In, Out>,\n run: (\n ...args: [...{ readonly [K in keyof In]: StoredOf<In[K]> }, OperatorContext]\n ) => StoredOf<Out> | null,\n ): MemoryImplementation =>\n implementation(\n operator.descriptor,\n 'handled',\n run as (...args: ReadonlyArray<unknown>) => unknown,\n ),\n\n /** A table of implementations. One operator implemented twice, or two under one name, is refused. */\n memory: (...implementations: ReadonlyArray<MemoryImplementation>): MemoryOperators =>\n table(implementations, false, []),\n\n /** Both tables' implementations. An operator both implement is refused: say which wins with `override`. */\n merge: (left: MemoryOperators, right: MemoryOperators): MemoryOperators => {\n const both = right.implementations.filter(({ operator }) => left.has(operator))\n if (both.length > 0) {\n fail(\n `both tables implement ${both.map(({ operator }) => nameOf(operator)).join(', ')}; replace with Operators.override`,\n )\n }\n return table(\n [...left.implementations, ...right.implementations],\n left.checked || right.checked,\n [...left.evidence, ...right.evidence],\n )\n },\n\n /**\n * The table with these implementations in place of its own for the same\n * operators. Evidence of a replaced implementation goes with it: it was\n * that implementation's.\n */\n override: (\n base: MemoryOperators,\n ...implementations: ReadonlyArray<MemoryImplementation>\n ): MemoryOperators => {\n const replaced = new Set(implementations.map(({ operator }) => operator))\n const names = new Set([...replaced].map(nameOf))\n return table(\n [\n ...base.implementations.filter(({ operator }) => !replaced.has(operator)),\n ...implementations,\n ],\n base.checked,\n // A replaced operator's evidence goes, and so does a law's that called it as a witness.\n base.evidence.filter(\n ({ operator, law }) =>\n !names.has(operator) && !Law.witnessesNamed(law).some(name => names.has(name)),\n ),\n )\n },\n\n /**\n * The table with what is known of its implementations' laws recorded: each\n * record of an operator it implements and a law that operator declares.\n * `Conformance.laws` gives `Tested` records, each of the implementation it\n * ran, and a table takes only those of its own; `Evidence.Proven` says a\n * proof elsewhere covers one.\n */\n withEvidence: (base: MemoryOperators, evidence: ReadonlyArray<Evidence>): MemoryOperators => {\n const own = new Map(base.implementations.map(each => [nameOf(each.operator), each]))\n for (const record of evidence) {\n const implemented =\n own.get(record.operator) ??\n fail(`evidence names ${record.operator}, which this table does not implement`)\n if (!implemented.operator.laws.map(Law.describe).some(law => sameLaw(law, record.law))) {\n fail(`evidence names a law of ${record.operator} it does not declare (${record.law.kind})`)\n }\n if (record._tag !== 'Tested') continue\n // Evidence of another implementation, or of a runner, says nothing of this one.\n const tested = tableTested(record)\n if (!Option.exists(tested, table => table.implementations.includes(implemented))) {\n fail(\n `evidence of ${record.operator}'s ${record.law.kind} law was not tested on this table's implementation; test it with Conformance.laws(op, table)`,\n )\n }\n // Nor with another implementation of a witness the law calls (`distributes`, `inverse`).\n const other = Law.witnessesNamed(record.law).find(\n name =>\n !Option.exists(tested, table => sameRun(runNamed(table, name), runNamed(base, name))),\n )\n if (other !== undefined) {\n fail(\n `evidence of ${record.operator}'s ${record.law.kind} law was tested with another implementation of ${other}, which it calls; test it with Conformance.laws(op, table)`,\n )\n }\n }\n return table(base.implementations, base.checked, [...base.evidence, ...evidence])\n },\n\n /**\n * The memory implementation `Conformance.laws` tested a record on; none for\n * a record of a runner (`Conformance.lawsBy`) or a proof.\n */\n testedOn: (record: Evidence): Option.Option<MemoryImplementation> =>\n Option.flatMap(tableTested(record), table =>\n Option.fromUndefinedOr(\n table.implementations.find(({ operator }) => nameOf(operator) === record.operator),\n ),\n ),\n\n /**\n * Whether `Conformance.lawsBy` made a record, through a runner: what an SQL\n * table takes. Which runner it was, the database's or another, is the\n * caller's word; a record built by hand, copied, or tested in memory is not.\n */\n testedByRunner,\n\n /**\n * Each operator the table implements, by name, with the laws it declares and\n * the evidence the table records of them: what a server can publish of how\n * it runs a body. JSON as it is.\n */\n describeTable: (base: MemoryOperators): ReadonlyArray<ImplementedOperator> =>\n base.implementations.map(({ operator }) => ({\n ...described(operator),\n evidence: base.evidence.filter(record => record.operator === nameOf(operator)),\n })),\n\n /**\n * An operator as JSON (`OperatorDescription`): its name, version and\n * description, each argument's and its result's stored side as a JSON\n * Schema (as JSON carries it: a number also admits the strings `\"Infinity\"`,\n * `\"-Infinity\"` and `\"NaN\"`), its null and collation rules, its laws, and\n * whether it is defined. Its signature, never its body.\n */\n describe: (operator: { readonly descriptor: OperatorDescriptor }): OperatorDescription =>\n described(operator.descriptor),\n\n /**\n * The table, checking every answer against its operator's output schema, so\n * an implementation that answers the wrong kind fails where it answered. For\n * tests: it costs a decode per call.\n */\n checked: (base: MemoryOperators): MemoryOperators =>\n table(base.implementations, true, base.evidence),\n\n /** The kernel's own operators (`Text.startsWith`): every memory interpreter's default. */\n kernel: table([startsWith], false, []),\n}\n"],"mappings":";;;;;;;;;;;;;AA+CA,MAAa,WAAW,KAAK,WAAqB;;AAMlD,MAAM,aAAa,OAAO,OAAO,OAAO,QAAQ,OAAO,OAAO;;;;;;;AAQ9D,MAAa,sBAAsB,OAAO,OAAO;;CAE/C,MAAM,OAAO;CACb,IAAI,OAAO;CACX,SAAS,OAAO,IAAI,MAAM,OAAO,uBAAuB,CAAC,CAAC;CAC1D,aAAa,OAAO,OAAO,OAAO,MAAM;CACxC,OAAO,OAAO,MAAM,UAAU;CAC9B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EAC1C,GAAG,UAAU,QAAQ;EACrB,UAAU,KAAK,SAAS,QAAO,WAAU,OAAO,aAAa,OAAO,QAAQ,CAAC;CAC/E,EAAE;;;;;;;;CASJ,WAAW,aACT,UAAU,SAAS,UAAU;;;;;;CAO/B,UAAU,SACR,MAAM,KAAK,iBAAiB,MAAM,KAAK,QAAQ;;CAGjD,QAAQ,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC,CAAC;AACvC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Metadata } from "foldkit-metadata";
|
|
2
|
+
export declare const ReadFilter: {
|
|
3
|
+
/** Entity metadata: the rows the server lists depend on these members. */
|
|
4
|
+
of: (members: ReadonlyArray<string>) => Metadata;
|
|
5
|
+
/** The members an Entity's metadata names, every attachment's together. */
|
|
6
|
+
members: (metadata: Metadata) => ReadonlyArray<string>;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=readFilter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readFilter.d.ts","names":[],"sources":["../src/readFilter.ts"],"mappings":";qBAgBa;;EAEX,KAAE,SAAY,0BAAwB;;EAGtC,UAAO,UAAa,aAAW"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Metadata } from "foldkit-metadata";
|
|
2
|
+
// src/readFilter.ts
|
|
3
|
+
/**
|
|
4
|
+
* The members a server filters an Entity's rows by under a rule the client
|
|
5
|
+
* does not run, such as an authorization rule. A client that places rows in
|
|
6
|
+
* a list judges membership by the list's body; a change to one of these
|
|
7
|
+
* members can move a row into or out of what the server lists, which only
|
|
8
|
+
* the server can say. Declared by whoever owns the rule (`foldkit-guard`
|
|
9
|
+
* attaches it), read by whoever places (`foldkit-remote`).
|
|
10
|
+
*/
|
|
11
|
+
const key = Metadata.key("foldkit-entity/read-filter", {
|
|
12
|
+
merge: (lists) => [[...new Set(lists.flat())]],
|
|
13
|
+
summarize: (members) => members.join(", ")
|
|
14
|
+
});
|
|
15
|
+
const ReadFilter = {
|
|
16
|
+
/** Entity metadata: the rows the server lists depend on these members. */
|
|
17
|
+
of: (members) => key.of(members),
|
|
18
|
+
/** The members an Entity's metadata names, every attachment's together. */
|
|
19
|
+
members: (metadata) => key.get(metadata)[0] ?? []
|
|
20
|
+
};
|
|
21
|
+
export { ReadFilter };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=readFilter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readFilter.js","names":[],"sources":["../src/readFilter.ts"],"sourcesContent":["/**\n * The members a server filters an Entity's rows by under a rule the client\n * does not run, such as an authorization rule. A client that places rows in\n * a list judges membership by the list's body; a change to one of these\n * members can move a row into or out of what the server lists, which only\n * the server can say. Declared by whoever owns the rule (`foldkit-guard`\n * attaches it), read by whoever places (`foldkit-remote`).\n */\nimport { Metadata } from 'foldkit-metadata'\n\nconst key = Metadata.key<ReadonlyArray<string>>('foldkit-entity/read-filter', {\n // Every rule attached filters rows, so their members add up.\n merge: lists => [[...new Set(lists.flat())]],\n summarize: members => members.join(', '),\n})\n\nexport const ReadFilter = {\n /** Entity metadata: the rows the server lists depend on these members. */\n of: (members: ReadonlyArray<string>): Metadata => key.of(members),\n\n /** The members an Entity's metadata names, every attachment's together. */\n members: (metadata: Metadata): ReadonlyArray<string> => key.get(metadata)[0] ?? [],\n}\n"],"mappings":";;;;;;;;;;AAUA,MAAM,MAAM,SAAS,IAA2B,8BAA8B;CAE5E,QAAO,UAAS,CAAC,CAAC,GAAG,IAAI,IAAI,MAAM,KAAK,CAAC,CAAC,CAAC;CAC3C,YAAW,YAAW,QAAQ,KAAK,IAAI;AACzC,CAAC;AAED,MAAa,aAAa;;CAExB,KAAK,YAA6C,IAAI,GAAG,OAAO;;CAGhE,UAAU,aAA8C,IAAI,IAAI,QAAQ,CAAC,CAAC,MAAM,CAAC;AACnF"}
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Records keyed by names a caller chose: a field, an input key, an
|
|
3
|
+
* implementation. Read as `record[key]`, `constructor` or `toString` finds
|
|
4
|
+
* `Object.prototype`'s; written as `record[key] = value`, `__proto__` sets the
|
|
5
|
+
* prototype and adds no key.
|
|
6
|
+
*/
|
|
7
|
+
/** The value `record` holds under `key` itself, never one it inherits. */
|
|
8
|
+
export declare const own: <A>(record: Readonly<Record<string, A>>, key: string) => A | undefined;
|
|
9
|
+
/** Sets `key` on `record` as a key of its own, whatever the name. */
|
|
10
|
+
export declare const put: <A>(record: Record<string, A>, key: string, value: A) => void;
|
|
11
|
+
//# sourceMappingURL=record.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.d.ts","names":[],"sources":["../src/record.ts"],"mappings":";;;;;;;qBAQa,MAAO,GAAC,QAAU,SAAS,eAAe,KAAG,gBAAgB;;qBAI7D,MAAO,GAAC,QAAU,eAAe,IAAE,aAAa,OAAS"}
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/record.ts
|
|
2
|
+
/**
|
|
3
|
+
* Records keyed by names a caller chose: a field, an input key, an
|
|
4
|
+
* implementation. Read as `record[key]`, `constructor` or `toString` finds
|
|
5
|
+
* `Object.prototype`'s; written as `record[key] = value`, `__proto__` sets the
|
|
6
|
+
* prototype and adds no key.
|
|
7
|
+
*/
|
|
8
|
+
/** The value `record` holds under `key` itself, never one it inherits. */
|
|
9
|
+
const own = (record, key) => Object.hasOwn(record, key) ? record[key] : void 0;
|
|
10
|
+
/** Sets `key` on `record` as a key of its own, whatever the name. */
|
|
11
|
+
const put = (record, key, value) => {
|
|
12
|
+
Object.defineProperty(record, key, {
|
|
13
|
+
value,
|
|
14
|
+
writable: true,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
export { own, put };
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.js","names":[],"sources":["../src/record.ts"],"sourcesContent":["/**\n * Records keyed by names a caller chose: a field, an input key, an\n * implementation. Read as `record[key]`, `constructor` or `toString` finds\n * `Object.prototype`'s; written as `record[key] = value`, `__proto__` sets the\n * prototype and adds no key.\n */\n\n/** The value `record` holds under `key` itself, never one it inherits. */\nexport const own = <A>(record: Readonly<Record<string, A>>, key: string): A | undefined =>\n Object.hasOwn(record, key) ? record[key] : undefined\n\n/** Sets `key` on `record` as a key of its own, whatever the name. */\nexport const put = <A>(record: Record<string, A>, key: string, value: A): void => {\n Object.defineProperty(record, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true,\n })\n}\n"],"mappings":";;;;;;;;AAQA,MAAa,OAAU,QAAqC,QAC1D,OAAO,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,KAAA;;AAG7C,MAAa,OAAU,QAA2B,KAAa,UAAmB;CAChF,OAAO,eAAe,QAAQ,KAAK;EACjC;EACA,UAAU;EACV,YAAY;EACZ,cAAc;CAChB,CAAC;AACH"}
|
package/dist/sets.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AnyEntity } from "./index.js";
|
|
2
|
+
import { NowExpr, ViewerExpr, ViewerPathExpr } from "./context.js";
|
|
3
|
+
/** The writes that set a member: every insert (`create`), or every insert and update (`write`). */
|
|
4
|
+
export type SetOn = 'create' | 'write';
|
|
5
|
+
/** A value the write's context gives: who asks, what their row holds, or when. */
|
|
6
|
+
export type ContextValue = ViewerExpr<unknown> | ViewerPathExpr<unknown> | NowExpr<unknown>;
|
|
7
|
+
/** One member `Entity.sets` names: its value, and the writes that set it. */
|
|
8
|
+
export interface EntitySet {
|
|
9
|
+
readonly value: ContextValue;
|
|
10
|
+
readonly on: SetOn;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* `value` checked against the member `key` it sets, as `Entity.sets` was
|
|
14
|
+
* given them: a relation takes `Expr.viewer` of its target, a field stored as
|
|
15
|
+
* text the viewer's id, a field stored as a number the time.
|
|
16
|
+
*/
|
|
17
|
+
export declare const checkedSet: (entity: AnyEntity, key: string, value: unknown, on: unknown) => EntitySet;
|
|
18
|
+
//# sourceMappingURL=sets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sets.d.ts","names":[],"sources":["../src/sets.ts"],"mappings":";;;YAUY;;YAGA,eAAe,sBAAsB,0BAA0B;;iBAG1D;WACN,OAAO;WACP,IAAI;;;;;;;qBAuBF,aAAU,QACb,WAAS,aACN,gBACG,gBAEb"}
|
package/dist/sets.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Match, Schema, SchemaAST } from "effect";
|
|
2
|
+
import { SchemaShape } from "./shape.js";
|
|
3
|
+
// src/sets.ts
|
|
4
|
+
/**
|
|
5
|
+
* What `Entity.sets` declares: members whose value is the write's context,
|
|
6
|
+
* never its input, and the writes that set them.
|
|
7
|
+
*/
|
|
8
|
+
const fail = (entity, message) => {
|
|
9
|
+
throw new Error(`Entity "${entity.name}": ${message}`);
|
|
10
|
+
};
|
|
11
|
+
/** How a field's values are stored, nulls aside: as text, as a number, or otherwise. */
|
|
12
|
+
const storedAs = (schema) => {
|
|
13
|
+
const stored = SchemaShape.present(Schema.toEncoded(schema).ast);
|
|
14
|
+
if (stored.every(SchemaShape.isText)) return "text";
|
|
15
|
+
if (stored.every(SchemaAST.isNumber)) return "number";
|
|
16
|
+
return "other";
|
|
17
|
+
};
|
|
18
|
+
const tagOf = (value) => typeof value === "object" && value !== null && "_tag" in value ? value._tag : void 0;
|
|
19
|
+
/**
|
|
20
|
+
* `value` checked against the member `key` it sets, as `Entity.sets` was
|
|
21
|
+
* given them: a relation takes `Expr.viewer` of its target, a field stored as
|
|
22
|
+
* text the viewer's id, a field stored as a number the time.
|
|
23
|
+
*/
|
|
24
|
+
const checkedSet = (entity, key, value, on) => {
|
|
25
|
+
if (on !== "create" && on !== "write") return fail(entity, `sets "${key}" on ${String(on)}; say on: 'create' or on: 'write'`);
|
|
26
|
+
if (key === "id") return fail(entity, "sets the id, which the client names");
|
|
27
|
+
if (Object.hasOwn(entity.sets, key)) return fail(entity, `"${key}" is set already`);
|
|
28
|
+
const tag = tagOf(value);
|
|
29
|
+
if (tag !== "Viewer" && tag !== "Now" && tag !== "ViewerPath") return fail(entity, `sets "${key}" to what is no value of the write's context (Expr.viewer, a path from it, Expr.now); a value from the input is a computed assignment`);
|
|
30
|
+
const context = value;
|
|
31
|
+
const relations = entity.relations;
|
|
32
|
+
const fields = entity.fields;
|
|
33
|
+
const member = Object.hasOwn(relations, key) ? relations[key] : Object.hasOwn(fields, key) ? fields[key] : void 0;
|
|
34
|
+
if (member === void 0) return fail(entity, `sets "${key}", which is not a field or relation`);
|
|
35
|
+
if (member._tag === "Relation") {
|
|
36
|
+
if (member.cardinality !== "one") return fail(entity, `sets "${key}", a many relation`);
|
|
37
|
+
const target = member.target();
|
|
38
|
+
if (context._tag !== "Viewer" || context.entity.token !== target.identity.token) return fail(entity, `sets the relation "${key}" to what is not Expr.viewer(${target.name})`);
|
|
39
|
+
return {
|
|
40
|
+
value: context,
|
|
41
|
+
on
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const stored = storedAs(member.schema);
|
|
45
|
+
Match.value(context).pipe(Match.tagsExhaustive({
|
|
46
|
+
Now: () => stored === "number" || fail(entity, `sets "${key}" to the time, but it is not stored as a number`),
|
|
47
|
+
Viewer: () => stored === "text" || fail(entity, `sets "${key}" to the viewer's id, but it is not stored as text`),
|
|
48
|
+
ViewerPath: ({ field }) => stored !== "other" && storedAs(field.schema) === stored || fail(entity, `sets "${key}" to what ${field.owner.name}.${field.key} holds, which it is not stored as`)
|
|
49
|
+
}));
|
|
50
|
+
return {
|
|
51
|
+
value: context,
|
|
52
|
+
on
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export { checkedSet };
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=sets.js.map
|
package/dist/sets.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sets.js","names":[],"sources":["../src/sets.ts"],"sourcesContent":["/**\n * What `Entity.sets` declares: members whose value is the write's context,\n * never its input, and the writes that set them.\n */\nimport { Match, Schema, SchemaAST } from 'effect'\nimport type { NowExpr, ViewerExpr, ViewerPathExpr } from './context.js'\nimport type { AnyEntity, EntityMember } from './index.js'\nimport { SchemaShape } from './shape.js'\n\n/** The writes that set a member: every insert (`create`), or every insert and update (`write`). */\nexport type SetOn = 'create' | 'write'\n\n/** A value the write's context gives: who asks, what their row holds, or when. */\nexport type ContextValue = ViewerExpr<unknown> | ViewerPathExpr<unknown> | NowExpr<unknown>\n\n/** One member `Entity.sets` names: its value, and the writes that set it. */\nexport interface EntitySet {\n readonly value: ContextValue\n readonly on: SetOn\n}\n\nconst fail = (entity: AnyEntity, message: string): never => {\n throw new Error(`Entity \"${entity.name}\": ${message}`)\n}\n\n/** How a field's values are stored, nulls aside: as text, as a number, or otherwise. */\nconst storedAs = (schema: Schema.Top): 'text' | 'number' | 'other' => {\n const stored = SchemaShape.present(Schema.toEncoded(schema).ast)\n if (stored.every(SchemaShape.isText)) return 'text'\n if (stored.every(SchemaAST.isNumber)) return 'number'\n return 'other'\n}\n\nconst tagOf = (value: unknown): unknown =>\n typeof value === 'object' && value !== null && '_tag' in value ? value._tag : undefined\n\n/**\n * `value` checked against the member `key` it sets, as `Entity.sets` was\n * given them: a relation takes `Expr.viewer` of its target, a field stored as\n * text the viewer's id, a field stored as a number the time.\n */\nexport const checkedSet = (\n entity: AnyEntity,\n key: string,\n value: unknown,\n on: unknown,\n): EntitySet => {\n if (on !== 'create' && on !== 'write') {\n return fail(entity, `sets \"${key}\" on ${String(on)}; say on: 'create' or on: 'write'`)\n }\n if (key === 'id') return fail(entity, 'sets the id, which the client names')\n if (Object.hasOwn(entity.sets, key)) return fail(entity, `\"${key}\" is set already`)\n const tag = tagOf(value)\n if (tag !== 'Viewer' && tag !== 'Now' && tag !== 'ViewerPath') {\n return fail(\n entity,\n `sets \"${key}\" to what is no value of the write's context (Expr.viewer, a path from it, Expr.now); a value from the input is a computed assignment`,\n )\n }\n const context = value as ContextValue\n const relations: Readonly<Record<string, EntityMember>> = entity.relations\n const fields: Readonly<Record<string, EntityMember>> = entity.fields\n const member = Object.hasOwn(relations, key)\n ? relations[key]\n : Object.hasOwn(fields, key)\n ? fields[key]\n : undefined\n if (member === undefined) {\n return fail(entity, `sets \"${key}\", which is not a field or relation`)\n }\n if (member._tag === 'Relation') {\n if (member.cardinality !== 'one') return fail(entity, `sets \"${key}\", a many relation`)\n const target = member.target()\n if (context._tag !== 'Viewer' || context.entity.token !== target.identity.token) {\n return fail(entity, `sets the relation \"${key}\" to what is not Expr.viewer(${target.name})`)\n }\n return { value: context, on }\n }\n const stored = storedAs(member.schema as Schema.Top)\n Match.value(context).pipe(\n Match.tagsExhaustive({\n Now: () =>\n stored === 'number' ||\n fail(entity, `sets \"${key}\" to the time, but it is not stored as a number`),\n Viewer: () =>\n stored === 'text' ||\n fail(entity, `sets \"${key}\" to the viewer's id, but it is not stored as text`),\n ViewerPath: ({ field }) =>\n (stored !== 'other' && storedAs(field.schema as unknown as Schema.Top) === stored) ||\n fail(\n entity,\n `sets \"${key}\" to what ${field.owner.name}.${field.key} holds, which it is not stored as`,\n ),\n }),\n )\n return { value: context, on }\n}\n"],"mappings":";;;;;;;AAqBA,MAAM,QAAQ,QAAmB,YAA2B;CAC1D,MAAM,IAAI,MAAM,WAAW,OAAO,KAAK,KAAK,SAAS;AACvD;;AAGA,MAAM,YAAY,WAAoD;CACpE,MAAM,SAAS,YAAY,QAAQ,OAAO,UAAU,MAAM,CAAC,CAAC,GAAG;CAC/D,IAAI,OAAO,MAAM,YAAY,MAAM,GAAG,OAAO;CAC7C,IAAI,OAAO,MAAM,UAAU,QAAQ,GAAG,OAAO;CAC7C,OAAO;AACT;AAEA,MAAM,S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AC/E,aAAa,EAAE,YACZ,WAAW,WAAW,SAAS,MAAM,MAA+B,MAAM,UAC3E,KACE,QACA,SAAS,IAAI,YAAY,MAAM,MAAM,KAAK,GAAG,MAAM,IAAI,kCACzD;CACJ,CAAC,CACH;CACA,OAAO;EAAE,OAAO;EAAS;CAAG;AAC9B"}
|
package/dist/shape.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SchemaAST } from "effect";
|
|
2
|
+
declare const isNullish: (ast: SchemaAST.AST) => boolean;
|
|
3
|
+
declare const isText: (ast: SchemaAST.AST) => boolean;
|
|
4
|
+
/** What a schema's AST holds, read through Effect's own guards. */
|
|
5
|
+
export declare const SchemaShape: {
|
|
6
|
+
/** True for the `null` and `undefined` members that make a schema optional. */
|
|
7
|
+
isNullish: typeof isNullish;
|
|
8
|
+
/**
|
|
9
|
+
* The members of a schema with `null` and `undefined` set aside, so optional
|
|
10
|
+
* text is still text; a schema that is not a union is its one member.
|
|
11
|
+
*/
|
|
12
|
+
present: (ast: SchemaAST.AST) => ReadonlyArray<SchemaAST.AST>;
|
|
13
|
+
/** True when every value the schema admits is a string. */
|
|
14
|
+
isText: typeof isText;
|
|
15
|
+
/**
|
|
16
|
+
* True when the AST is checked to hold integers (`Schema.isInt`, alone or in
|
|
17
|
+
* a group such as `isInt32`). Read it from a schema's encoded side to ask
|
|
18
|
+
* what the store holds.
|
|
19
|
+
*/
|
|
20
|
+
holdsIntegers: (ast: SchemaAST.AST) => boolean;
|
|
21
|
+
};
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=shape.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.d.ts","names":[],"sources":["../src/shape.ts"],"mappings":";cAEM,YAAS,KAAS,UAAU;cAG5B,SAAM,KAAS,UAAU;;qBAkBlB;;;;;;;EAQI,UAAA,KAAA,UAAU,QAAM,cAAc,UAAU;;;;;;;;EAWlC,gBAAA,KAAA,UAAU"}
|
package/dist/shape.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Match, SchemaAST } from "effect";
|
|
2
|
+
// src/shape.ts
|
|
3
|
+
const isNullish = (ast) => SchemaAST.isNull(ast) || SchemaAST.isUndefined(ast);
|
|
4
|
+
const isText = (ast) => SchemaAST.isString(ast) || SchemaAST.isTemplateLiteral(ast) || SchemaAST.isLiteral(ast) && typeof ast.literal === "string" || SchemaAST.isUnion(ast) && ast.types.length > 0 && ast.types.every(isText);
|
|
5
|
+
/** Whether any of these checks is `Schema.isInt`, alone or inside a group (`isInt32`). */
|
|
6
|
+
const checksInteger = (checks) => checks.some((check) => Match.value(check).pipe(Match.tagsExhaustive({
|
|
7
|
+
Filter: (filter) => filter.annotations?.representation?.id === "effect/schema/isInt",
|
|
8
|
+
FilterGroup: (group) => checksInteger(group.checks)
|
|
9
|
+
})));
|
|
10
|
+
/** What a schema's AST holds, read through Effect's own guards. */
|
|
11
|
+
const SchemaShape = {
|
|
12
|
+
/** True for the `null` and `undefined` members that make a schema optional. */
|
|
13
|
+
isNullish,
|
|
14
|
+
/**
|
|
15
|
+
* The members of a schema with `null` and `undefined` set aside, so optional
|
|
16
|
+
* text is still text; a schema that is not a union is its one member.
|
|
17
|
+
*/
|
|
18
|
+
present: (ast) => SchemaAST.isUnion(ast) ? ast.types.filter((member) => !isNullish(member)) : [ast],
|
|
19
|
+
/** True when every value the schema admits is a string. */
|
|
20
|
+
isText,
|
|
21
|
+
/**
|
|
22
|
+
* True when the AST is checked to hold integers (`Schema.isInt`, alone or in
|
|
23
|
+
* a group such as `isInt32`). Read it from a schema's encoded side to ask
|
|
24
|
+
* what the store holds.
|
|
25
|
+
*/
|
|
26
|
+
holdsIntegers: (ast) => checksInteger(ast.checks ?? [])
|
|
27
|
+
};
|
|
28
|
+
export { SchemaShape };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.js","names":[],"sources":["../src/shape.ts"],"sourcesContent":["import { Match, SchemaAST } from 'effect'\n\nconst isNullish = (ast: SchemaAST.AST): boolean =>\n SchemaAST.isNull(ast) || SchemaAST.isUndefined(ast)\n\nconst isText = (ast: SchemaAST.AST): boolean =>\n SchemaAST.isString(ast) ||\n SchemaAST.isTemplateLiteral(ast) ||\n (SchemaAST.isLiteral(ast) && typeof ast.literal === 'string') ||\n (SchemaAST.isUnion(ast) && ast.types.length > 0 && ast.types.every(isText))\n\n/** Whether any of these checks is `Schema.isInt`, alone or inside a group (`isInt32`). */\nconst checksInteger = (checks: ReadonlyArray<SchemaAST.Check<unknown>>): boolean =>\n checks.some(check =>\n Match.value(check).pipe(\n Match.tagsExhaustive({\n Filter: filter => filter.annotations?.representation?.id === 'effect/schema/isInt',\n FilterGroup: group => checksInteger(group.checks),\n }),\n ),\n )\n\n/** What a schema's AST holds, read through Effect's own guards. */\nexport const SchemaShape = {\n /** True for the `null` and `undefined` members that make a schema optional. */\n isNullish,\n\n /**\n * The members of a schema with `null` and `undefined` set aside, so optional\n * text is still text; a schema that is not a union is its one member.\n */\n present: (ast: SchemaAST.AST): ReadonlyArray<SchemaAST.AST> =>\n SchemaAST.isUnion(ast) ? ast.types.filter(member => !isNullish(member)) : [ast],\n\n /** True when every value the schema admits is a string. */\n isText,\n\n /**\n * True when the AST is checked to hold integers (`Schema.isInt`, alone or in\n * a group such as `isInt32`). Read it from a schema's encoded side to ask\n * what the store holds.\n */\n holdsIntegers: (ast: SchemaAST.AST): boolean => checksInteger(ast.checks ?? []),\n}\n"],"mappings":";;AAEA,MAAM,aAAa,QACjB,UAAU,OAAO,GAAG,KAAK,UAAU,YAAY,GAAG;AAEpD,MAAM,UAAU,QACd,UAAU,SAAS,GAAG,KACtB,UAAU,kBAAkB,GAAG,KAC9B,UAAU,UAAU,GAAG,KAAK,OAAO,IAAI,YAAY,YACnD,UAAU,QAAQ,GAAG,KAAK,IAAI,MAAM,SAAS,KAAK,IAAI,MAAM,MAAM,MAAM;;AAG3E,MAAM,iBAAiB,WACrB,OAAO,MAAK,UACV,MAAM,MAAM,KAAK,CAAC,CAAC,KACjB,MAAM,eAAe;CACnB,SAAQ,WAAU,OAAO,aAAa,gBAAgB,OAAO;CAC7D,cAAa,UAAS,cAAc,MAAM,MAAM;AAClD,CAAC,CACH,CACF;;AAGF,MAAa,cAAc;;CAEzB;;;;;CAMA,UAAU,QACR,UAAU,QAAQ,GAAG,IAAI,IAAI,MAAM,QAAO,WAAU,CAAC,UAAU,MAAM,CAAC,IAAI,CAAC,GAAG;;CAGhF;;;;;;CAOA,gBAAgB,QAAgC,cAAc,IAAI,UAAU,CAAC,CAAC;AAChF"}
|
package/dist/tested.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { Evidence, MemoryOperators } from "./operators.js";
|
|
3
|
+
export declare const stampTested: (record: Evidence, operators: MemoryOperators) => void;
|
|
4
|
+
export declare const stampRun: (record: Evidence) => void;
|
|
5
|
+
/** The table `Conformance.laws` tested a record on. */
|
|
6
|
+
export declare const tableTested: (record: Evidence) => Option.Option<MemoryOperators>;
|
|
7
|
+
export declare const testedByRunner: (record: Evidence) => boolean;
|
|
8
|
+
//# sourceMappingURL=tested.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tested.d.ts","names":[],"sources":["../src/tested.ts"],"mappings":";;qBAea,cAAW,QAAY,UAAQ,WAAa;qBAI5C,WAAQ,QAAY;;qBAKpB,cAAW,QAAY,aAAW,OAAO,OAAO;qBAGhD,iBAAc,QAAY"}
|
package/dist/tested.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
// src/tested.ts
|
|
3
|
+
/**
|
|
4
|
+
* What made each `Tested` record: `Conformance.laws` over one memory table,
|
|
5
|
+
* or `Conformance.lawsBy` through a runner. A table records only evidence of
|
|
6
|
+
* its own kind, so a record built by hand, copied, or tested on another
|
|
7
|
+
* implementation (of its operator, or of a witness its law calls) is refused.
|
|
8
|
+
* Kept beside the records rather than in them: a record is JSON a server
|
|
9
|
+
* publishes, and an implementation is a function. Not exported from the
|
|
10
|
+
* package.
|
|
11
|
+
*/
|
|
12
|
+
const testedOn = /* @__PURE__ */ new WeakMap();
|
|
13
|
+
const ranByRunner = /* @__PURE__ */ new WeakSet();
|
|
14
|
+
const stampTested = (record, operators) => {
|
|
15
|
+
testedOn.set(record, operators);
|
|
16
|
+
};
|
|
17
|
+
const stampRun = (record) => {
|
|
18
|
+
ranByRunner.add(record);
|
|
19
|
+
};
|
|
20
|
+
/** The table `Conformance.laws` tested a record on. */
|
|
21
|
+
const tableTested = (record) => Option.fromUndefinedOr(testedOn.get(record));
|
|
22
|
+
const testedByRunner = (record) => ranByRunner.has(record);
|
|
23
|
+
export { stampRun, stampTested, tableTested, testedByRunner };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=tested.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tested.js","names":[],"sources":["../src/tested.ts"],"sourcesContent":["/**\n * What made each `Tested` record: `Conformance.laws` over one memory table,\n * or `Conformance.lawsBy` through a runner. A table records only evidence of\n * its own kind, so a record built by hand, copied, or tested on another\n * implementation (of its operator, or of a witness its law calls) is refused.\n * Kept beside the records rather than in them: a record is JSON a server\n * publishes, and an implementation is a function. Not exported from the\n * package.\n */\nimport { Option } from 'effect'\nimport type { Evidence, MemoryOperators } from './operators.js'\n\nconst testedOn = new WeakMap<Evidence, MemoryOperators>()\nconst ranByRunner = new WeakSet<Evidence>()\n\nexport const stampTested = (record: Evidence, operators: MemoryOperators): void => {\n testedOn.set(record, operators)\n}\n\nexport const stampRun = (record: Evidence): void => {\n ranByRunner.add(record)\n}\n\n/** The table `Conformance.laws` tested a record on. */\nexport const tableTested = (record: Evidence): Option.Option<MemoryOperators> =>\n Option.fromUndefinedOr(testedOn.get(record))\n\nexport const testedByRunner = (record: Evidence): boolean => ranByRunner.has(record)\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,2BAAW,IAAI,QAAmC;AACxD,MAAM,8BAAc,IAAI,QAAkB;AAE1C,MAAa,eAAe,QAAkB,cAAqC;CACjF,SAAS,IAAI,QAAQ,SAAS;AAChC;AAEA,MAAa,YAAY,WAA2B;CAClD,YAAY,IAAI,MAAM;AACxB;;AAGA,MAAa,eAAe,WAC1B,OAAO,gBAAgB,SAAS,IAAI,MAAM,CAAC;AAE7C,MAAa,kBAAkB,WAA8B,YAAY,IAAI,MAAM"}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export declare const Text: {
|
|
3
|
+
/**
|
|
4
|
+
* Whether the first text begins with the second, under the first's collation.
|
|
5
|
+
*
|
|
6
|
+
* | Case | Answer |
|
|
7
|
+
* | --- | --- |
|
|
8
|
+
* | either is null | unknown |
|
|
9
|
+
* | the prefix is `''` | true |
|
|
10
|
+
* | otherwise | whether it begins so, compared under the collation |
|
|
11
|
+
*
|
|
12
|
+
* The collation is the one its text operands share (a field's declared one,
|
|
13
|
+
* or a call's), or one given to the call. `binary` compares exactly;
|
|
14
|
+
* `asciiFold` folds ASCII letters on both sides. Operands declaring
|
|
15
|
+
* different collations, text with none, or a backend's locale are refused
|
|
16
|
+
* where the call is written.
|
|
17
|
+
* Prefixes are by character, so non-ASCII and astral text begin as they read,
|
|
18
|
+
* and half of an astral character begins nothing. A lone surrogate, which
|
|
19
|
+
* UTF-8 cannot hold, compares as U+FFFD, as a database stores it.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* Query.where(Text.startsWith(Product.fields.upc, input.search))
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
startsWith: import("./expr.js").Operator<readonly [Schema.NullOr<Schema.String>, Schema.NullOr<Schema.String>], Schema.Boolean>;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.d.ts","names":[],"sources":["../src/text.ts"],"mappings":";qBAOa;;;;;;;;;;;;;;;;;;;;;;;EAuBX,gCAAU,mBAAA,OAAA,OAAA,OAAA,SAAA,OAAA,OAAA,OAAA,UAAA,OAAA"}
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Expr } from "./expr.js";
|
|
3
|
+
// src/text.ts
|
|
4
|
+
/**
|
|
5
|
+
* The kernel's text operators: primitive, so each interpreter implements them,
|
|
6
|
+
* and each states what it means here, where data-query §6.0.1 holds the rest.
|
|
7
|
+
*/
|
|
8
|
+
const Text = {
|
|
9
|
+
/**
|
|
10
|
+
* Whether the first text begins with the second, under the first's collation.
|
|
11
|
+
*
|
|
12
|
+
* | Case | Answer |
|
|
13
|
+
* | --- | --- |
|
|
14
|
+
* | either is null | unknown |
|
|
15
|
+
* | the prefix is `''` | true |
|
|
16
|
+
* | otherwise | whether it begins so, compared under the collation |
|
|
17
|
+
*
|
|
18
|
+
* The collation is the one its text operands share (a field's declared one,
|
|
19
|
+
* or a call's), or one given to the call. `binary` compares exactly;
|
|
20
|
+
* `asciiFold` folds ASCII letters on both sides. Operands declaring
|
|
21
|
+
* different collations, text with none, or a backend's locale are refused
|
|
22
|
+
* where the call is written.
|
|
23
|
+
* Prefixes are by character, so non-ASCII and astral text begin as they read,
|
|
24
|
+
* and half of an astral character begins nothing. A lone surrogate, which
|
|
25
|
+
* UTF-8 cannot hold, compares as U+FFFD, as a database stores it.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* Query.where(Text.startsWith(Product.fields.upc, input.search))
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
startsWith: Expr.operator("text.startsWith", {
|
|
32
|
+
input: [Schema.NullOr(Schema.String), Schema.NullOr(Schema.String)],
|
|
33
|
+
output: Schema.Boolean,
|
|
34
|
+
collation: "required",
|
|
35
|
+
description: "Whether the first text begins with the second, under the first's collation."
|
|
36
|
+
}) };
|
|
37
|
+
export { Text };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=text.js.map
|