foldkit-entity 0.17.0 → 0.19.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 +475 -227
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +10 -4
- package/dist/conformance/index.js.map +1 -1
- package/dist/descriptor.d.ts +5 -1
- package/dist/descriptor.d.ts.map +1 -1
- package/dist/descriptor.js +6 -2
- package/dist/descriptor.js.map +1 -1
- package/dist/entity.d.ts +3 -2
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +5 -3
- package/dist/entity.js.map +1 -1
- package/dist/enumerate.d.ts +44 -0
- package/dist/enumerate.d.ts.map +1 -0
- package/dist/enumerate.js +128 -0
- package/dist/enumerate.js.map +1 -0
- package/dist/evaluate.d.ts +11 -5
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +27 -6
- package/dist/evaluate.js.map +1 -1
- package/dist/expr.d.ts +8 -2
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +19 -10
- package/dist/expr.js.map +1 -1
- package/dist/footprint.d.ts +120 -0
- package/dist/footprint.d.ts.map +1 -0
- package/dist/footprint.js +41 -0
- package/dist/footprint.js.map +1 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/invariant.d.ts +41 -11
- package/dist/invariant.d.ts.map +1 -1
- package/dist/invariant.js +83 -38
- package/dist/invariant.js.map +1 -1
- package/dist/lawIds.d.ts +15 -0
- package/dist/lawIds.d.ts.map +1 -0
- package/dist/lawIds.js +13 -0
- package/dist/lawIds.js.map +1 -0
- package/dist/laws.d.ts +308 -0
- package/dist/laws.d.ts.map +1 -0
- package/dist/laws.js +566 -0
- package/dist/laws.js.map +1 -0
- package/dist/operators.d.ts +1 -1
- package/dist/refine.d.ts +64 -0
- package/dist/refine.d.ts.map +1 -0
- package/dist/refine.js +104 -0
- package/dist/refine.js.map +1 -0
- package/dist/rows.d.ts +15 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +23 -0
- package/dist/rows.js.map +1 -0
- package/dist/uncollated.d.ts +10 -0
- package/dist/uncollated.d.ts.map +1 -0
- package/dist/uncollated.js +18 -0
- package/dist/uncollated.js.map +1 -0
- package/dist/write.d.ts +64 -6
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +88 -12
- package/dist/write.js.map +1 -1
- package/package.json +3 -3
package/dist/refine.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { AnyEntity } from "./index.js";
|
|
3
|
+
import { BeforeRow } from "./invariant.js";
|
|
4
|
+
import { Row } from "./evaluate.js";
|
|
5
|
+
import { Predicate, Reads } from "./expr.js";
|
|
6
|
+
import { MemoryOperators as MemoryOperators$1 } from "./operators.js";
|
|
7
|
+
/**
|
|
8
|
+
* A named state of `E`'s row. `Narrow` is what `is` narrows a row to: the
|
|
9
|
+
* fields a literal form names, nothing for a predicate form.
|
|
10
|
+
*/
|
|
11
|
+
export interface Refinement<E extends AnyEntity, Name extends string, Narrow> {
|
|
12
|
+
readonly _tag: 'Refinement';
|
|
13
|
+
readonly entity: E;
|
|
14
|
+
readonly name: Name;
|
|
15
|
+
/** Over the row as stored: a `Query.where`, a guard row rule, a write's `when`. */
|
|
16
|
+
readonly predicate: Predicate & Reads<E['name']>;
|
|
17
|
+
/**
|
|
18
|
+
* Over the row a write leaves (each field as `Expr.next`): what an invariant
|
|
19
|
+
* reads. Throws for a refinement that reaches another row.
|
|
20
|
+
*/
|
|
21
|
+
readonly after: Predicate & Reads<E['name']>;
|
|
22
|
+
/** Whether it reads its own row alone: only such a one is a write's `from` or `becomes`. */
|
|
23
|
+
readonly local: boolean;
|
|
24
|
+
/** The fields of its own row it reads, by key. */
|
|
25
|
+
readonly reads: ReadonlyArray<string>;
|
|
26
|
+
/**
|
|
27
|
+
* Whether a row (as an application holds it, decoded) is definitely in the
|
|
28
|
+
* state, narrowing the fields a literal form names. False where it lacks a
|
|
29
|
+
* field the state reads, and for a refinement reaching another row.
|
|
30
|
+
*/
|
|
31
|
+
is<R extends object>(row: R): row is R & Narrow;
|
|
32
|
+
}
|
|
33
|
+
export type AnyRefinement = Refinement<AnyEntity, string, unknown>;
|
|
34
|
+
/** A refinement of the Entity named `Name`, as a write over any descriptor of it takes one. */
|
|
35
|
+
export type RefinementOf<E extends AnyEntity> = Refinement<AnyEntity & {
|
|
36
|
+
readonly name: E['name'];
|
|
37
|
+
}, string, unknown>;
|
|
38
|
+
/** The literal form: a value per field, each typed by what the field stores. */
|
|
39
|
+
export type RefinementLiteral<E extends AnyEntity> = { readonly [K in keyof E['fields'] & string]?: Schema.Codec.Encoded<E['fields'][K]['schema']>; };
|
|
40
|
+
/** The predicate form: built over `row`, each field as `Expr.field`, and nothing of the past. */
|
|
41
|
+
export type RefinementBuild<E extends AnyEntity> = (rows: {
|
|
42
|
+
readonly row: BeforeRow<E>;
|
|
43
|
+
}) => Predicate & Reads<E['name']>;
|
|
44
|
+
/**
|
|
45
|
+
* Declares a named state of `entity`'s row, on the Entity itself (every
|
|
46
|
+
* descriptor of it sees it), and returns it. The literal form is a value per
|
|
47
|
+
* field (`{ status: 'draft' }`); the predicate form is built over `row`
|
|
48
|
+
* (`({ row }) => Expr.gt(row.stock, row.reserved)`), and may reach another
|
|
49
|
+
* row (`Expr.exists`, `Expr.path`), which makes it no write's `from` or
|
|
50
|
+
* `becomes`. A name declared twice on one Entity throws.
|
|
51
|
+
*/
|
|
52
|
+
export declare function refine<E extends AnyEntity, const Name extends string, const L extends RefinementLiteral<E>>(entity: E, name: Name, literal: L & { readonly [K in Exclude<keyof L, keyof E['fields']>]: never; }): Refinement<E, Name, { readonly [K in keyof L]: L[K]; }>;
|
|
53
|
+
export declare function refine<E extends AnyEntity, const Name extends string>(entity: E, name: Name, build: RefinementBuild<E>): Refinement<E, Name, {}>;
|
|
54
|
+
/** Every state declared on the Entity, in the order they were declared. */
|
|
55
|
+
export declare const states: (entity: AnyEntity) => ReadonlyArray<AnyRefinement>;
|
|
56
|
+
/**
|
|
57
|
+
* Whether a row as stored (encoded, as `Entity.judge` reads it) is in the
|
|
58
|
+
* state: none where the row lacks a field the state reads, an operator it calls
|
|
59
|
+
* is not in `operators`, or the state reaches another row.
|
|
60
|
+
*/
|
|
61
|
+
export declare const inState: (refinement: AnyRefinement, row: Row, operators?: MemoryOperators$1) => Option.Option<boolean>;
|
|
62
|
+
/** Refuses a refinement of another Entity, or one reaching another row, as a write's `role`. */
|
|
63
|
+
export declare const localTo: (entity: AnyEntity, refinement: AnyRefinement, role: 'from' | 'becomes') => AnyRefinement;
|
|
64
|
+
//# sourceMappingURL=refine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.d.ts","names":["MemoryOperators"],"sources":["../src/refine.ts"],"mappings":";;;;;;;;;;iBA+BiB,WAAW,UAAU,WAAW,qBAAqB;WAC3D;WACA,QAAQ;WACR,MAAM;;WAEN,WAAW,YAAY,MAAM;;;;;WAK7B,OAAO,YAAY,MAAM;;WAEzB;;WAEA,OAAO;;;;;;EAMhB,GAAG,kBAAkB,KAAK,IAAI,OAAO,IAAI;;YAG/B,gBAAgB,WAAW;;YAG3B,aAAa,UAAU,aAAa,WAC9C;WAAuB,MAAM;;;YAMnB,kBAAkB,UAAU,yBAC5B,WAAW,wBAAwB,OAAO,MAAM,QAAQ,YAAY;;YAIpE,gBAAgB,UAAU,cAAc;WACzC,KAAK,UAAU;MACpB,YAAY,MAAM;;;;;;;;;wBAuER,OACd,UAAU,iBACJ,2BACA,UAAU,kBAAkB,IAElC,QAAQ,GACR,MAAM,MACN,SAAS,gBAAgB,KAAK,cAAc,SAAS,0BACpD,WAAW,GAAG,kBAAkB,WAAW,IAAI,EAAE;wBACpC,OAAO,UAAU,iBAAiB,qBAChD,QAAQ,GACR,MAAM,MACN,OAAO,gBAAgB,KACtB,WAAW,GAAG;;qBA2DJ,SAAM,QAAY,cAAY,cAAc;;;;;;qBAQ5C,UAAO,YACN,eAAa,KACpB,KAAG,YACGA,sBACV,OAAO;;qBAGG,UAAO,QACV,WAAS,YACL,eAAa,6BAExB"}
|
package/dist/refine.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Match, Option, Schema } from "effect";
|
|
2
|
+
import { heldRow, oneRelations } from "./rows.js";
|
|
3
|
+
import { Judge, OperatorMissingError } from "./evaluate.js";
|
|
4
|
+
import { Expr, Query, nameNode } from "./expr.js";
|
|
5
|
+
import { kernelOperators } from "./memoryTable.js";
|
|
6
|
+
// src/refine.ts
|
|
7
|
+
/**
|
|
8
|
+
* Refinements: a named state of an Entity's row (`Draft`, `Published`,
|
|
9
|
+
* `InStock`), declared once on the Entity and read as a value. Not a
|
|
10
|
+
* descriptor: every reader of an Entity would have to tell one apart, and one
|
|
11
|
+
* that did not would treat `Draft` as `Product` and say nothing. A query, a
|
|
12
|
+
* guard rule or a write's `from` and `becomes` read the predicate it holds.
|
|
13
|
+
*/
|
|
14
|
+
const declared = /* @__PURE__ */ new WeakMap();
|
|
15
|
+
const fail$1 = (entity, name, message) => {
|
|
16
|
+
throw new Error(`[foldkit-entity] Entity.refine ${entity.name}.${name}: ${message}`);
|
|
17
|
+
};
|
|
18
|
+
/** The row `build` reads: each field, and each `one` relation as the id it points at, read by `read`. */
|
|
19
|
+
const rowOf = (entity, read) => {
|
|
20
|
+
const links = oneRelations(entity);
|
|
21
|
+
return Object.fromEntries([...Object.entries(entity.fields).map(([key, field]) => [key, read(field)]), ...links.map((link) => [link.key, read(link)])]);
|
|
22
|
+
};
|
|
23
|
+
const literalPredicate = (entity, name, literal, row) => {
|
|
24
|
+
const fields = entity.fields;
|
|
25
|
+
const checks = Object.entries(literal).map(([key, value]) => {
|
|
26
|
+
if (!Object.hasOwn(fields, key)) return fail$1(entity, name, `"${key}" is not a field`);
|
|
27
|
+
const stored = Schema.toEncoded(fields[key].schema);
|
|
28
|
+
if (!Schema.is(stored)(value)) fail$1(entity, name, `${JSON.stringify(value)} is no value "${key}" stores`);
|
|
29
|
+
return value === null ? Expr.isNull(row[key]) : Expr.eq(row[key], value);
|
|
30
|
+
});
|
|
31
|
+
if (checks.length === 0) fail$1(entity, name, "the literal form names no field");
|
|
32
|
+
return checks.length === 1 ? checks[0] : Expr.and(checks[0], ...checks.slice(1));
|
|
33
|
+
};
|
|
34
|
+
/** Whether a stored row is in the state: none where it lacks a field the state reads. */
|
|
35
|
+
const judged = (refinement, row, operators) => {
|
|
36
|
+
if (!refinement.local) return Option.none();
|
|
37
|
+
try {
|
|
38
|
+
const verdict = Judge.predicate(refinement.predicate, heldRow(refinement.entity, row), { operators });
|
|
39
|
+
return Match.valueTags(verdict, {
|
|
40
|
+
Unknown: () => Option.none(),
|
|
41
|
+
Known: ({ truth }) => Option.some(truth === true)
|
|
42
|
+
});
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof OperatorMissingError) return Option.none();
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
function refine(entity, name, form) {
|
|
49
|
+
if (name.length === 0) fail$1(entity, name, "a state needs a name");
|
|
50
|
+
const known = declared.get(entity.identity) ?? [];
|
|
51
|
+
if (known.some((state) => state.name === name)) fail$1(entity, name, "is declared twice");
|
|
52
|
+
const build = (row) => typeof form === "function" ? form({ row }) : literalPredicate(entity, name, form, row);
|
|
53
|
+
const predicate = build(rowOf(entity, (member) => Expr.field(member)));
|
|
54
|
+
const read = Query.dependencies(Query.from(entity).pipe(Query.where(predicate)));
|
|
55
|
+
if (read.inputs.length > 0) fail$1(entity, name, `reads the input ${read.inputs.join(", ")}; a state is of the row alone`);
|
|
56
|
+
if (read.next.length > 0) fail$1(entity, name, "reads the row after a write; build it over `row`");
|
|
57
|
+
const local = read.reaches.length === 0;
|
|
58
|
+
const after = local ? Option.some(build(rowOf(entity, (member) => Expr.next(member)))) : Option.none();
|
|
59
|
+
nameNode(predicate, `${entity.name}.${name}`);
|
|
60
|
+
Option.map(after, (node) => nameNode(node, `next ${entity.name}.${name}`));
|
|
61
|
+
const reads = Object.freeze(read.predicate.map((field) => field.key));
|
|
62
|
+
const refinement = Object.freeze({
|
|
63
|
+
_tag: "Refinement",
|
|
64
|
+
entity,
|
|
65
|
+
name,
|
|
66
|
+
predicate,
|
|
67
|
+
get after() {
|
|
68
|
+
return Option.getOrElse(after, () => fail$1(entity, name, "reaches another row, so it has no row after a write"));
|
|
69
|
+
},
|
|
70
|
+
local,
|
|
71
|
+
reads,
|
|
72
|
+
is: (row) => {
|
|
73
|
+
const fields = entity.fields;
|
|
74
|
+
const values = row;
|
|
75
|
+
const stored = {};
|
|
76
|
+
for (const key of reads) {
|
|
77
|
+
if (!Object.hasOwn(values, key)) return false;
|
|
78
|
+
const encoded = Schema.encodeUnknownOption(fields[key].schema)(values[key]);
|
|
79
|
+
if (Option.isNone(encoded)) return false;
|
|
80
|
+
stored[key] = encoded.value;
|
|
81
|
+
}
|
|
82
|
+
return Option.getOrElse(judged(refinement, stored, kernelOperators), () => false);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
declared.set(entity.identity, [...known, refinement]);
|
|
86
|
+
return refinement;
|
|
87
|
+
}
|
|
88
|
+
/** Every state declared on the Entity, in the order they were declared. */
|
|
89
|
+
const states = (entity) => declared.get(entity.identity) ?? [];
|
|
90
|
+
/**
|
|
91
|
+
* Whether a row as stored (encoded, as `Entity.judge` reads it) is in the
|
|
92
|
+
* state: none where the row lacks a field the state reads, an operator it calls
|
|
93
|
+
* is not in `operators`, or the state reaches another row.
|
|
94
|
+
*/
|
|
95
|
+
const inState = (refinement, row, operators = kernelOperators) => judged(refinement, row, operators);
|
|
96
|
+
/** Refuses a refinement of another Entity, or one reaching another row, as a write's `role`. */
|
|
97
|
+
const localTo = (entity, refinement, role) => {
|
|
98
|
+
if (refinement.entity.identity !== entity.identity) throw new Error(`[foldkit-entity] Write of ${entity.name}: its ${role} is ${refinement.entity.name}.${refinement.name}, a state of another Entity`);
|
|
99
|
+
if (!refinement.local) throw new Error(`[foldkit-entity] Write of ${entity.name}: its ${role} ${entity.name}.${refinement.name} reaches another row, and a writer judges the row it writes alone`);
|
|
100
|
+
return refinement;
|
|
101
|
+
};
|
|
102
|
+
export { inState, localTo, refine, states };
|
|
103
|
+
|
|
104
|
+
//# sourceMappingURL=refine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.js","names":["fail"],"sources":["../src/refine.ts"],"sourcesContent":["/**\n * Refinements: a named state of an Entity's row (`Draft`, `Published`,\n * `InStock`), declared once on the Entity and read as a value. Not a\n * descriptor: every reader of an Entity would have to tell one apart, and one\n * that did not would treat `Draft` as `Product` and say nothing. A query, a\n * guard rule or a write's `from` and `becomes` read the predicate it holds.\n */\nimport { Match, Option, Schema } from 'effect'\nimport { Judge, OperatorMissingError, type Row } from './evaluate.js'\nimport {\n Expr,\n Query,\n nameNode,\n type FieldExpr,\n type NextExpr,\n type OneRelation,\n type Predicate,\n type Reads,\n} from './expr.js'\nimport type { AnyEntity, EntityField, EntityIdentity } from './index.js'\nimport type { BeforeRow } from './invariant.js'\nimport { kernelOperators } from './memoryTable.js'\nimport { heldRow, oneRelations } from './rows.js'\nimport type { MemoryOperators } from './operators.js'\n\ntype AnyField = EntityField<string, string, Schema.Constraint>\n\n/**\n * A named state of `E`'s row. `Narrow` is what `is` narrows a row to: the\n * fields a literal form names, nothing for a predicate form.\n */\nexport interface Refinement<E extends AnyEntity, Name extends string, Narrow> {\n readonly _tag: 'Refinement'\n readonly entity: E\n readonly name: Name\n /** Over the row as stored: a `Query.where`, a guard row rule, a write's `when`. */\n readonly predicate: Predicate & Reads<E['name']>\n /**\n * Over the row a write leaves (each field as `Expr.next`): what an invariant\n * reads. Throws for a refinement that reaches another row.\n */\n readonly after: Predicate & Reads<E['name']>\n /** Whether it reads its own row alone: only such a one is a write's `from` or `becomes`. */\n readonly local: boolean\n /** The fields of its own row it reads, by key. */\n readonly reads: ReadonlyArray<string>\n /**\n * Whether a row (as an application holds it, decoded) is definitely in the\n * state, narrowing the fields a literal form names. False where it lacks a\n * field the state reads, and for a refinement reaching another row.\n */\n is<R extends object>(row: R): row is R & Narrow\n}\n\nexport type AnyRefinement = Refinement<AnyEntity, string, unknown>\n\n/** A refinement of the Entity named `Name`, as a write over any descriptor of it takes one. */\nexport type RefinementOf<E extends AnyEntity> = Refinement<\n AnyEntity & { readonly name: E['name'] },\n string,\n unknown\n>\n\n/** The literal form: a value per field, each typed by what the field stores. */\nexport type RefinementLiteral<E extends AnyEntity> = {\n readonly [K in keyof E['fields'] & string]?: Schema.Codec.Encoded<E['fields'][K]['schema']>\n}\n\n/** The predicate form: built over `row`, each field as `Expr.field`, and nothing of the past. */\nexport type RefinementBuild<E extends AnyEntity> = (rows: {\n readonly row: BeforeRow<E>\n}) => Predicate & Reads<E['name']>\n\nconst declared = new WeakMap<EntityIdentity<string>, Array<AnyRefinement>>()\n\nconst fail = (entity: AnyEntity, name: string, message: string): never => {\n throw new Error(`[foldkit-entity] Entity.refine ${entity.name}.${name}: ${message}`)\n}\n\n/** The row `build` reads: each field, and each `one` relation as the id it points at, read by `read`. */\nconst rowOf = (\n entity: AnyEntity,\n read: (member: AnyField | OneRelation) => FieldExpr<unknown> | NextExpr<unknown>,\n): Readonly<Record<string, unknown>> => {\n const links = oneRelations(entity)\n return Object.fromEntries([\n ...Object.entries(entity.fields as Readonly<Record<string, AnyField>>).map(\n ([key, field]) => [key, read(field)] as const,\n ),\n ...links.map(link => [link.key, read(link)] as const),\n ])\n}\n\nconst literalPredicate = (\n entity: AnyEntity,\n name: string,\n literal: Readonly<Record<string, unknown>>,\n row: Readonly<Record<string, unknown>>,\n): Predicate => {\n const fields = entity.fields as Readonly<Record<string, AnyField>>\n const checks = Object.entries(literal).map(([key, value]) => {\n if (!Object.hasOwn(fields, key)) return fail(entity, name, `\"${key}\" is not a field`)\n const stored = Schema.toEncoded(fields[key]!.schema as unknown as Schema.Top)\n if (!Schema.is(stored as Schema.Codec<unknown>)(value))\n fail(entity, name, `${JSON.stringify(value)} is no value \"${key}\" stores`)\n return value === null\n ? Expr.isNull(row[key] as FieldExpr<unknown>)\n : Expr.eq(row[key] as FieldExpr<unknown>, value as never)\n })\n if (checks.length === 0) fail(entity, name, 'the literal form names no field')\n return checks.length === 1 ? checks[0]! : Expr.and(checks[0]!, ...checks.slice(1))\n}\n\n/** Whether a stored row is in the state: none where it lacks a field the state reads. */\nconst judged = (\n refinement: AnyRefinement,\n row: Row,\n operators: MemoryOperators,\n): Option.Option<boolean> => {\n if (!refinement.local) return Option.none()\n try {\n const verdict = Judge.predicate(refinement.predicate, heldRow(refinement.entity, row), {\n operators,\n })\n return Match.valueTags(verdict, {\n Unknown: () => Option.none(),\n Known: ({ truth }) => Option.some(truth === true),\n })\n } catch (error) {\n if (error instanceof OperatorMissingError) return Option.none()\n throw error\n }\n}\n\n/**\n * Declares a named state of `entity`'s row, on the Entity itself (every\n * descriptor of it sees it), and returns it. The literal form is a value per\n * field (`{ status: 'draft' }`); the predicate form is built over `row`\n * (`({ row }) => Expr.gt(row.stock, row.reserved)`), and may reach another\n * row (`Expr.exists`, `Expr.path`), which makes it no write's `from` or\n * `becomes`. A name declared twice on one Entity throws.\n */\nexport function refine<\n E extends AnyEntity,\n const Name extends string,\n const L extends RefinementLiteral<E>,\n>(\n entity: E,\n name: Name,\n literal: L & { readonly [K in Exclude<keyof L, keyof E['fields']>]: never },\n): Refinement<E, Name, { readonly [K in keyof L]: L[K] }>\nexport function refine<E extends AnyEntity, const Name extends string>(\n entity: E,\n name: Name,\n build: RefinementBuild<E>,\n): Refinement<E, Name, {}>\nexport function refine(\n entity: AnyEntity,\n name: string,\n form: Readonly<Record<string, unknown>> | RefinementBuild<AnyEntity>,\n): AnyRefinement {\n if (name.length === 0) fail(entity, name, 'a state needs a name')\n const known = declared.get(entity.identity) ?? []\n if (known.some(state => state.name === name)) fail(entity, name, 'is declared twice')\n const build = (row: Readonly<Record<string, unknown>>): Predicate =>\n typeof form === 'function'\n ? (form({ row: row as BeforeRow<AnyEntity> }) as Predicate)\n : literalPredicate(entity, name, form, row)\n const predicate = build(rowOf(entity, member => Expr.field(member as never)))\n // `where` refuses a predicate over another Entity, which the type allows when it is unmarked.\n const read = Query.dependencies(Query.from(entity).pipe(Query.where(predicate)))\n if (read.inputs.length > 0)\n fail(entity, name, `reads the input ${read.inputs.join(', ')}; a state is of the row alone`)\n if (read.next.length > 0) fail(entity, name, 'reads the row after a write; build it over `row`')\n const local = read.reaches.length === 0\n // Built again over the row after, as an invariant's `after` is; a reach would still read the row before.\n const after = local\n ? Option.some(build(rowOf(entity, member => Expr.next(member as never) as never)))\n : Option.none<Predicate>()\n nameNode(predicate, `${entity.name}.${name}`)\n Option.map(after, node => nameNode(node, `next ${entity.name}.${name}`))\n const reads = Object.freeze(read.predicate.map(field => field.key))\n const refinement: AnyRefinement = Object.freeze({\n _tag: 'Refinement' as const,\n entity,\n name,\n predicate,\n get after(): Predicate {\n return Option.getOrElse(after, () =>\n fail(entity, name, 'reaches another row, so it has no row after a write'),\n )\n },\n local,\n reads,\n is: (row: object): row is never => {\n const fields = entity.fields as Readonly<Record<string, AnyField>>\n const values = row as Readonly<Record<string, unknown>>\n const stored: Record<string, unknown> = {}\n for (const key of reads) {\n if (!Object.hasOwn(values, key)) return false\n const encoded = Schema.encodeUnknownOption(\n fields[key]!.schema as unknown as Schema.Codec<unknown, unknown>,\n )(values[key])\n if (Option.isNone(encoded)) return false\n stored[key] = encoded.value\n }\n return Option.getOrElse(judged(refinement, stored, kernelOperators), () => false)\n },\n })\n declared.set(entity.identity, [...known, refinement])\n return refinement\n}\n\n/** Every state declared on the Entity, in the order they were declared. */\nexport const states = (entity: AnyEntity): ReadonlyArray<AnyRefinement> =>\n declared.get(entity.identity) ?? []\n\n/**\n * Whether a row as stored (encoded, as `Entity.judge` reads it) is in the\n * state: none where the row lacks a field the state reads, an operator it calls\n * is not in `operators`, or the state reaches another row.\n */\nexport const inState = (\n refinement: AnyRefinement,\n row: Row,\n operators: MemoryOperators = kernelOperators,\n): Option.Option<boolean> => judged(refinement, row, operators)\n\n/** Refuses a refinement of another Entity, or one reaching another row, as a write's `role`. */\nexport const localTo = (\n entity: AnyEntity,\n refinement: AnyRefinement,\n role: 'from' | 'becomes',\n): AnyRefinement => {\n if (refinement.entity.identity !== entity.identity)\n throw new Error(\n `[foldkit-entity] Write of ${entity.name}: its ${role} is ${refinement.entity.name}.${refinement.name}, a state of another Entity`,\n )\n if (!refinement.local)\n throw new Error(\n `[foldkit-entity] Write of ${entity.name}: its ${role} ${entity.name}.${refinement.name} reaches another row, and a writer judges the row it writes alone`,\n )\n return refinement\n}\n"],"mappings":";;;;;;;;;;;;;AAyEA,MAAM,2BAAW,IAAI,QAAsD;AAE3E,MAAMA,UAAQ,QAAmB,MAAc,YAA2B;CACxE,MAAM,IAAI,MAAM,kCAAkC,OAAO,KAAK,GAAG,KAAK,IAAI,SAAS;AACrF;;AAGA,MAAM,SACJ,QACA,SACsC;CACtC,MAAM,QAAQ,aAAa,MAAM;CACjC,OAAO,OAAO,YAAY,CACxB,GAAG,OAAO,QAAQ,OAAO,MAA4C,CAAC,CAAC,KACpE,CAAC,KAAK,WAAW,CAAC,KAAK,KAAK,KAAK,CAAC,CACrC,GACA,GAAG,MAAM,KAAI,SAAQ,CAAC,KAAK,KAAK,KAAK,IAAI,CAAC,CAAU,CACtD,CAAC;AACH;AAEA,MAAM,oBACJ,QACA,MACA,SACA,QACc;CACd,MAAM,SAAS,OAAO;CACtB,MAAM,SAAS,OAAO,QAAQ,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;EAC3D,IAAI,CAAC,OAAO,OAAO,QAAQ,GAAG,GAAG,OAAOA,OAAK,QAAQ,MAAM,IAAI,IAAI,iBAAiB;EACpF,MAAM,SAAS,OAAO,UAAU,OAAO,IAAI,CAAE,MAA+B;EAC5E,I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WACrB,SAAS,IAAI,OAAO,QAAQ,KAAK,CAAC;;;;;;AAOpC,MAAa,WACX,YACA,KACA,YAA6B,oBACF,OAAO,YAAY,KAAK,SAAS;;AAG9D,MAAa,WACX,QACA,YACA,SACkB;CAClB,IAAI,WAAW,OAAO,aAAa,OAAO,UACxC,MAAM,IAAI,MACR,6BAA6B,OAAO,KAAK,QAAQ,KAAK,MAAM,WAAW,OAAO,KAAK,GAAG,WAAW,KAAK,4BACxG;CACF,IAAI,CAAC,WAAW,OACd,MAAM,IAAI,MACR,6BAA6B,OAAO,KAAK,QAAQ,KAAK,GAAG,OAAO,KAAK,GAAG,WAAW,KAAK,kEAC1F;CACF,OAAO;AACT"}
|
package/dist/rows.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { AnyEntity } from "./index.js";
|
|
3
|
+
import { Row } from "./evaluate.js";
|
|
4
|
+
import { OneRelation } from "./expr.js";
|
|
5
|
+
/** An Entity's `one` relations: a row holds each as the id it points at; a `many` holds no one value. */
|
|
6
|
+
export declare const oneRelations: (entity: AnyEntity) => ReadonlyArray<OneRelation>;
|
|
7
|
+
/** A row as stored, held: a key it lacks is unheld, so a rule reading it is undecided. */
|
|
8
|
+
export declare const heldRow: (entity: AnyEntity, row: Row) => import("./held.js").HeldRow;
|
|
9
|
+
/**
|
|
10
|
+
* The id a `one` relation's value names, as a row holds it: a ref's or a
|
|
11
|
+
* nested Selection value's `id`, an id as it is, and null for one pointing
|
|
12
|
+
* nowhere. None for a value naming no id (a nested Selection that left `id` out).
|
|
13
|
+
*/
|
|
14
|
+
export declare const linkId: (held: unknown) => Option.Option<string | number | null>;
|
|
15
|
+
//# sourceMappingURL=rows.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.d.ts","names":[],"sources":["../src/rows.ts"],"mappings":";;;;;qBAQa,eAAY,QAAY,cAAY,cAAc;;qBAWlD,UAAO,QAAY,WAAS,KAAO,4BAAG;;;;;;qBAYtC,SAAM,kBAAoB,OAAO"}
|
package/dist/rows.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Match, Option } from "effect";
|
|
2
|
+
import { Held } from "./held.js";
|
|
3
|
+
// src/rows.ts
|
|
4
|
+
/** What invariants, refinements and the enumerator share in reading an Entity's rows. */
|
|
5
|
+
/** An Entity's `one` relations: a row holds each as the id it points at; a `many` holds no one value. */
|
|
6
|
+
const oneRelations = (entity) => Object.values(entity.relations).flatMap((member) => Match.value(member).pipe(Match.tag("Relation", (relation) => relation.cardinality === "one" ? [relation] : []), Match.orElse(() => [])));
|
|
7
|
+
/** A row as stored, held: a key it lacks is unheld, so a rule reading it is undecided. */
|
|
8
|
+
const heldRow = (entity, row) => Held.rows([{
|
|
9
|
+
entity,
|
|
10
|
+
rows: [row],
|
|
11
|
+
complete: false
|
|
12
|
+
}]).rows(entity)[0];
|
|
13
|
+
/** An id as an Entity's id Schema may type it. */
|
|
14
|
+
const isId = (value) => typeof value === "string" || typeof value === "number";
|
|
15
|
+
/**
|
|
16
|
+
* The id a `one` relation's value names, as a row holds it: a ref's or a
|
|
17
|
+
* nested Selection value's `id`, an id as it is, and null for one pointing
|
|
18
|
+
* nowhere. None for a value naming no id (a nested Selection that left `id` out).
|
|
19
|
+
*/
|
|
20
|
+
const linkId = (held) => held === null ? Option.some(null) : isId(held) ? Option.some(held) : typeof held === "object" && "id" in held && isId(held.id) ? Option.some(held.id) : Option.none();
|
|
21
|
+
export { heldRow, linkId, oneRelations };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=rows.js.map
|
package/dist/rows.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.js","names":[],"sources":["../src/rows.ts"],"sourcesContent":["/** What invariants, refinements and the enumerator share in reading an Entity's rows. */\nimport { Match, Option } from 'effect'\nimport type { Row } from './evaluate.js'\nimport { Held } from './held.js'\nimport type { OneRelation } from './expr.js'\nimport type { AnyEntity, EntityMember } from './index.js'\n\n/** An Entity's `one` relations: a row holds each as the id it points at; a `many` holds no one value. */\nexport const oneRelations = (entity: AnyEntity): ReadonlyArray<OneRelation> =>\n Object.values(entity.relations as Readonly<Record<string, EntityMember>>).flatMap(member =>\n Match.value(member).pipe(\n Match.tag('Relation', relation =>\n relation.cardinality === 'one' ? [relation as OneRelation] : [],\n ),\n Match.orElse(() => []),\n ),\n )\n\n/** A row as stored, held: a key it lacks is unheld, so a rule reading it is undecided. */\nexport const heldRow = (entity: AnyEntity, row: Row) =>\n Held.rows([{ entity, rows: [row], complete: false }]).rows(entity)[0]!\n\n/** An id as an Entity's id Schema may type it. */\nconst isId = (value: unknown): value is string | number =>\n typeof value === 'string' || typeof value === 'number'\n\n/**\n * The id a `one` relation's value names, as a row holds it: a ref's or a\n * nested Selection value's `id`, an id as it is, and null for one pointing\n * nowhere. None for a value naming no id (a nested Selection that left `id` out).\n */\nexport const linkId = (held: unknown): Option.Option<string | number | null> =>\n held === null\n ? Option.some(null)\n : isId(held)\n ? Option.some(held)\n : typeof held === 'object' && 'id' in held && isId(held.id)\n ? Option.some(held.id)\n : Option.none()\n"],"mappings":";;;;;AAQA,MAAa,gBAAgB,WAC3B,OAAO,OAAO,OAAO,SAAmD,CAAC,CAAC,SAAQ,WAChF,MAAM,MAAM,MAAM,CAAC,CAAC,KAClB,MAAM,IAAI,aAAY,aACpB,SAAS,gBAAgB,QAAQ,CAAC,QAAuB,IAAI,CAAC,CAChE,GACA,MAAM,aAAa,CAAC,CAAC,CACvB,CACF;;AAGF,MAAa,WAAW,QAAmB,QACzC,KAAK,KAAK,CAAC;CAAE;CAAQ,MAAM,CAAC,GAAG;CAAG,UAAU;AAAM,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC;;AAGrE,MAAM,QAAQ,UACZ,OAAO,UAAU,YAAY,OAAO,UAAU;;;;;;AAOhD,MAAa,UAAU,SACrB,SAAS,OACL,OAAO,KAAK,IAAI,IAChB,KAAK,IAAI,IACP,OAAO,KAAK,IAAI,IAChB,OAAO,SAAS,YAAY,QAAQ,QAAQ,KAAK,KAAK,EAAE,IACtD,OAAO,KAAK,KAAK,EAAE,IACnB,OAAO,KAAK"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { OrderTerm } from "./expr.js";
|
|
3
|
+
/**
|
|
4
|
+
* Why an order term cannot be answered the same by every reader for want of a
|
|
5
|
+
* collation: a field of text that declares none. SQLite compares bytes,
|
|
6
|
+
* Postgres follows its database's locale and JavaScript compares UTF-16 units,
|
|
7
|
+
* so `Query.placement` and `evaluate` both refuse it, for this reason.
|
|
8
|
+
*/
|
|
9
|
+
export declare const uncollatedText: (orderTerm: OrderTerm) => Option.Option<string>;
|
|
10
|
+
//# sourceMappingURL=uncollated.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uncollated.d.ts","names":[],"sources":["../src/uncollated.ts"],"mappings":";;;;;;;;qBAqBa,iBAAc,WAAe,cAAY,OAAO"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Match, Option, Schema, SchemaAST } from "effect";
|
|
2
|
+
// src/uncollated.ts
|
|
3
|
+
/**
|
|
4
|
+
* Whether values encoded by `ast` order the same everywhere: numbers and
|
|
5
|
+
* booleans, never text. A null among them does too: where it goes is said on
|
|
6
|
+
* every order term.
|
|
7
|
+
*/
|
|
8
|
+
const ordersIntrinsically = (ast) => SchemaAST.isNumber(ast) || SchemaAST.isBoolean(ast) || SchemaAST.isLiteral(ast) && typeof ast.literal !== "string" || SchemaAST.isUnion(ast) && ast.types.every((member) => SchemaAST.isNull(member) || ordersIntrinsically(member));
|
|
9
|
+
/**
|
|
10
|
+
* Why an order term cannot be answered the same by every reader for want of a
|
|
11
|
+
* collation: a field of text that declares none. SQLite compares bytes,
|
|
12
|
+
* Postgres follows its database's locale and JavaScript compares UTF-16 units,
|
|
13
|
+
* so `Query.placement` and `evaluate` both refuse it, for this reason.
|
|
14
|
+
*/
|
|
15
|
+
const uncollatedText = (orderTerm) => Match.value(orderTerm.expr).pipe(Match.tag("Field", (field) => Option.isNone(orderTerm.collation) && !ordersIntrinsically(Schema.toEncoded(field.schema).ast) ? Option.some(`it orders by ${field.owner.name}.${field.key}, text with no declared collation`) : Option.none()), Match.orElse(() => Option.none()));
|
|
16
|
+
export { uncollatedText };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=uncollated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uncollated.js","names":[],"sources":["../src/uncollated.ts"],"sourcesContent":["import { Match, Option, Schema, SchemaAST } from 'effect'\nimport type { OrderTerm } from './expr.js'\n\n/**\n * Whether values encoded by `ast` order the same everywhere: numbers and\n * booleans, never text. A null among them does too: where it goes is said on\n * every order term.\n */\nconst ordersIntrinsically = (ast: SchemaAST.AST): boolean =>\n SchemaAST.isNumber(ast) ||\n SchemaAST.isBoolean(ast) ||\n (SchemaAST.isLiteral(ast) && typeof ast.literal !== 'string') ||\n (SchemaAST.isUnion(ast) &&\n ast.types.every(member => SchemaAST.isNull(member) || ordersIntrinsically(member)))\n\n/**\n * Why an order term cannot be answered the same by every reader for want of a\n * collation: a field of text that declares none. SQLite compares bytes,\n * Postgres follows its database's locale and JavaScript compares UTF-16 units,\n * so `Query.placement` and `evaluate` both refuse it, for this reason.\n */\nexport const uncollatedText = (orderTerm: OrderTerm): Option.Option<string> =>\n Match.value(orderTerm.expr).pipe(\n Match.tag('Field', field =>\n Option.isNone(orderTerm.collation) && !ordersIntrinsically(Schema.toEncoded(field.schema).ast)\n ? Option.some(\n `it orders by ${field.owner.name}.${field.key}, text with no declared collation`,\n )\n : Option.none<string>(),\n ),\n Match.orElse(() => Option.none<string>()),\n )\n"],"mappings":";;;;;;;AAQA,MAAM,uBAAuB,QAC3B,UAAU,SAAS,GAAG,KACtB,UAAU,UAAU,GAAG,KACtB,UAAU,UAAU,GAAG,KAAK,OAAO,IAAI,YAAY,YACnD,UAAU,QAAQ,GAAG,KACpB,IAAI,MAAM,OAAM,WAAU,UAAU,OAAO,MAAM,KAAK,oBAAoB,MAAM,CAAC;;;;;;;AAQrF,MAAa,kBAAkB,cAC7B,MAAM,MAAM,UAAU,IAAI,CAAC,CAAC,KAC1B,MAAM,IAAI,UAAS,UACjB,OAAO,OAAO,UAAU,SAAS,KAAK,CAAC,oBAAoB,OAAO,UAAU,MAAM,MAAM,CAAC,CAAC,GAAG,IACzF,OAAO,KACL,gBAAgB,MAAM,MAAM,KAAK,GAAG,MAAM,IAAI,kCAChD,IACA,OAAO,KAAa,CAC1B,GACA,MAAM,aAAa,OAAO,KAAa,CAAC,CAC1C"}
|
package/dist/write.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Data, Effect, Option, Schema } from "effect";
|
|
2
2
|
import { AnyEntity, EntityField, EntityInput, EntityRef, EntityRelation } from "./index.js";
|
|
3
3
|
import { ContextValue } from "./sets.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { AnyRefinement, RefinementOf } from "./refine.js";
|
|
5
|
+
import { BeforeRow, Invariant } from "./invariant.js";
|
|
6
6
|
import { AnyExpr, AnyQuery as AnyQuery$1, InputExpr, Predicate, Reads } from "./expr.js";
|
|
7
|
+
import { Collation as Collation$1 } from "./collation.js";
|
|
7
8
|
import { Held } from "./held.js";
|
|
8
9
|
import { ExprContext as ExprContext$1 } from "./context.js";
|
|
9
10
|
import { MemoryOperators as MemoryOperators$1 } from "./operators.js";
|
|
11
|
+
import { Footprint } from "./footprint.js";
|
|
10
12
|
type AnyField = EntityField<string, string, Schema.Constraint>;
|
|
11
13
|
type OneRelation$1 = EntityRelation<string, string, AnyEntity, 'one', boolean>;
|
|
12
14
|
/** One input key the write sets, and the field it sets. */
|
|
@@ -106,6 +108,24 @@ export interface Write<E extends AnyEntity, Fields extends Schema.Struct.Fields,
|
|
|
106
108
|
* a row it holds of, and refuses the rest as unmet.
|
|
107
109
|
*/
|
|
108
110
|
readonly when: Option.Option<AnyQuery$1>;
|
|
111
|
+
/**
|
|
112
|
+
* The state the row must be in for the write to land (`from: Draft`): its
|
|
113
|
+
* predicate is in `when` too, so a row in another state is refused as unmet.
|
|
114
|
+
*/
|
|
115
|
+
readonly from: Option.Option<AnyRefinement>;
|
|
116
|
+
/**
|
|
117
|
+
* The state every row the write can start from is in once it lands
|
|
118
|
+
* (`becomes: Published`): proved where the write is declared, by
|
|
119
|
+
* enumerating them, or, where enumeration cannot decide, kept by `ensures`.
|
|
120
|
+
*/
|
|
121
|
+
readonly becomes: Option.Option<AnyRefinement>;
|
|
122
|
+
/**
|
|
123
|
+
* The postcondition a `becomes` enumeration could not decide is held to
|
|
124
|
+
* where the write commits: an invariant of this write alone, with the id
|
|
125
|
+
* `becomes:<State>`, judged by each writer beside the Entity's invariants
|
|
126
|
+
* and refused as `Invalid`. None for a write with no such claim.
|
|
127
|
+
*/
|
|
128
|
+
readonly ensures: Option.Option<Invariant>;
|
|
109
129
|
/** The keys it sets, in the input's order. */
|
|
110
130
|
readonly sets: ReadonlyArray<WriteSet>;
|
|
111
131
|
/**
|
|
@@ -149,23 +169,36 @@ export type WriteComputations<E extends AnyEntity, Fields extends Schema.Struct.
|
|
|
149
169
|
type UpdateOptions<E extends AnyEntity, Fields extends Schema.Struct.Fields> = RowOptions<Fields> & {
|
|
150
170
|
readonly increment?: WriteIncrements<E, Fields>;
|
|
151
171
|
readonly compute?: WriteComputations<E, Fields>;
|
|
172
|
+
readonly becomes?: RefinementOf<E> | undefined;
|
|
152
173
|
};
|
|
153
|
-
/** `update`: conditioned when given `when`, which the overloads tell apart. */
|
|
174
|
+
/** `update`: conditioned when given `when` or `from`, which the overloads tell apart. */
|
|
154
175
|
interface UpdateWrite {
|
|
155
176
|
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
156
177
|
readonly when: WriteCondition<E, Fields>;
|
|
178
|
+
readonly from?: RefinementOf<E> | undefined;
|
|
157
179
|
}): ConditionedWrite<E, Fields, 'Update'>;
|
|
158
180
|
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
159
181
|
readonly when?: undefined;
|
|
182
|
+
readonly from: RefinementOf<E>;
|
|
183
|
+
}): ConditionedWrite<E, Fields, 'Update'>;
|
|
184
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
185
|
+
readonly when?: undefined;
|
|
186
|
+
readonly from?: undefined;
|
|
160
187
|
}): Write<E, Fields, 'Update'>;
|
|
161
188
|
}
|
|
162
|
-
/** `delete`: conditioned when given `when`, as an update is. */
|
|
189
|
+
/** `delete`: conditioned when given `when` or `from`, as an update is. */
|
|
163
190
|
interface DeleteWrite {
|
|
164
191
|
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
165
192
|
readonly when: WriteCondition<E, Fields>;
|
|
193
|
+
readonly from?: RefinementOf<E> | undefined;
|
|
166
194
|
}): ConditionedWrite<E, Fields, 'Delete'>;
|
|
167
195
|
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
168
196
|
readonly when?: undefined;
|
|
197
|
+
readonly from: RefinementOf<E>;
|
|
198
|
+
}): ConditionedWrite<E, Fields, 'Delete'>;
|
|
199
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
200
|
+
readonly when?: undefined;
|
|
201
|
+
readonly from?: undefined;
|
|
169
202
|
}): Write<E, Fields, 'Delete'>;
|
|
170
203
|
}
|
|
171
204
|
/**
|
|
@@ -204,6 +237,8 @@ export interface ContextValues {
|
|
|
204
237
|
* server reads every row its asker may see, and reads a hidden one as null.
|
|
205
238
|
*/
|
|
206
239
|
export type ContextRead<E, R> = (row: EntityRef, key: string) => Effect.Effect<Held<unknown>, E, R>;
|
|
240
|
+
/** Every member a write may change, by key: `Write.writes`, and the revision it expects, which it moves on. */
|
|
241
|
+
export declare const writtenKeys: (write: AnyWrite) => ReadonlyArray<string>;
|
|
207
242
|
export declare const Write: {
|
|
208
243
|
/**
|
|
209
244
|
* Sets, on the row whose id is the input's `id` key, every field the input
|
|
@@ -221,7 +256,15 @@ export declare const Write: {
|
|
|
221
256
|
*
|
|
222
257
|
* `when` is what the row must hold for the update to land, checked where it
|
|
223
258
|
* commits (`({ input }) => Expr.eq(Post.fields.status, 'draft')`); a row it
|
|
224
|
-
* does not hold of is refused as unmet.
|
|
259
|
+
* does not hold of is refused as unmet. `from: Draft` (an `Entity.refine`
|
|
260
|
+
* state) joins that state's predicate to it.
|
|
261
|
+
*
|
|
262
|
+
* `becomes: Published` says every row it can start from is in that state
|
|
263
|
+
* once it lands. It is proved where the write is declared, by enumerating
|
|
264
|
+
* those rows over finite fields: one it leaves outside throws, naming it.
|
|
265
|
+
* One enumeration cannot decide (`becomes: InStock` over an increment) is
|
|
266
|
+
* the write's postcondition, `ensures`: each writer judges it where the
|
|
267
|
+
* write commits and refuses a row it leaves outside as `Invalid`.
|
|
225
268
|
*
|
|
226
269
|
* `compute` sets a field to what an expression comes to over the row before
|
|
227
270
|
* the write and the input, run where it commits
|
|
@@ -238,10 +281,12 @@ export declare const Write: {
|
|
|
238
281
|
*
|
|
239
282
|
* What its Entity sets from the write's context (`Entity.sets`: who asks,
|
|
240
283
|
* when) it writes too, never from the input: a server fills them, a client
|
|
241
|
-
* predicts them. An update writes those set `on: 'write'`.
|
|
284
|
+
* predicts them. An update writes those set `on: 'write'`. `becomes`, as an
|
|
285
|
+
* update's, is proved over what the insert sets, or else ensured.
|
|
242
286
|
*/
|
|
243
287
|
insert: <E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: {
|
|
244
288
|
readonly id: keyof Fields & string;
|
|
289
|
+
readonly becomes?: RefinementOf<E> | undefined;
|
|
245
290
|
}) => Write<E, Fields, 'Insert'>;
|
|
246
291
|
/**
|
|
247
292
|
* Deletes the row whose id is the input's `id` key, only while it is still at
|
|
@@ -259,6 +304,13 @@ export declare const Write: {
|
|
|
259
304
|
readonly field: string;
|
|
260
305
|
readonly revision: unknown;
|
|
261
306
|
}>;
|
|
307
|
+
/**
|
|
308
|
+
* The postcondition a write's `becomes` holds it to where it commits, as
|
|
309
|
+
* the invariant a refusal names (`Refusal.isInvalidBy(refusal,
|
|
310
|
+
* Write.ensured(Sell.write))`). Throws for a write with none: one with no
|
|
311
|
+
* `becomes`, or one whose `becomes` was proved where it was declared.
|
|
312
|
+
*/
|
|
313
|
+
ensured: (write: AnyWrite) => Invariant;
|
|
262
314
|
/** The members any invocation may write: what a query reading none of them cannot be changed by. */
|
|
263
315
|
writes: (write: AnyWrite) => ReadonlyArray<AnyField | OneRelation$1>;
|
|
264
316
|
/**
|
|
@@ -273,6 +325,12 @@ export declare const Write: {
|
|
|
273
325
|
contextValues: <E, R>(write: AnyWrite, by: ExprContext$1, read: ContextRead<E, R>) => Effect.Effect<ContextValues, E, R>;
|
|
274
326
|
/** The Entities a write's paths from the viewer read, by name: what a server must have a source of. */
|
|
275
327
|
contextReads: (write: AnyWrite) => ReadonlyArray<string>;
|
|
328
|
+
/**
|
|
329
|
+
* What a write reads and writes of its row, by field key (`Write.reads`,
|
|
330
|
+
* and `Write.writes` with the revision it expects, which it moves on): what
|
|
331
|
+
* `Laws.check` judges it by.
|
|
332
|
+
*/
|
|
333
|
+
footprint: (write: AnyWrite) => Footprint;
|
|
276
334
|
/**
|
|
277
335
|
* The fields a write reads from the row it writes: those its `when` reads,
|
|
278
336
|
* each it increments, each its assignments read, and the revision it
|
package/dist/write.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"write.d.ts","names":["OneRelation","Collation","AnyQuery","ExprContext","MemoryOperators"],"sources":["../src/write.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"write.d.ts","names":["OneRelation","Collation","AnyQuery","ExprContext","MemoryOperators"],"sources":["../src/write.ts"],"mappings":";;;;;;;;;;;KAmDK,WAAW,4BAA4B,OAAO;KAC9CA,gBAAc,+BAA+B;;iBAGjC;WACN;WACA,OAAO;;;;;;;iBAQD,uBAAuB;WAC7B;;;;;;;iBAQM;WACN,OAAO;WACP,MAAM;;;;;;WAMN;;;;;;;;;YAUC,cAAc,KAAK;EAC7B;aAAkB;;EAClB;aAAkB;;EAClB;aACW;aACA,MAAM,cAAc;aACpB,WAAWC;;;qBAGX,aAAW,KAAA,WAAA;;WALD;WACJ,MAAA,cAAc;WACT,WAAAA;;;WALC;;;WACE;;;iBAUV;WACN;WACA,UAAUD;;;iBAIJ;WACN,QAAQ,WAAWA;WACnB,OAAO;;;YAIN;;;;;;iBAOK,MACf,UAAU,WACV,eAAe,OAAO,OAAO,QAC7B,aAAa,YAAY;WAEhB,MAAM;WACN,OAAO,YAAY,GAAG;;WAEtB,IAAI;aAAsB,WAAW;;;;;;WAKrC,SAAS;aAAsB,WAAW;;;;;;;WAM1C,MAAM,OAAO,OAAOE;;;;;WAKpB,MAAM,OAAO,OAAO;;;;;;WAMpB,SAAS,OAAO,OAAO;;;;;;;WAOvB,SAAS,OAAO,OAAO;;WAEvB,MAAM,cAAc;;;;;WAKpB,YAAY,cAAc;;WAE1B,aAAa,cAAc;;WAE3B,OAAO,cAAc;;;;;WAKrB,SAAS,cAAc;;YAGtB,WAAW,MAAM;;YAGjB,iBACV,UAAU,WACV,eAAe,OAAO,OAAO,QAC7B,aAAa,YAAY,aACvB,MAAM,GAAG,QAAQ;WAAmB,MAAM,OAAO,KAAKA;;UAEhD,WAAW,eAAe,OAAO,OAAO;WACvC,UAAU;WACV,eAAe;;;KAUrB,WAAW,aACb,WAAW,kBAAkB,OAAO,WAAW,O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