foldkit-entity 0.15.0 → 0.17.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 +128 -17
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +20 -0
- package/dist/conformance/index.js.map +1 -1
- package/dist/conformance/operator.d.ts.map +1 -1
- package/dist/conformance/operator.js +13 -4
- package/dist/conformance/operator.js.map +1 -1
- package/dist/entity.d.ts +14 -2
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +41 -12
- package/dist/entity.js.map +1 -1
- package/dist/evaluate.d.ts +1 -1
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +22 -18
- package/dist/evaluate.js.map +1 -1
- package/dist/expr.d.ts +70 -20
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +67 -47
- package/dist/expr.js.map +1 -1
- package/dist/held.js +11 -3
- package/dist/held.js.map +1 -1
- package/dist/index.d.ts +38 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/dist/invariant.d.ts.map +1 -1
- package/dist/invariant.js +26 -20
- package/dist/invariant.js.map +1 -1
- package/dist/law.d.ts.map +1 -1
- package/dist/law.js +3 -9
- package/dist/law.js.map +1 -1
- package/dist/lawCore.d.ts +42 -0
- package/dist/lawCore.d.ts.map +1 -0
- package/dist/lawCore.js +12 -0
- package/dist/lawCore.js.map +1 -0
- package/dist/memoryTable.d.ts +11 -0
- package/dist/memoryTable.d.ts.map +1 -0
- package/dist/memoryTable.js +69 -0
- package/dist/memoryTable.js.map +1 -0
- package/dist/operators.d.ts.map +1 -1
- package/dist/operators.js +2 -66
- package/dist/operators.js.map +1 -1
- package/dist/sets.js.map +1 -1
- package/dist/write.d.ts +1 -1
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +40 -18
- package/dist/write.js.map +1 -1
- package/package.json +1 -1
package/dist/write.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { BeforeRow } from "./invariant.js";
|
|
|
5
5
|
import { Collation as Collation$1 } from "./collation.js";
|
|
6
6
|
import { AnyExpr, AnyQuery as AnyQuery$1, InputExpr, Predicate, Reads } from "./expr.js";
|
|
7
7
|
import { Held } from "./held.js";
|
|
8
|
-
import { MemoryOperators as MemoryOperators$1 } from "./operators.js";
|
|
9
8
|
import { ExprContext as ExprContext$1 } from "./context.js";
|
|
9
|
+
import { MemoryOperators as MemoryOperators$1 } from "./operators.js";
|
|
10
10
|
type AnyField = EntityField<string, string, Schema.Constraint>;
|
|
11
11
|
type OneRelation$1 = EntityRelation<string, string, AnyEntity, 'one', boolean>;
|
|
12
12
|
/** One input key the write sets, and the field it sets. */
|
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":";;;;;;;;;KA+CK,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;;WAEpB,MAAM,cAAc;;;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,eAAe,UAAU,WAAW,eAAe,OAAO,OAAO,WAAW;WAC7E,mBACG,WAAW,kBAAkB,UAAU,OAAO,OAAO,KAAK,OAAO;MAEzE,YAAY,MAAM;;iBAGP;WACN;WACA;WACA,QAAQ,SAAS;;;;;;WAMjB,OAAO,SAAS,eAAe,OAAO,OAAO;;WAE7C,YAAY,SAAS;;WAErB,aAAa,SAAS,eAAe;;;
|
|
1
|
+
{"version":3,"file":"write.d.ts","names":["OneRelation","Collation","AnyQuery","ExprContext","MemoryOperators"],"sources":["../src/write.ts"],"mappings":";;;;;;;;;KA+CK,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;;WAEpB,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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;iBA0K/B;WACN,QAAQ,SAAS;WACjB,OAAO,SAAS,eAAe,OAAO,OAAO;WAC7C,UAAU;;;;;;;YAQT,YAAY,GAAG,MAAM,KAAK,WAAW,gBAAgB,OAAO,OAAO,eAAe,GAAG;qBAsUpF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqCF,SAAA,UAAU,WAAW,eAAe,OAAO,OAAO,QAAM,OACxD,YAAY,GAAG,cAAY;aACd,UAAU;QAC7B,MAAM,GAAG;;;;;;;;;;;;;EAqBD,WAAA,eAAe,OAAO,OAAO,QAAM,OACrC,MAAM,WAAW,SAAO,OACxB,OAAO,OAAO,KAAK,YACzB,OAAO;aAAkB;aAAwB;;;EAOpC,SAAA,OAAA,aAAW,cAAc,WAAWF;;;;;;;;;;EAiBpC,gBAAA,GAAG,GAAC,OACX,UAAQ,IACXG,eAAW,MACT,YAAY,GAAG,OACpB,OAAO,OAAO,eAAe,GAAG;;EA4Cb,eAAA,OAAA,aAAW;;;;;;;EAgBlB,QAAA,OAAA,aAAW,cAAc;;;;;;;EAoBjC,OAAA,eAAe,OAAO,OAAO,QAAM,OACjC,MAAM,WAAW,SAAO,OACxB,OAAO,OAAO,KAAK,SAAO,OAC1B,0BACN;;;;;EAmDiB,aAAA,OAAA,KAAK,8CAA4C;;;;;;;;;;EAsB9D,QAAA,KAAA,SAAS,0BAAwB,OAC/B,KAAK,sDAAoD;aAC5C,WAAWC;;aAEtB,KAAK,SAAS;aACd,SAAS"}
|
package/dist/write.js
CHANGED
|
@@ -50,8 +50,13 @@ const given = (value) => {
|
|
|
50
50
|
const fieldAt = (input, key, role) => {
|
|
51
51
|
const member = own(membersOf(input), key);
|
|
52
52
|
if (member === void 0) return fail(input, `${role} "${key}" is not a key of the input`);
|
|
53
|
-
|
|
54
|
-
return member
|
|
53
|
+
const unmapped = () => fail(input, `${role} "${key}" is not mapped to a field`);
|
|
54
|
+
return Match.valueTags(member, {
|
|
55
|
+
Field: (field) => field,
|
|
56
|
+
Unmapped: unmapped,
|
|
57
|
+
RelationInput: unmapped,
|
|
58
|
+
NestedInput: unmapped
|
|
59
|
+
});
|
|
55
60
|
};
|
|
56
61
|
/**
|
|
57
62
|
* What every kind shares: the key naming the row, the expected revision, and
|
|
@@ -174,7 +179,10 @@ const walked = (path, by, read) => Option.match(viewerIdOf(path.viewer.entity, b
|
|
|
174
179
|
};
|
|
175
180
|
}
|
|
176
181
|
const end = yield* read(at, path.field.key);
|
|
177
|
-
return
|
|
182
|
+
return Held.match(end, {
|
|
183
|
+
onHeld: (value) => Held.of(value ?? null),
|
|
184
|
+
onUnheld: () => end
|
|
185
|
+
});
|
|
178
186
|
})
|
|
179
187
|
});
|
|
180
188
|
/** Whether a field stores a null. */
|
|
@@ -254,7 +262,10 @@ const assignmentOf = (input, fieldKey, compute, declared) => {
|
|
|
254
262
|
const computationOf = (write, expr, input) => {
|
|
255
263
|
switch (expr._tag) {
|
|
256
264
|
case "Field":
|
|
257
|
-
case "Next": return Computation.Field({ key:
|
|
265
|
+
case "Next": return Computation.Field({ key: Match.valueTags(expr, {
|
|
266
|
+
Field: ({ key }) => key,
|
|
267
|
+
Next: ({ field }) => field.key
|
|
268
|
+
}) });
|
|
258
269
|
case "Literal": return Computation.Value({ value: expr.value });
|
|
259
270
|
case "Input": return Computation.Value({ value: Schema.encodeUnknownSync(write.input.schema.fields[expr.key])(input[expr.key]) });
|
|
260
271
|
case "Call": return Computation.Call({
|
|
@@ -390,21 +401,28 @@ const Write = {
|
|
|
390
401
|
const links = {};
|
|
391
402
|
const unfilled = [];
|
|
392
403
|
for (const { member, value } of write.context) {
|
|
393
|
-
const comes = value.
|
|
394
|
-
onHeld: (
|
|
404
|
+
const comes = yield* Match.value(value).pipe(Match.tag("ViewerPath", (path) => Effect.map(walked(path, by, read), (held) => Held.match(held, {
|
|
405
|
+
onHeld: (some) => Option.some(some),
|
|
395
406
|
onUnheld: () => Option.none()
|
|
396
|
-
})
|
|
407
|
+
}))), Match.orElse((other) => Effect.succeed(contextValue(other, by))));
|
|
397
408
|
if (Option.isNone(comes)) unfilled.push(member.key);
|
|
398
|
-
else
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
409
|
+
else {
|
|
410
|
+
const given = comes.value;
|
|
411
|
+
Match.valueTags(member, {
|
|
412
|
+
Relation: (relation) => {
|
|
413
|
+
if (given !== null) put(links, relation.key, Option.some({
|
|
414
|
+
entity: relation.target().name,
|
|
415
|
+
id: String(given)
|
|
416
|
+
}));
|
|
417
|
+
else if (relation.optional) put(links, relation.key, Option.none());
|
|
418
|
+
else unfilled.push(relation.key);
|
|
419
|
+
},
|
|
420
|
+
Field: (field) => {
|
|
421
|
+
if (given !== null || holdsNull(field)) put(values, field.key, given);
|
|
422
|
+
else unfilled.push(field.key);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
408
426
|
}
|
|
409
427
|
return {
|
|
410
428
|
values,
|
|
@@ -413,7 +431,11 @@ const Write = {
|
|
|
413
431
|
};
|
|
414
432
|
}),
|
|
415
433
|
/** The Entities a write's paths from the viewer read, by name: what a server must have a source of. */
|
|
416
|
-
contextReads: (write) => [...new Set(write.context.flatMap(({ value }) =>
|
|
434
|
+
contextReads: (write) => [...new Set(write.context.flatMap(({ value }) => {
|
|
435
|
+
if (value._tag !== "ViewerPath") return [];
|
|
436
|
+
const { viewer, via } = value;
|
|
437
|
+
return [viewer.entity.name, ...via.map((relation) => relation.target().name)];
|
|
438
|
+
}))],
|
|
417
439
|
/**
|
|
418
440
|
* The fields a write reads from the row it writes: those its `when` reads,
|
|
419
441
|
* each it increments, each its assignments read, and the revision it
|
package/dist/write.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"write.js","names":[],"sources":["../src/write.ts"],"sourcesContent":["/**\n * A write as a value: what an operation changes, declared over the input it\n * takes, so the changes can be known before it runs and its interpreters can\n * agree on what it means.\n *\n * A `Write` is built on `Entity.input`, which already says which input key is\n * which member of the Entity. `Write.update`, `Write.insert` and `Write.delete`\n * add what to do with them: the key that names the row, and optionally the key\n * holding the revision it was read at. Every other key mapped to a field is\n * set; a key mapped to `Entity.unmapped` is about the operation and is not\n * written.\n *\n * It is authority someone declared, never something derived from an Entity: an\n * Entity having a field does not mean anyone may set it.\n */\nimport { Data, Effect, Match, Option, Schema, SchemaAST } from 'effect'\nimport type {\n AnyEntity,\n EntityMember,\n EntityField,\n EntityInput,\n EntityRef,\n EntityRelation,\n InputMember,\n} from './index.js'\nimport type { Collation } from './collation.js'\nimport { applyOperator } from './evaluate.js'\nimport {\n Expr,\n Query,\n dependenciesOf,\n isPredicate,\n type AnyExpr,\n type AnyQuery,\n type InputExpr,\n type Operandish,\n type Predicate,\n type Reads,\n} from './expr.js'\nimport type { BeforeRow } from './invariant.js'\nimport { contextValue, viewerIdOf, type ExprContext, type ViewerPathExpr } from './context.js'\nimport { Held, idOfRef } from './held.js'\nimport type { MemoryOperators } from './operators.js'\nimport { own, put } from './record.js'\nimport { SchemaShape } from './shape.js'\nimport type { ContextValue } from './sets.js'\n\ntype AnyField = EntityField<string, string, Schema.Constraint>\ntype OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>\n\n/** One input key the write sets, and the field it sets. */\nexport interface WriteSet {\n readonly key: string\n readonly field: AnyField\n}\n\n/**\n * One input key holding an amount, and the field it moves. `integer` is whether\n * the amount's schema holds it to an integer (`Schema.isInt`): a writer over an\n * integer column refuses an increment without it, which the Entity cannot see.\n */\nexport interface WriteIncrement extends WriteSet {\n readonly integer: boolean\n}\n\n/**\n * One field the write computes (`label := concat(title, ' draft')`): an\n * expression over the row before the write and the input, every defined\n * operator in it expanded, so what is carried is primitive calls alone.\n */\nexport interface WriteAssignment {\n readonly field: AnyField\n readonly expr: AnyExpr\n /**\n * Whether `expr` comes to an integer, by the schemas of what it reads and\n * calls: a writer over an integer column refuses an assignment without it,\n * which the Entity cannot see.\n */\n readonly integer: boolean\n}\n\n/**\n * A computed assignment as data, bound to one input: what travels to a server\n * and sits in a client's pending patch, which holds no descriptor and no\n * function. A field of the row before the write, a stored value, or a call of\n * a primitive operator by name (`id`, or `id@version`), with the collation it\n * compares text under (`null` for none: this is JSON).\n */\nexport type Computation = Data.TaggedEnum<{\n Field: { readonly key: string }\n Value: { readonly value: unknown }\n Call: {\n readonly operator: string\n readonly args: ReadonlyArray<Computation>\n readonly collation: Collation | null\n }\n}>\nexport const Computation = Data.taggedEnum<Computation>()\n\n/** One input key the write points at another row with: a `one` relation, by the target's id. */\nexport interface WriteLink {\n readonly key: string\n readonly relation: OneRelation\n}\n\n/** One member the write's context sets (`Entity.sets`): a field or a `one` relation, and its value. */\nexport interface ContextSet {\n readonly member: AnyField | OneRelation\n readonly value: ContextValue\n}\n\n/** What a declared write does to its row. */\nexport type WriteKind = 'Update' | 'Insert' | 'Delete'\n\n/**\n * One row of an Entity, by id, from an operation's input: updated, inserted\n * with an id the client chose, or deleted. `Kind` is the one its constructor\n * made, so what it may be refused with can follow it.\n */\nexport interface Write<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind = WriteKind,\n> {\n readonly _tag: Kind\n readonly input: EntityInput<E, Fields, any>\n /** The input key holding the row's id, and the Entity's `id` field it is. */\n readonly id: WriteSet & { readonly key: keyof Fields & string }\n /**\n * The input key holding the revision the row was read at, and its field, when\n * the write is refused for a row that moved on since.\n */\n readonly expect: (WriteSet & { readonly key: keyof Fields & string }) | undefined\n /**\n * What the row must hold for the write to land, over the Entity, read\n * before the write and checked where it commits: an interpreter writes only\n * a row it holds of, and refuses the rest as unmet.\n */\n readonly when: Option.Option<AnyQuery>\n /** The keys it sets, in the input's order. */\n readonly sets: ReadonlyArray<WriteSet>\n /**\n * The fields it moves by an amount the input holds (`stock + quantity`),\n * computed where the write commits: the input key, and the field it moves.\n */\n readonly increments: ReadonlyArray<WriteIncrement>\n /** The fields it computes, each from the row before the write and the input. */\n readonly assignments: ReadonlyArray<WriteAssignment>\n /** The `one` relations it points elsewhere, in the input's order. */\n readonly links: ReadonlyArray<WriteLink>\n /**\n * What the write's context sets where it lands (`Entity.sets`): an insert\n * every such member, an update those set `on: 'write'`, a delete none.\n */\n readonly context: ReadonlyArray<ContextSet>\n}\n\nexport type AnyWrite = Write<AnyEntity, any>\n\n/** A write declared with a precondition, so it may be refused as unmet. */\nexport type ConditionedWrite<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind = WriteKind,\n> = Write<E, Fields, Kind> & { readonly when: Option.Some<AnyQuery> }\n\ninterface RowOptions<Fields extends Schema.Struct.Fields> {\n readonly id: keyof Fields & string\n readonly expect?: keyof Fields & string\n}\n\n/** The keys of `Fields` whose values are numbers. */\ntype NumberKeys<Fields> = {\n [K in keyof Fields & string]: Fields[K] extends Schema.Top\n ? Schema.Schema.Type<Fields[K]> extends number\n ? K\n : never\n : never\n}[keyof Fields & string]\n\n/** An update's increments: a number field of the Entity, by the input key holding the amount. */\nexport type WriteIncrements<E extends AnyEntity, Fields extends Schema.Struct.Fields> = {\n readonly [\n K in NumberKeys<{ [F in keyof E['fields']]: E['fields'][F]['schema'] }>\n ]?: NumberKeys<Fields>\n}\n\n/**\n * An update's computed fields: each a field of the Entity, by what it comes\n * to over the row before the write (`row`, each field as `Expr.field`) and\n * the input. Its kind is checked against the field's where it is declared.\n */\nexport type WriteComputations<E extends AnyEntity, Fields extends Schema.Struct.Fields> = {\n readonly [F in keyof E['fields'] & string]?: (context: {\n readonly row: BeforeRow<E>\n readonly input: {\n readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>\n }\n }) => AnyExpr\n}\n\n/** An update's options: a row's, what it moves by an amount, and what it computes. */\ntype UpdateOptions<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n> = RowOptions<Fields> & {\n readonly increment?: WriteIncrements<E, Fields>\n readonly compute?: WriteComputations<E, Fields>\n}\n\n/** `update`: conditioned when given `when`, which the overloads tell apart. */\ninterface UpdateWrite {\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: UpdateOptions<E, Fields> & { readonly when: WriteCondition<E, Fields> },\n ): ConditionedWrite<E, Fields, 'Update'>\n // `when?: undefined`, so an optional `when` in a variable cannot pick this one and lose `Unmet`.\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: UpdateOptions<E, Fields> & { readonly when?: undefined },\n ): Write<E, Fields, 'Update'>\n}\n\n/** `delete`: conditioned when given `when`, as an update is. */\ninterface DeleteWrite {\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: RowOptions<Fields> & { readonly when: WriteCondition<E, Fields> },\n ): ConditionedWrite<E, Fields, 'Delete'>\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: RowOptions<Fields> & { readonly when?: undefined },\n ): Write<E, Fields, 'Delete'>\n}\n\n// `declared` sets `when` exactly when it is given, which is what the overloads say.\nconst update: UpdateWrite = (\n input: EntityInput<AnyEntity, any, any>,\n options: UpdateOptions<AnyEntity, any>,\n) =>\n settingSomething(\n Object.freeze({\n ...declared('Update', input, options),\n context: contextOf(input.entity, 'write'),\n }),\n ) as never\n\nconst remove: DeleteWrite = (input: EntityInput<AnyEntity, any, any>, options: RowOptions<any>) => {\n const write = declared('Delete', input, options)\n const named = [...write.sets, ...write.links][0]\n if (named !== undefined) fail(input, `a delete sets nothing, but \"${named.key}\" is mapped`)\n return write as never\n}\n\n/**\n * A write's precondition: a predicate over its Entity's row, given one\n * placeholder per input key, as a `Query.define` body is. It is built once,\n * so `input` stands for what the write will be given.\n */\nexport type WriteCondition<E extends AnyEntity, Fields extends Schema.Struct.Fields> = (context: {\n readonly input: {\n readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>\n }\n}) => Predicate & Reads<E['name']>\n\n/** One row's new values, in the store's wire shape: what a client patches and a server writes. */\nexport interface BoundWrite {\n readonly entity: string\n readonly id: string\n readonly values: Readonly<Record<string, unknown>>\n /**\n * Each `one` relation it points, by the relation's key: the row it points at,\n * or none for an optional relation set to nothing. How a ref is carried (a\n * key on the wire, a foreign key in a table) is each interpreter's.\n */\n readonly links: Readonly<Record<string, Option.Option<EntityRef>>>\n /** Each field it moves, by the amount, encoded: added by the server, and predicted over what a client holds. */\n readonly increments: Readonly<Record<string, number>>\n /** Each field it computes, as data: run by the server where it writes, predicted by a client. */\n readonly assignments: Readonly<Record<string, Computation>>\n}\n\nconst fail = (input: EntityInput<AnyEntity, any, any>, message: string): never => {\n throw new Error(`[foldkit-entity] Write of ${input.entity.name}: ${message}`)\n}\n\nconst membersOf = (\n input: EntityInput<AnyEntity, any, any>,\n): Readonly<Record<string, InputMember | undefined>> => input.members\n\n/** A field's value as the store holds it. A field's schema needs no service, so none is asked. */\nconst encoded = (field: AnyField, value: unknown): unknown =>\n Schema.encodeUnknownSync(field.schema as unknown as Schema.Codec<unknown, unknown>)(value)\n\n/**\n * A key's value as given, when it was: an absent key, and an `Option` that\n * holds nothing, give none, and a `Some` what it holds. What `Entity.input`\n * lets a key map itself to, a write can bind.\n */\nconst given = (value: unknown): Option.Option<unknown> => {\n if (value === undefined) return Option.none()\n return Option.isOption(value) ? value : Option.some(value)\n}\n\nconst fieldAt = (input: EntityInput<AnyEntity, any, any>, key: string, role: string): AnyField => {\n const member = own(membersOf(input), key)\n if (member === undefined) return fail(input, `${role} \"${key}\" is not a key of the input`)\n if (member._tag !== 'Field') return fail(input, `${role} \"${key}\" is not mapped to a field`)\n return member\n}\n\n/**\n * What every kind shares: the key naming the row, the expected revision, and\n * what the other keys set or point. A kind adds its own checks.\n */\nconst declared = <E extends AnyEntity, Fields extends Schema.Struct.Fields, Kind extends WriteKind>(\n kind: Kind,\n input: EntityInput<E, Fields, any>,\n options: {\n readonly id: keyof Fields & string\n readonly expect?: keyof Fields & string\n readonly when?: WriteCondition<E, Fields> | undefined\n readonly increment?: Readonly<Record<string, string | undefined>>\n readonly compute?: WriteComputations<E, Fields> | undefined\n },\n): Write<E, Fields, Kind> => {\n const id = fieldAt(input, options.id, 'id')\n if (id.key !== 'id') fail(input, `id \"${options.id}\" is mapped to \"${id.key}\", not to \"id\"`)\n const idSchema = input.schema.fields[options.id] as unknown as Schema.Top\n const takes = Schema.is(Schema.toType(idSchema) as Schema.Codec<unknown>)\n if (SchemaAST.isOptional(idSchema.ast) || takes(undefined) || takes(Option.none()))\n fail(input, `id \"${options.id}\" may be absent, and a declared write names its row`)\n if (options.expect === options.id) fail(input, 'expect cannot be the id')\n const expect =\n options.expect === undefined\n ? undefined\n : Object.freeze({ key: options.expect, field: fieldAt(input, options.expect, 'expect') })\n const sets: Array<WriteSet> = []\n const links: Array<WriteLink> = []\n for (const [key, member] of Object.entries(membersOf(input))) {\n if (member === undefined || key === options.id || key === options.expect) continue\n switch (member._tag) {\n case 'Field':\n sets.push(Object.freeze({ key, field: member }))\n break\n case 'Unmapped':\n break\n case 'RelationInput':\n if (member.relation.cardinality !== 'one') {\n fail(input, `\"${key}\" is a many relation; a write points one relation at a time`)\n }\n links.push(Object.freeze({ key, relation: member.relation as OneRelation }))\n break\n case 'NestedInput':\n fail(input, `\"${key}\" writes a nested row; a write writes its own row only`)\n }\n }\n const placeholders: Record<string, InputExpr<unknown>> = Object.create(null)\n for (const [key, schema] of Object.entries(input.schema.fields)) {\n placeholders[key] = Expr.input(key, schema as unknown as Schema.Codec<unknown, unknown>)\n }\n const when = Option.map(Option.fromUndefinedOr(options.when), condition => {\n // `where` refuses a predicate over another Entity, which the type allows when it is unmarked.\n return Query.from(input.entity).pipe(\n Query.where(condition({ input: placeholders as never }) as Predicate),\n ) as AnyQuery\n })\n const increments = Object.entries(options.increment ?? {}).flatMap(([fieldKey, key]) =>\n key === undefined ? [] : [incrementOf(input, fieldKey, key, { expect, sets })],\n )\n const fields = input.entity.fields as Readonly<Record<string, E['fields'][string]>>\n const row = Object.fromEntries(\n Object.entries(fields).map(([key, field]) => [key, Expr.field(field)]),\n ) as unknown as BeforeRow<E>\n const computes: ReadonlyArray<\n readonly [string, WriteComputations<E, Fields>[keyof E['fields'] & string]]\n > = Object.entries(options.compute ?? {})\n const assignments = computes.flatMap(([fieldKey, compute]) =>\n compute === undefined\n ? []\n : [\n assignmentOf(input, fieldKey, () => compute({ row, input: placeholders as never }), {\n expect,\n sets,\n increments,\n }),\n ],\n )\n // What the context sets is never the input's, on any write.\n const written = [\n ...sets.map(set => set.field.key),\n ...links.map(link => link.relation.key),\n ...increments.map(increment => increment.field.key),\n ...assignments.map(assignment => assignment.field.key),\n ].find(key => Object.hasOwn(input.entity.sets, key))\n if (written !== undefined)\n fail(input, `\"${written}\" is set by the write's context (Entity.sets); no input sets it`)\n return Object.freeze({\n _tag: kind,\n input,\n id: Object.freeze({ key: options.id, field: id }),\n expect,\n when,\n sets: Object.freeze(sets),\n increments: Object.freeze(increments),\n assignments: Object.freeze(assignments),\n links: Object.freeze(links),\n context: [],\n })\n}\n\n/**\n * What the context sets on a write of `entity`: every member on an insert\n * (`create`), those set `on: 'write'` on an update (`write`).\n */\nconst contextOf = (entity: AnyEntity, on: 'create' | 'write'): ReadonlyArray<ContextSet> => {\n const relations: Readonly<Record<string, EntityMember>> = entity.relations\n const fields: Readonly<Record<string, AnyField>> = entity.fields\n // `Entity.sets` took a field, or a `one` relation.\n return Object.entries(entity.sets).flatMap(([key, set]) =>\n on === 'write' && set.on === 'create'\n ? []\n : [\n Object.freeze({\n member: Object.hasOwn(relations, key) ? (relations[key] as OneRelation) : fields[key]!,\n value: set.value,\n }),\n ],\n )\n}\n\n/** The kind of one stored form, as far as an assignment must agree with its field. */\nconst storedKind = (ast: SchemaAST.AST): string => {\n if (SchemaAST.isNull(ast)) return 'null'\n const literal = SchemaAST.isLiteral(ast) ? typeof ast.literal : undefined\n if (SchemaAST.isNumber(ast) || literal === 'number') return 'number'\n if (SchemaAST.isString(ast) || SchemaAST.isTemplateLiteral(ast) || literal === 'string') {\n return 'text'\n }\n if (SchemaAST.isBoolean(ast) || literal === 'boolean') return 'boolean'\n return 'other'\n}\n\n/** What a write's context comes to: see `Write.contextValues`. */\nexport interface ContextValues {\n readonly values: Readonly<Record<string, unknown>>\n readonly links: Readonly<Record<string, Option.Option<EntityRef>>>\n readonly unfilled: ReadonlyArray<string>\n}\n\n/**\n * One member of one row, as a holder has it: a field's encoded value, or a\n * `one` relation's ref key (`'User:ada'`) or null; unheld where it lacks it. A\n * server reads every row its asker may see, and reads a hidden one as null.\n */\nexport type ContextRead<E, R> = (row: EntityRef, key: string) => Effect.Effect<Held<unknown>, E, R>\n\n/**\n * What a path from the viewer reads: null for a visitor or a viewer of\n * another Entity, or where a link is null; unheld where `read` lacks a row.\n */\nconst walked = <E, R>(\n path: ViewerPathExpr<unknown>,\n by: ExprContext,\n read: ContextRead<E, R>,\n): Effect.Effect<Held<unknown>, E, R> =>\n Option.match(viewerIdOf(path.viewer.entity, by), {\n onNone: () => Effect.succeed(Held.of(null)),\n onSome: id =>\n Effect.gen(function* () {\n let at: EntityRef = { entity: path.viewer.entity.name, id }\n for (const relation of path.via) {\n const link = yield* read(at, relation.key)\n if (link._tag === 'Unheld') return link\n if (link.value === null || link.value === undefined) return Held.of(null)\n at = {\n entity: relation.target().name,\n id: idOfRef(link.value, `${relation.owner.name}.${relation.key}`),\n }\n }\n const end = yield* read(at, path.field.key)\n return end._tag === 'Unheld' ? end : Held.of(end.value ?? null)\n }),\n })\n\n/** Whether a field stores a null. */\nconst holdsNull = (field: AnyField): boolean =>\n storedKinds(Schema.toEncoded(field.schema as unknown as Schema.Top).ast).has('null')\n\n/** Every kind a stored form may hold: a union's members' kinds. */\nconst storedKinds = (ast: SchemaAST.AST): ReadonlySet<string> =>\n new Set(\n SchemaAST.isUnion(ast)\n ? ast.types.flatMap(member => [...storedKinds(member)])\n : [storedKind(ast)],\n )\n\n/** The stored kinds a scalar may come to, by what it is. */\nconst kindsOf = (expr: AnyExpr): ReadonlySet<string> => {\n switch (expr._tag) {\n case 'Field':\n return storedKinds(Schema.toEncoded(expr.schema).ast)\n case 'Next':\n return storedKinds(Schema.toEncoded(expr.field.schema).ast)\n case 'Input':\n return storedKinds(Schema.toEncoded(expr.schema).ast)\n case 'Literal':\n return new Set([\n expr.value === null ? 'null' : typeof expr.value === 'string' ? 'text' : typeof expr.value,\n ])\n case 'Call':\n return storedKinds(Schema.toEncoded(expr.operator.output).ast)\n // Refused in an assignment, below; a visitor's viewer is null.\n case 'Viewer':\n case 'Now':\n return new Set([...storedKinds(Schema.toEncoded(expr.schema).ast), 'null'])\n // Refused in an assignment, below; a path reaching no row reads null.\n case 'Path':\n case 'ViewerPath':\n return new Set([...storedKinds(Schema.toEncoded(expr.field.schema).ast), 'null'])\n }\n}\n\n/**\n * Whether `expr` may come to null: a nullable field or input, a null literal,\n * an operator answering one, or a strict operator given an argument that may\n * be null, which it answers with null.\n */\nconst mayBeNull = (expr: AnyExpr): boolean =>\n kindsOf(expr).has('null') ||\n (expr._tag === 'Call' &&\n expr.operator.nulls === 'strict' &&\n expr.args.some(arg => !isPredicate(arg) && mayBeNull(arg)))\n\n/** Whether every value a schema stores, null aside, is checked to be an integer. */\nconst storesIntegers = (schema: Schema.Top): boolean => {\n const present = SchemaShape.present(Schema.toEncoded(schema).ast)\n return present.length > 0 && present.every(SchemaShape.holdsIntegers)\n}\n\n/** Whether a scalar comes to an integer (or null) by what it is: its schema, or a literal's value. */\nconst comesToInteger = (expr: AnyExpr): boolean =>\n Match.value(expr).pipe(\n Match.tags({\n Field: ({ schema }) => storesIntegers(schema),\n Input: ({ schema }) => storesIntegers(schema),\n Literal: ({ value }) => value === null || Number.isInteger(value),\n Call: ({ operator }) => storesIntegers(operator.output),\n }),\n // What else an expression reads is refused in an assignment before this is asked.\n Match.orElse(() => false),\n )\n\n/** Whether a predicate is anywhere in `expr`, which data carries only as a value. */\nconst holdsPredicate = (expr: Operandish): boolean =>\n isPredicate(expr) || (expr._tag === 'Call' && expr.args.some(holdsPredicate))\n\n/**\n * One computed field, checked: a field of the Entity written by nothing else,\n * computed from this Entity's row before the write and the input alone\n * (never `Next`, so assignments do not depend on each other), to a value of\n * the field's stored kind, with no predicate in it.\n */\nconst assignmentOf = (\n input: EntityInput<AnyEntity, any, any>,\n fieldKey: string,\n compute: () => AnyExpr,\n declared: {\n readonly expect: WriteSet | undefined\n readonly sets: ReadonlyArray<WriteSet>\n readonly increments: ReadonlyArray<WriteIncrement>\n },\n): WriteAssignment => {\n const fields = input.entity.fields as Readonly<Record<string, AnyField>>\n const field = Object.hasOwn(fields, fieldKey) ? fields[fieldKey] : undefined\n if (field === undefined) return fail(input, `computes \"${fieldKey}\", which is not a field`)\n if (fieldKey === 'id') return fail(input, 'computes the id')\n if (declared.expect?.field.key === fieldKey) {\n return fail(input, `computes \"${fieldKey}\", the revision it expects`)\n }\n if (\n declared.sets.some(set => set.field.key === fieldKey) ||\n declared.increments.some(increment => increment.field.key === fieldKey)\n ) {\n return fail(input, `computes \"${fieldKey}\", which the write also sets`)\n }\n const operand: Operandish = compute()\n const expr = isPredicate(operand) ? operand : Expr.expanded(operand)\n if (isPredicate(expr) || holdsPredicate(expr)) {\n return fail(\n input,\n `computes \"${fieldKey}\" with a predicate, which an assignment carries only as a value`,\n )\n }\n const reads = dependenciesOf(expr)\n if (reads.next.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" from the row after the write, which an assignment never reads`,\n )\n }\n const context = reads.operations.filter(\n operation => operation === 'viewer' || operation === 'now',\n )\n if (context.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" from the write's context (${context.map(term => `Expr.${term}`).join(', ')}), which an assignment does not read; a value the context gives is declared with Entity.sets`,\n )\n }\n if (reads.reaches.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" through a relation, from another row, which an assignment does not read`,\n )\n }\n const stranger = reads.fields.find(read => read.owner.name !== input.entity.name)\n if (stranger !== undefined) {\n return fail(\n input,\n `computes \"${fieldKey}\" from ${stranger.owner.name}.${stranger.key}, another Entity's field`,\n )\n }\n const stored = storedKinds(Schema.toEncoded(field.schema as unknown as Schema.Top).ast)\n const comes = [...kindsOf(expr)].filter(kind => kind !== 'null' && !stored.has(kind))\n if (comes.length > 0) {\n return fail(input, `computes \"${fieldKey}\" as ${comes.join(' or ')}, which it does not store`)\n }\n // Memory would show the null, and the database refuse it as NOT NULL.\n if (!stored.has('null') && mayBeNull(expr)) {\n return fail(input, `computes \"${fieldKey}\" as possibly null, which it does not store`)\n }\n // As with an increment's amount: Postgres rounds a fraction written to an integer\n // column, SQLite stores it, and memory keeps it. So it never reaches either.\n const integer = comesToInteger(expr)\n if (storesIntegers(field.schema as unknown as Schema.Top) && !integer) {\n return fail(\n input,\n `computes \"${fieldKey}\" as a number that may not be an integer, but \"${fieldKey}\" is one; compute it with an operator, input or literal that is (Schema.Int)`,\n )\n }\n return Object.freeze({ field, expr, integer })\n}\n\n/** An assignment's expression, bound to one input, as data. */\nconst computationOf = (\n write: AnyWrite,\n expr: AnyExpr,\n input: Readonly<Record<string, unknown>>,\n): Computation => {\n switch (expr._tag) {\n // `Next` is refused where an assignment is declared.\n case 'Field':\n case 'Next':\n return Computation.Field({ key: expr._tag === 'Field' ? expr.key : expr.field.key })\n case 'Literal':\n return Computation.Value({ value: expr.value })\n case 'Input':\n return Computation.Value({\n value: Schema.encodeUnknownSync(\n write.input.schema.fields[expr.key] as unknown as Schema.Codec<unknown, unknown>,\n )(input[expr.key]),\n })\n case 'Call':\n return Computation.Call({\n operator:\n expr.operator.version === 1\n ? expr.operator.id\n : `${expr.operator.id}@${expr.operator.version}`,\n args: expr.args.map(arg => computationOf(write, arg as AnyExpr, input)),\n collation: Option.getOrNull(expr.collation),\n })\n // Refused where an assignment is declared, so none reaches here.\n case 'Viewer':\n case 'Now':\n throw new Error(\n `[foldkit-entity] an assignment of ${write.input.entity.name} reads the write's context`,\n )\n case 'Path':\n case 'ViewerPath':\n throw new Error(\n `[foldkit-entity] an assignment of ${write.input.entity.name} reads another row`,\n )\n }\n}\n\n/**\n * What a computation comes to over `row`, the row before the write: none when\n * the row lacks a field it reads, or `operators` lacks an operator it calls.\n */\nconst computed = (\n computation: Computation,\n row: Readonly<Record<string, unknown>>,\n operators: MemoryOperators,\n): Option.Option<unknown> =>\n Computation.$match(computation, {\n Field: ({ key }) => (Object.hasOwn(row, key) ? Option.some(row[key]) : Option.none()),\n Value: ({ value }) => Option.some(value),\n Call: ({ operator, args, collation }) =>\n Option.flatMap(operators.named(operator), descriptor =>\n Option.map(Option.all(args.map(arg => computed(arg, row, operators))), values =>\n applyOperator(descriptor, values, {\n collation: Option.fromNullOr(collation),\n operators,\n }),\n ),\n ),\n })\n\n/** Whether a value's stored form is a plain number. */\nconst storedAsNumber = (schema: Schema.Top): boolean =>\n SchemaAST.isNumber(Schema.toEncoded(schema).ast)\n\n/** Whether a value's stored form is checked to be an integer. */\nconst storedAsInteger = (schema: Schema.Top): boolean =>\n SchemaShape.holdsIntegers(Schema.toEncoded(schema).ast)\n\n/**\n * One increment, checked: a field of the Entity stored as a number, written\n * by nothing else, moved by an unmapped input key whose value is a number, and\n * an integer when the field is one.\n */\nconst incrementOf = (\n input: EntityInput<AnyEntity, any, any>,\n fieldKey: string,\n key: string,\n declared: { readonly expect: WriteSet | undefined; readonly sets: ReadonlyArray<WriteSet> },\n): WriteIncrement => {\n const fields = input.entity.fields as Readonly<Record<string, AnyField>>\n const field = Object.hasOwn(fields, fieldKey) ? fields[fieldKey] : undefined\n if (field === undefined) return fail(input, `increments \"${fieldKey}\", which is not a field`)\n if (fieldKey === 'id') return fail(input, 'increments the id')\n if (declared.expect?.field.key === fieldKey) {\n return fail(input, `increments \"${fieldKey}\", the revision it expects`)\n }\n if (declared.sets.some(set => set.field.key === fieldKey)) {\n return fail(input, `increments \"${fieldKey}\", which a key also sets`)\n }\n if (!storedAsNumber(field.schema as unknown as Schema.Top)) {\n return fail(input, `increments \"${fieldKey}\", which is not stored as a number`)\n }\n const member = own(membersOf(input), key)\n if (member === undefined) return fail(input, `\"${key}\" is not a key of the input`)\n if (member._tag !== 'Unmapped') {\n return fail(input, `\"${key}\" is an amount, so it must be unmapped, not written as a member`)\n }\n const amount = input.schema.fields[key] as unknown as Schema.Top\n if (!storedAsNumber(amount)) return fail(input, `\"${key}\" is an amount, but not a number`)\n // The databases split over a fraction added to an integer: Postgres refuses the\n // statement, SQLite stores it in the integer column. So it never reaches either.\n const integer = storedAsInteger(amount)\n if (storedAsInteger(field.schema as unknown as Schema.Top) && !integer) {\n return fail(\n input,\n `\"${key}\" may not be an integer, but \"${fieldKey}\" is one; give the amount Schema.Int`,\n )\n }\n return Object.freeze({ key, field, integer })\n}\n\n/** An update or an insert that would write nothing is refused where it is declared. */\nconst settingSomething = <\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind,\n>(\n write: Write<E, Fields, Kind>,\n): Write<E, Fields, Kind> =>\n write.sets.length === 0 &&\n write.links.length === 0 &&\n write.increments.length === 0 &&\n write.assignments.length === 0 &&\n write.context.length === 0\n ? fail(write.input, 'it sets no field')\n : write\n\nexport const Write = {\n /**\n * Sets, on the row whose id is the input's `id` key, every field the input\n * maps a key to. `expect` names the key holding the revision the row was\n * read at: an interpreter writes only a row still at that revision.\n *\n * ```ts\n * const EditPost = Write.update(EditPostInput, { id: 'id' })\n * ```\n *\n * A key mapped to a `one` relation (`Relation.input`) points the row at the\n * id it holds. Refused where it is written: an `id` that is not the Entity's\n * `id` field, a `many` relation or a nested key (not written by an update\n * yet), or nothing to set.\n *\n * `when` is what the row must hold for the update to land, checked where it\n * commits (`({ input }) => Expr.eq(Post.fields.status, 'draft')`); a row it\n * does not hold of is refused as unmet.\n *\n * `compute` sets a field to what an expression comes to over the row before\n * the write and the input, run where it commits\n * (`{ label: ({ row }) => concat(row.title, ' draft') }`). It is refused for\n * a field another key writes, an expression over the row after the write or\n * another Entity, or one of a kind the field does not store.\n */\n update,\n\n /**\n * Inserts the row whose id is the input's `id` key, an id the client chose,\n * with every field and `one` relation the input maps a key to. An insert\n * expects no revision: there is no row to have read. A row already there\n * under that id (a retry) is left as it is.\n *\n * What its Entity sets from the write's context (`Entity.sets`: who asks,\n * when) it writes too, never from the input: a server fills them, a client\n * predicts them. An update writes those set `on: 'write'`.\n */\n insert: <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: { readonly id: keyof Fields & string },\n ): Write<E, Fields, 'Insert'> =>\n settingSomething(\n Object.freeze({\n ...declared('Insert', input, options),\n context: contextOf(input.entity, 'create'),\n }),\n ),\n\n /**\n * Deletes the row whose id is the input's `id` key, only while it is still at\n * the revision `expect` names, when it names one. It sets nothing, so a key\n * mapped to a member is refused where it is written. A row already gone (a\n * retry) is gone. `when`, as an update's, is what the row must hold.\n */\n delete: remove,\n\n /**\n * The revision this input says the row was read at, encoded, with its field:\n * what an interpreter writes only a row still at. None for a write that\n * expects none.\n */\n expected: <Fields extends Schema.Struct.Fields>(\n write: Write<AnyEntity, Fields>,\n value: Schema.Struct.Type<Fields>,\n ): Option.Option<{ readonly field: string; readonly revision: unknown }> =>\n Option.map(Option.fromUndefinedOr(write.expect), ({ key, field }) => ({\n field: field.key,\n revision: encoded(field, (value as Readonly<Record<string, unknown>>)[key]),\n })),\n\n /** The members any invocation may write: what a query reading none of them cannot be changed by. */\n writes: (write: AnyWrite): ReadonlyArray<AnyField | OneRelation> => [\n ...write.sets.map(set => set.field),\n ...write.increments.map(increment => increment.field),\n ...write.assignments.map(assignment => assignment.field),\n ...write.links.map(link => link.relation),\n ...write.context.map(set => set.member),\n ],\n\n /**\n * What the context sets come to, encoded as the store holds them: each\n * field's value and each relation's link. A path from the viewer is read\n * through `read`, one row at a time. `unfilled` names each member there is\n * no value for: none to read (no reading of the time, a row or field\n * `read` does not hold), or a null where the member holds none (a visitor,\n * a viewer of another Entity, a path reaching no row). A server refuses such\n * a write; a client predicts nothing for the member.\n */\n contextValues: <E, R>(\n write: AnyWrite,\n by: ExprContext,\n read: ContextRead<E, R>,\n ): Effect.Effect<ContextValues, E, R> =>\n Effect.gen(function* () {\n const values: Record<string, unknown> = {}\n const links: Record<string, Option.Option<EntityRef>> = {}\n const unfilled: Array<string> = []\n for (const { member, value } of write.context) {\n const comes: Option.Option<unknown> =\n value._tag === 'ViewerPath'\n ? Held.match(yield* walked(value, by, read), {\n onHeld: held => Option.some(held),\n onUnheld: () => Option.none(),\n })\n : contextValue(value, by)\n if (Option.isNone(comes)) {\n unfilled.push(member.key)\n } else if (member._tag === 'Relation') {\n // `Entity.sets` points a relation only at the viewer of its target.\n const id = comes.value\n if (id !== null)\n put(links, member.key, Option.some({ entity: member.target().name, id: String(id) }))\n else if (member.optional) put(links, member.key, Option.none())\n else unfilled.push(member.key)\n } else if (comes.value !== null || holdsNull(member)) {\n put(values, member.key, comes.value)\n } else {\n unfilled.push(member.key)\n }\n }\n return { values, links, unfilled }\n }),\n\n /** The Entities a write's paths from the viewer read, by name: what a server must have a source of. */\n contextReads: (write: AnyWrite): ReadonlyArray<string> => [\n ...new Set(\n write.context.flatMap(({ value }) =>\n value._tag === 'ViewerPath'\n ? [value.viewer.entity.name, ...value.via.map(relation => relation.target().name)]\n : [],\n ),\n ),\n ],\n\n /**\n * The fields a write reads from the row it writes: those its `when` reads,\n * each it increments, each its assignments read, and the revision it\n * expects. What an interpreter must read, and what a startup check judges\n * it could disclose.\n */\n reads: (write: AnyWrite): ReadonlyArray<AnyField> => {\n const fields = new Map<string, AnyField>()\n const add = (field: AnyField) => fields.set(field.key, field)\n Option.map(write.when, body =>\n Query.dependencies(body).predicate.forEach(field => add(field as unknown as AnyField)),\n )\n write.increments.forEach(increment => add(increment.field))\n write.assignments.forEach(assignment =>\n dependenciesOf(assignment.expr).fields.forEach(field => add(field as unknown as AnyField)),\n )\n if (write.expect !== undefined) add(write.expect.field)\n return [...fields.values()]\n },\n\n /**\n * The row this input writes and its new values, encoded as the store holds\n * them: the patch a client shows before the server answers. `keys`, when\n * given, narrows it to those input keys (what an author changed); the rest of\n * the input is still the whole, validated value.\n */\n bind: <Fields extends Schema.Struct.Fields>(\n write: Write<AnyEntity, Fields>,\n value: Schema.Struct.Type<Fields>,\n keys?: ReadonlyArray<string>,\n ): BoundWrite => {\n const input = value as Readonly<Record<string, unknown>>\n const wanted = keys === undefined ? undefined : new Set(keys)\n const asked = (key: string) => wanted === undefined || wanted.has(key)\n const values: Record<string, unknown> = {}\n for (const { key, field } of write.sets) {\n if (!asked(key)) continue\n // A key the input leaves out, or an `Option` holding nothing, writes nothing.\n Option.map(given(own(input, key)), held => put(values, field.key, encoded(field, held)))\n }\n const links: Record<string, Option.Option<EntityRef>> = {}\n for (const { key, relation } of write.links) {\n if (!asked(key)) continue\n const target = relation.target()\n put(\n links,\n relation.key,\n Option.map(Option.flatMap(given(own(input, key)), Option.fromNullishOr), id => ({\n entity: target.name,\n id: String(encoded(target.fields.id as AnyField, id)),\n })),\n )\n }\n const increments: Record<string, number> = {}\n for (const { key, field } of write.increments) {\n // `keys` names input keys, so an increment is asked for by its amount's key.\n if (!asked(key)) continue\n const schema = write.input.schema.fields[key] as unknown as Schema.Codec<unknown, number>\n Option.map(given(own(input, key)), held =>\n put(increments, field.key, Schema.encodeUnknownSync(schema)(held)),\n )\n }\n // Every assignment: it reads the row as well as the input, so no key alone asks for it.\n const assignments: Record<string, Computation> = {}\n for (const { field, expr } of write.assignments) {\n put(assignments, field.key, computationOf(write, expr, input))\n }\n return {\n entity: write.input.entity.name,\n id: String(encoded(write.id.field, input[write.id.key])),\n values,\n links,\n increments,\n assignments,\n }\n },\n\n /**\n * The fields of the row before it that a bound write's amounts and\n * assignments read: what to read to work out the row it leaves.\n */\n boundReads: (bound: Pick<BoundWrite, 'increments' | 'assignments'>): ReadonlyArray<string> => {\n const keys = new Set(Object.keys(bound.increments))\n const walk = (computation: Computation): void =>\n Computation.$match(computation, {\n Field: ({ key }) => void keys.add(key),\n Value: () => undefined,\n Call: ({ args }) => args.forEach(walk),\n })\n Object.values(bound.assignments).forEach(walk)\n return [...keys]\n },\n\n /**\n * The row a bound write leaves over `row`, the row before it: its values\n * set, each amount added (a null stays null, as in SQL), and each\n * assignment computed by `operators` over the row before. `unknown` names\n * each field it could not work out: one `row` lacks, or holds as no number\n * for an amount, or an assignment calling an operator the table lacks. That\n * field keeps what `row` holds. The one computation of this row: a guard\n * judges it, a writer explains a miss with it, a client predicts it.\n */\n apply: (\n row: Readonly<Record<string, unknown>>,\n bound: Pick<BoundWrite, 'values' | 'increments' | 'assignments'>,\n options: { readonly operators: MemoryOperators },\n ): {\n readonly row: Readonly<Record<string, unknown>>\n readonly unknown: ReadonlyArray<string>\n } => {\n const after: Record<string, unknown> = { ...row, ...bound.values }\n const unknown: Array<string> = []\n for (const [field, amount] of Object.entries(bound.increments)) {\n const held = Object.hasOwn(row, field) ? row[field] : undefined\n if (held === null) after[field] = null\n else if (typeof held === 'number') after[field] = held + amount\n else unknown.push(field)\n }\n for (const [field, computation] of Object.entries(bound.assignments)) {\n Option.match(computed(computation, row, options.operators), {\n onNone: () => unknown.push(field),\n onSome: value => put(after, field, value),\n })\n }\n return { row: after, unknown }\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiGA,MAAa,cAAc,KAAK,WAAwB;AA2IxD,MAAM,UACJ,OACA,YAEA,iBACE,OAAO,OAAO;CACZ,GAAG,SAAS,UAAU,OAAO,OAAO;CACpC,SAAS,UAAU,MAAM,QAAQ,OAAO;AAC1C,CAAC,CACH;AAEF,MAAM,UAAuB,OAAyC,YAA6B;CACjG,MAAM,QAAQ,SAAS,UAAU,OAAO,OAAO;CAC/C,MAAM,QAAQ,CAAC,GAAG,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,CAAC;CAC9C,IAAI,UAAU,KAAA,GAAW,KAAK,OAAO,+BAA+B,MAAM,IAAI,YAAY;CAC1F,OAAO;AACT;AA8BA,M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AW,CAAC,CAAC,QAAQ,IAAI;EAC7C,OAAO,CAAC,GAAG,IAAI;CACjB;;;;;;;;;;CAWA,QACE,KACA,OACA,YAIG;EACH,MAAM,QAAiC;GAAE,GAAG;GAAK,GAAG,MAAM;EAAO;EACjE,MAAM,UAAyB,CAAC;EAChC,KAAK,MAAM,CAAC,OAAO,WAAW,OAAO,QAAQ,MAAM,UAAU,GAAG;GAC9D,MAAM,OAAO,OAAO,OAAO,KAAK,KAAK,IAAI,IAAI,SAAS,KAAA;GACtD,IAAI,SAAS,MAAM,MAAM,SAAS;QAC7B,IAAI,OAAO,SAAS,UAAU,MAAM,SAAS,OAAO;QACpD,QAAQ,KAAK,KAAK;EACzB;EACA,KAAK,MAAM,CAAC,OAAO,gBAAgB,OAAO,QAAQ,MAAM,WAAW,GACjE,OAAO,MAAM,SAAS,aAAa,KAAK,QAAQ,SAAS,GAAG;GAC1D,cAAc,QAAQ,KAAK,KAAK;GAChC,SAAQ,UAAS,IAAI,OAAO,OAAO,KAAK;EAC1C,CAAC;EAEH,OAAO;GAAE,KAAK;GAAO;EAAQ;CAC/B;AACF"}
|
|
1
|
+
{"version":3,"file":"write.js","names":[],"sources":["../src/write.ts"],"sourcesContent":["/**\n * A write as a value: what an operation changes, declared over the input it\n * takes, so the changes can be known before it runs and its interpreters can\n * agree on what it means.\n *\n * A `Write` is built on `Entity.input`, which already says which input key is\n * which member of the Entity. `Write.update`, `Write.insert` and `Write.delete`\n * add what to do with them: the key that names the row, and optionally the key\n * holding the revision it was read at. Every other key mapped to a field is\n * set; a key mapped to `Entity.unmapped` is about the operation and is not\n * written.\n *\n * It is authority someone declared, never something derived from an Entity: an\n * Entity having a field does not mean anyone may set it.\n */\nimport { Data, Effect, Match, Option, Schema, SchemaAST } from 'effect'\nimport type {\n AnyEntity,\n EntityMember,\n EntityField,\n EntityInput,\n EntityRef,\n EntityRelation,\n InputMember,\n} from './index.js'\nimport type { Collation } from './collation.js'\nimport { applyOperator } from './evaluate.js'\nimport {\n Expr,\n Query,\n dependenciesOf,\n isPredicate,\n type AnyExpr,\n type AnyQuery,\n type InputExpr,\n type Operandish,\n type Predicate,\n type Reads,\n} from './expr.js'\nimport type { BeforeRow } from './invariant.js'\nimport { contextValue, viewerIdOf, type ExprContext, type ViewerPathExpr } from './context.js'\nimport { Held, idOfRef } from './held.js'\nimport type { MemoryOperators } from './operators.js'\nimport { own, put } from './record.js'\nimport { SchemaShape } from './shape.js'\nimport type { ContextValue } from './sets.js'\n\ntype AnyField = EntityField<string, string, Schema.Constraint>\ntype OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>\n\n/** One input key the write sets, and the field it sets. */\nexport interface WriteSet {\n readonly key: string\n readonly field: AnyField\n}\n\n/**\n * One input key holding an amount, and the field it moves. `integer` is whether\n * the amount's schema holds it to an integer (`Schema.isInt`): a writer over an\n * integer column refuses an increment without it, which the Entity cannot see.\n */\nexport interface WriteIncrement extends WriteSet {\n readonly integer: boolean\n}\n\n/**\n * One field the write computes (`label := concat(title, ' draft')`): an\n * expression over the row before the write and the input, every defined\n * operator in it expanded, so what is carried is primitive calls alone.\n */\nexport interface WriteAssignment {\n readonly field: AnyField\n readonly expr: AnyExpr\n /**\n * Whether `expr` comes to an integer, by the schemas of what it reads and\n * calls: a writer over an integer column refuses an assignment without it,\n * which the Entity cannot see.\n */\n readonly integer: boolean\n}\n\n/**\n * A computed assignment as data, bound to one input: what travels to a server\n * and sits in a client's pending patch, which holds no descriptor and no\n * function. A field of the row before the write, a stored value, or a call of\n * a primitive operator by name (`id`, or `id@version`), with the collation it\n * compares text under (`null` for none: this is JSON).\n */\nexport type Computation = Data.TaggedEnum<{\n Field: { readonly key: string }\n Value: { readonly value: unknown }\n Call: {\n readonly operator: string\n readonly args: ReadonlyArray<Computation>\n readonly collation: Collation | null\n }\n}>\nexport const Computation = Data.taggedEnum<Computation>()\n\n/** One input key the write points at another row with: a `one` relation, by the target's id. */\nexport interface WriteLink {\n readonly key: string\n readonly relation: OneRelation\n}\n\n/** One member the write's context sets (`Entity.sets`): a field or a `one` relation, and its value. */\nexport interface ContextSet {\n readonly member: AnyField | OneRelation\n readonly value: ContextValue\n}\n\n/** What a declared write does to its row. */\nexport type WriteKind = 'Update' | 'Insert' | 'Delete'\n\n/**\n * One row of an Entity, by id, from an operation's input: updated, inserted\n * with an id the client chose, or deleted. `Kind` is the one its constructor\n * made, so what it may be refused with can follow it.\n */\nexport interface Write<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind = WriteKind,\n> {\n readonly _tag: Kind\n readonly input: EntityInput<E, Fields, any>\n /** The input key holding the row's id, and the Entity's `id` field it is. */\n readonly id: WriteSet & { readonly key: keyof Fields & string }\n /**\n * The input key holding the revision the row was read at, and its field, when\n * the write is refused for a row that moved on since.\n */\n readonly expect: (WriteSet & { readonly key: keyof Fields & string }) | undefined\n /**\n * What the row must hold for the write to land, over the Entity, read\n * before the write and checked where it commits: an interpreter writes only\n * a row it holds of, and refuses the rest as unmet.\n */\n readonly when: Option.Option<AnyQuery>\n /** The keys it sets, in the input's order. */\n readonly sets: ReadonlyArray<WriteSet>\n /**\n * The fields it moves by an amount the input holds (`stock + quantity`),\n * computed where the write commits: the input key, and the field it moves.\n */\n readonly increments: ReadonlyArray<WriteIncrement>\n /** The fields it computes, each from the row before the write and the input. */\n readonly assignments: ReadonlyArray<WriteAssignment>\n /** The `one` relations it points elsewhere, in the input's order. */\n readonly links: ReadonlyArray<WriteLink>\n /**\n * What the write's context sets where it lands (`Entity.sets`): an insert\n * every such member, an update those set `on: 'write'`, a delete none.\n */\n readonly context: ReadonlyArray<ContextSet>\n}\n\nexport type AnyWrite = Write<AnyEntity, any>\n\n/** A write declared with a precondition, so it may be refused as unmet. */\nexport type ConditionedWrite<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind = WriteKind,\n> = Write<E, Fields, Kind> & { readonly when: Option.Some<AnyQuery> }\n\ninterface RowOptions<Fields extends Schema.Struct.Fields> {\n readonly id: keyof Fields & string\n readonly expect?: keyof Fields & string\n}\n\n/** The keys of `Fields` whose values are numbers. */\ntype NumberKeys<Fields> = {\n [K in keyof Fields & string]: Fields[K] extends Schema.Top\n ? Schema.Schema.Type<Fields[K]> extends number\n ? K\n : never\n : never\n}[keyof Fields & string]\n\n/** An update's increments: a number field of the Entity, by the input key holding the amount. */\nexport type WriteIncrements<E extends AnyEntity, Fields extends Schema.Struct.Fields> = {\n readonly [\n K in NumberKeys<{ [F in keyof E['fields']]: E['fields'][F]['schema'] }>\n ]?: NumberKeys<Fields>\n}\n\n/**\n * An update's computed fields: each a field of the Entity, by what it comes\n * to over the row before the write (`row`, each field as `Expr.field`) and\n * the input. Its kind is checked against the field's where it is declared.\n */\nexport type WriteComputations<E extends AnyEntity, Fields extends Schema.Struct.Fields> = {\n readonly [F in keyof E['fields'] & string]?: (context: {\n readonly row: BeforeRow<E>\n readonly input: {\n readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>\n }\n }) => AnyExpr\n}\n\n/** An update's options: a row's, what it moves by an amount, and what it computes. */\ntype UpdateOptions<\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n> = RowOptions<Fields> & {\n readonly increment?: WriteIncrements<E, Fields>\n readonly compute?: WriteComputations<E, Fields>\n}\n\n/** `update`: conditioned when given `when`, which the overloads tell apart. */\ninterface UpdateWrite {\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: UpdateOptions<E, Fields> & { readonly when: WriteCondition<E, Fields> },\n ): ConditionedWrite<E, Fields, 'Update'>\n // `when?: undefined`, so an optional `when` in a variable cannot pick this one and lose `Unmet`.\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: UpdateOptions<E, Fields> & { readonly when?: undefined },\n ): Write<E, Fields, 'Update'>\n}\n\n/** `delete`: conditioned when given `when`, as an update is. */\ninterface DeleteWrite {\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: RowOptions<Fields> & { readonly when: WriteCondition<E, Fields> },\n ): ConditionedWrite<E, Fields, 'Delete'>\n <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: RowOptions<Fields> & { readonly when?: undefined },\n ): Write<E, Fields, 'Delete'>\n}\n\n// `declared` sets `when` exactly when it is given, which is what the overloads say.\nconst update: UpdateWrite = (\n input: EntityInput<AnyEntity, any, any>,\n options: UpdateOptions<AnyEntity, any>,\n) =>\n settingSomething(\n Object.freeze({\n ...declared('Update', input, options),\n context: contextOf(input.entity, 'write'),\n }),\n ) as never\n\nconst remove: DeleteWrite = (input: EntityInput<AnyEntity, any, any>, options: RowOptions<any>) => {\n const write = declared('Delete', input, options)\n const named = [...write.sets, ...write.links][0]\n if (named !== undefined) fail(input, `a delete sets nothing, but \"${named.key}\" is mapped`)\n return write as never\n}\n\n/**\n * A write's precondition: a predicate over its Entity's row, given one\n * placeholder per input key, as a `Query.define` body is. It is built once,\n * so `input` stands for what the write will be given.\n */\nexport type WriteCondition<E extends AnyEntity, Fields extends Schema.Struct.Fields> = (context: {\n readonly input: {\n readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>\n }\n}) => Predicate & Reads<E['name']>\n\n/** One row's new values, in the store's wire shape: what a client patches and a server writes. */\nexport interface BoundWrite {\n readonly entity: string\n readonly id: string\n readonly values: Readonly<Record<string, unknown>>\n /**\n * Each `one` relation it points, by the relation's key: the row it points at,\n * or none for an optional relation set to nothing. How a ref is carried (a\n * key on the wire, a foreign key in a table) is each interpreter's.\n */\n readonly links: Readonly<Record<string, Option.Option<EntityRef>>>\n /** Each field it moves, by the amount, encoded: added by the server, and predicted over what a client holds. */\n readonly increments: Readonly<Record<string, number>>\n /** Each field it computes, as data: run by the server where it writes, predicted by a client. */\n readonly assignments: Readonly<Record<string, Computation>>\n}\n\nconst fail = (input: EntityInput<AnyEntity, any, any>, message: string): never => {\n throw new Error(`[foldkit-entity] Write of ${input.entity.name}: ${message}`)\n}\n\nconst membersOf = (\n input: EntityInput<AnyEntity, any, any>,\n): Readonly<Record<string, InputMember | undefined>> => input.members\n\n/** A field's value as the store holds it. A field's schema needs no service, so none is asked. */\nconst encoded = (field: AnyField, value: unknown): unknown =>\n Schema.encodeUnknownSync(field.schema as unknown as Schema.Codec<unknown, unknown>)(value)\n\n/**\n * A key's value as given, when it was: an absent key, and an `Option` that\n * holds nothing, give none, and a `Some` what it holds. What `Entity.input`\n * lets a key map itself to, a write can bind.\n */\nconst given = (value: unknown): Option.Option<unknown> => {\n if (value === undefined) return Option.none()\n return Option.isOption(value) ? value : Option.some(value)\n}\n\nconst fieldAt = (input: EntityInput<AnyEntity, any, any>, key: string, role: string): AnyField => {\n const member = own(membersOf(input), key)\n if (member === undefined) return fail(input, `${role} \"${key}\" is not a key of the input`)\n const unmapped = () => fail(input, `${role} \"${key}\" is not mapped to a field`)\n return Match.valueTags(member, {\n Field: field => field,\n Unmapped: unmapped,\n RelationInput: unmapped,\n NestedInput: unmapped,\n })\n}\n\n/**\n * What every kind shares: the key naming the row, the expected revision, and\n * what the other keys set or point. A kind adds its own checks.\n */\nconst declared = <E extends AnyEntity, Fields extends Schema.Struct.Fields, Kind extends WriteKind>(\n kind: Kind,\n input: EntityInput<E, Fields, any>,\n options: {\n readonly id: keyof Fields & string\n readonly expect?: keyof Fields & string\n readonly when?: WriteCondition<E, Fields> | undefined\n readonly increment?: Readonly<Record<string, string | undefined>>\n readonly compute?: WriteComputations<E, Fields> | undefined\n },\n): Write<E, Fields, Kind> => {\n const id = fieldAt(input, options.id, 'id')\n if (id.key !== 'id') fail(input, `id \"${options.id}\" is mapped to \"${id.key}\", not to \"id\"`)\n const idSchema = input.schema.fields[options.id] as unknown as Schema.Top\n const takes = Schema.is(Schema.toType(idSchema) as Schema.Codec<unknown>)\n if (SchemaAST.isOptional(idSchema.ast) || takes(undefined) || takes(Option.none()))\n fail(input, `id \"${options.id}\" may be absent, and a declared write names its row`)\n if (options.expect === options.id) fail(input, 'expect cannot be the id')\n const expect =\n options.expect === undefined\n ? undefined\n : Object.freeze({ key: options.expect, field: fieldAt(input, options.expect, 'expect') })\n const sets: Array<WriteSet> = []\n const links: Array<WriteLink> = []\n for (const [key, member] of Object.entries(membersOf(input))) {\n if (member === undefined || key === options.id || key === options.expect) continue\n switch (member._tag) {\n case 'Field':\n sets.push(Object.freeze({ key, field: member }))\n break\n case 'Unmapped':\n break\n case 'RelationInput':\n if (member.relation.cardinality !== 'one') {\n fail(input, `\"${key}\" is a many relation; a write points one relation at a time`)\n }\n links.push(Object.freeze({ key, relation: member.relation as OneRelation }))\n break\n case 'NestedInput':\n fail(input, `\"${key}\" writes a nested row; a write writes its own row only`)\n }\n }\n const placeholders: Record<string, InputExpr<unknown>> = Object.create(null)\n for (const [key, schema] of Object.entries(input.schema.fields)) {\n placeholders[key] = Expr.input(key, schema as unknown as Schema.Codec<unknown, unknown>)\n }\n const when = Option.map(Option.fromUndefinedOr(options.when), condition => {\n // `where` refuses a predicate over another Entity, which the type allows when it is unmarked.\n return Query.from(input.entity).pipe(\n Query.where(condition({ input: placeholders as never }) as Predicate),\n ) as AnyQuery\n })\n const increments = Object.entries(options.increment ?? {}).flatMap(([fieldKey, key]) =>\n key === undefined ? [] : [incrementOf(input, fieldKey, key, { expect, sets })],\n )\n const fields = input.entity.fields as Readonly<Record<string, E['fields'][string]>>\n const row = Object.fromEntries(\n Object.entries(fields).map(([key, field]) => [key, Expr.field(field)]),\n ) as unknown as BeforeRow<E>\n const computes: ReadonlyArray<\n readonly [string, WriteComputations<E, Fields>[keyof E['fields'] & string]]\n > = Object.entries(options.compute ?? {})\n const assignments = computes.flatMap(([fieldKey, compute]) =>\n compute === undefined\n ? []\n : [\n assignmentOf(input, fieldKey, () => compute({ row, input: placeholders as never }), {\n expect,\n sets,\n increments,\n }),\n ],\n )\n // What the context sets is never the input's, on any write.\n const written = [\n ...sets.map(set => set.field.key),\n ...links.map(link => link.relation.key),\n ...increments.map(increment => increment.field.key),\n ...assignments.map(assignment => assignment.field.key),\n ].find(key => Object.hasOwn(input.entity.sets, key))\n if (written !== undefined)\n fail(input, `\"${written}\" is set by the write's context (Entity.sets); no input sets it`)\n return Object.freeze({\n _tag: kind,\n input,\n id: Object.freeze({ key: options.id, field: id }),\n expect,\n when,\n sets: Object.freeze(sets),\n increments: Object.freeze(increments),\n assignments: Object.freeze(assignments),\n links: Object.freeze(links),\n context: [],\n })\n}\n\n/**\n * What the context sets on a write of `entity`: every member on an insert\n * (`create`), those set `on: 'write'` on an update (`write`).\n */\nconst contextOf = (entity: AnyEntity, on: 'create' | 'write'): ReadonlyArray<ContextSet> => {\n const relations: Readonly<Record<string, EntityMember>> = entity.relations\n const fields: Readonly<Record<string, AnyField>> = entity.fields\n // `Entity.sets` took a field, or a `one` relation.\n return Object.entries(entity.sets).flatMap(([key, set]) =>\n on === 'write' && set.on === 'create'\n ? []\n : [\n Object.freeze({\n member: Object.hasOwn(relations, key) ? (relations[key] as OneRelation) : fields[key]!,\n value: set.value,\n }),\n ],\n )\n}\n\n/** The kind of one stored form, as far as an assignment must agree with its field. */\nconst storedKind = (ast: SchemaAST.AST): string => {\n if (SchemaAST.isNull(ast)) return 'null'\n const literal = SchemaAST.isLiteral(ast) ? typeof ast.literal : undefined\n if (SchemaAST.isNumber(ast) || literal === 'number') return 'number'\n if (SchemaAST.isString(ast) || SchemaAST.isTemplateLiteral(ast) || literal === 'string') {\n return 'text'\n }\n if (SchemaAST.isBoolean(ast) || literal === 'boolean') return 'boolean'\n return 'other'\n}\n\n/** What a write's context comes to: see `Write.contextValues`. */\nexport interface ContextValues {\n readonly values: Readonly<Record<string, unknown>>\n readonly links: Readonly<Record<string, Option.Option<EntityRef>>>\n readonly unfilled: ReadonlyArray<string>\n}\n\n/**\n * One member of one row, as a holder has it: a field's encoded value, or a\n * `one` relation's ref key (`'User:ada'`) or null; unheld where it lacks it. A\n * server reads every row its asker may see, and reads a hidden one as null.\n */\nexport type ContextRead<E, R> = (row: EntityRef, key: string) => Effect.Effect<Held<unknown>, E, R>\n\n/**\n * What a path from the viewer reads: null for a visitor or a viewer of\n * another Entity, or where a link is null; unheld where `read` lacks a row.\n */\nconst walked = <E, R>(\n path: ViewerPathExpr<unknown>,\n by: ExprContext,\n read: ContextRead<E, R>,\n): Effect.Effect<Held<unknown>, E, R> =>\n Option.match(viewerIdOf(path.viewer.entity, by), {\n onNone: () => Effect.succeed(Held.of(null)),\n onSome: id =>\n Effect.gen(function* () {\n let at: EntityRef = { entity: path.viewer.entity.name, id }\n for (const relation of path.via) {\n const link = yield* read(at, relation.key)\n if (link._tag === 'Unheld') return link\n if (link.value === null || link.value === undefined) return Held.of(null)\n at = {\n entity: relation.target().name,\n id: idOfRef(link.value, `${relation.owner.name}.${relation.key}`),\n }\n }\n const end = yield* read(at, path.field.key)\n return Held.match(end, {\n onHeld: value => Held.of(value ?? null),\n onUnheld: () => end,\n })\n }),\n })\n\n/** Whether a field stores a null. */\nconst holdsNull = (field: AnyField): boolean =>\n storedKinds(Schema.toEncoded(field.schema as unknown as Schema.Top).ast).has('null')\n\n/** Every kind a stored form may hold: a union's members' kinds. */\nconst storedKinds = (ast: SchemaAST.AST): ReadonlySet<string> =>\n new Set(\n SchemaAST.isUnion(ast)\n ? ast.types.flatMap(member => [...storedKinds(member)])\n : [storedKind(ast)],\n )\n\n/** The stored kinds a scalar may come to, by what it is. */\nconst kindsOf = (expr: AnyExpr): ReadonlySet<string> => {\n switch (expr._tag) {\n case 'Field':\n return storedKinds(Schema.toEncoded(expr.schema).ast)\n case 'Next':\n return storedKinds(Schema.toEncoded(expr.field.schema).ast)\n case 'Input':\n return storedKinds(Schema.toEncoded(expr.schema).ast)\n case 'Literal':\n return new Set([\n expr.value === null ? 'null' : typeof expr.value === 'string' ? 'text' : typeof expr.value,\n ])\n case 'Call':\n return storedKinds(Schema.toEncoded(expr.operator.output).ast)\n // Refused in an assignment, below; a visitor's viewer is null.\n case 'Viewer':\n case 'Now':\n return new Set([...storedKinds(Schema.toEncoded(expr.schema).ast), 'null'])\n // Refused in an assignment, below; a path reaching no row reads null.\n case 'Path':\n case 'ViewerPath':\n return new Set([...storedKinds(Schema.toEncoded(expr.field.schema).ast), 'null'])\n }\n}\n\n/**\n * Whether `expr` may come to null: a nullable field or input, a null literal,\n * an operator answering one, or a strict operator given an argument that may\n * be null, which it answers with null.\n */\nconst mayBeNull = (expr: AnyExpr): boolean =>\n kindsOf(expr).has('null') ||\n (expr._tag === 'Call' &&\n expr.operator.nulls === 'strict' &&\n expr.args.some(arg => !isPredicate(arg) && mayBeNull(arg)))\n\n/** Whether every value a schema stores, null aside, is checked to be an integer. */\nconst storesIntegers = (schema: Schema.Top): boolean => {\n const present = SchemaShape.present(Schema.toEncoded(schema).ast)\n return present.length > 0 && present.every(SchemaShape.holdsIntegers)\n}\n\n/** Whether a scalar comes to an integer (or null) by what it is: its schema, or a literal's value. */\nconst comesToInteger = (expr: AnyExpr): boolean =>\n Match.value(expr).pipe(\n Match.tags({\n Field: ({ schema }) => storesIntegers(schema),\n Input: ({ schema }) => storesIntegers(schema),\n Literal: ({ value }) => value === null || Number.isInteger(value),\n Call: ({ operator }) => storesIntegers(operator.output),\n }),\n // What else an expression reads is refused in an assignment before this is asked.\n Match.orElse(() => false),\n )\n\n/** Whether a predicate is anywhere in `expr`, which data carries only as a value. */\nconst holdsPredicate = (expr: Operandish): boolean =>\n isPredicate(expr) || (expr._tag === 'Call' && expr.args.some(holdsPredicate))\n\n/**\n * One computed field, checked: a field of the Entity written by nothing else,\n * computed from this Entity's row before the write and the input alone\n * (never `Next`, so assignments do not depend on each other), to a value of\n * the field's stored kind, with no predicate in it.\n */\nconst assignmentOf = (\n input: EntityInput<AnyEntity, any, any>,\n fieldKey: string,\n compute: () => AnyExpr,\n declared: {\n readonly expect: WriteSet | undefined\n readonly sets: ReadonlyArray<WriteSet>\n readonly increments: ReadonlyArray<WriteIncrement>\n },\n): WriteAssignment => {\n const fields = input.entity.fields as Readonly<Record<string, AnyField>>\n const field = Object.hasOwn(fields, fieldKey) ? fields[fieldKey] : undefined\n if (field === undefined) return fail(input, `computes \"${fieldKey}\", which is not a field`)\n if (fieldKey === 'id') return fail(input, 'computes the id')\n if (declared.expect?.field.key === fieldKey) {\n return fail(input, `computes \"${fieldKey}\", the revision it expects`)\n }\n if (\n declared.sets.some(set => set.field.key === fieldKey) ||\n declared.increments.some(increment => increment.field.key === fieldKey)\n ) {\n return fail(input, `computes \"${fieldKey}\", which the write also sets`)\n }\n const operand: Operandish = compute()\n const expr = isPredicate(operand) ? operand : Expr.expanded(operand)\n if (isPredicate(expr) || holdsPredicate(expr)) {\n return fail(\n input,\n `computes \"${fieldKey}\" with a predicate, which an assignment carries only as a value`,\n )\n }\n const reads = dependenciesOf(expr)\n if (reads.next.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" from the row after the write, which an assignment never reads`,\n )\n }\n const context = reads.operations.filter(\n operation => operation === 'viewer' || operation === 'now',\n )\n if (context.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" from the write's context (${context.map(term => `Expr.${term}`).join(', ')}), which an assignment does not read; a value the context gives is declared with Entity.sets`,\n )\n }\n if (reads.reaches.length > 0) {\n return fail(\n input,\n `computes \"${fieldKey}\" through a relation, from another row, which an assignment does not read`,\n )\n }\n const stranger = reads.fields.find(read => read.owner.name !== input.entity.name)\n if (stranger !== undefined) {\n return fail(\n input,\n `computes \"${fieldKey}\" from ${stranger.owner.name}.${stranger.key}, another Entity's field`,\n )\n }\n const stored = storedKinds(Schema.toEncoded(field.schema as unknown as Schema.Top).ast)\n const comes = [...kindsOf(expr)].filter(kind => kind !== 'null' && !stored.has(kind))\n if (comes.length > 0) {\n return fail(input, `computes \"${fieldKey}\" as ${comes.join(' or ')}, which it does not store`)\n }\n // Memory would show the null, and the database refuse it as NOT NULL.\n if (!stored.has('null') && mayBeNull(expr)) {\n return fail(input, `computes \"${fieldKey}\" as possibly null, which it does not store`)\n }\n // As with an increment's amount: Postgres rounds a fraction written to an integer\n // column, SQLite stores it, and memory keeps it. So it never reaches either.\n const integer = comesToInteger(expr)\n if (storesIntegers(field.schema as unknown as Schema.Top) && !integer) {\n return fail(\n input,\n `computes \"${fieldKey}\" as a number that may not be an integer, but \"${fieldKey}\" is one; compute it with an operator, input or literal that is (Schema.Int)`,\n )\n }\n return Object.freeze({ field, expr, integer })\n}\n\n/** An assignment's expression, bound to one input, as data. */\nconst computationOf = (\n write: AnyWrite,\n expr: AnyExpr,\n input: Readonly<Record<string, unknown>>,\n): Computation => {\n switch (expr._tag) {\n // `Next` is refused where an assignment is declared.\n case 'Field':\n case 'Next':\n return Computation.Field({\n key: Match.valueTags(expr, { Field: ({ key }) => key, Next: ({ field }) => field.key }),\n })\n case 'Literal':\n return Computation.Value({ value: expr.value })\n case 'Input':\n return Computation.Value({\n value: Schema.encodeUnknownSync(\n write.input.schema.fields[expr.key] as unknown as Schema.Codec<unknown, unknown>,\n )(input[expr.key]),\n })\n case 'Call':\n return Computation.Call({\n operator:\n expr.operator.version === 1\n ? expr.operator.id\n : `${expr.operator.id}@${expr.operator.version}`,\n args: expr.args.map(arg => computationOf(write, arg as AnyExpr, input)),\n collation: Option.getOrNull(expr.collation),\n })\n // Refused where an assignment is declared, so none reaches here.\n case 'Viewer':\n case 'Now':\n throw new Error(\n `[foldkit-entity] an assignment of ${write.input.entity.name} reads the write's context`,\n )\n case 'Path':\n case 'ViewerPath':\n throw new Error(\n `[foldkit-entity] an assignment of ${write.input.entity.name} reads another row`,\n )\n }\n}\n\n/**\n * What a computation comes to over `row`, the row before the write: none when\n * the row lacks a field it reads, or `operators` lacks an operator it calls.\n */\nconst computed = (\n computation: Computation,\n row: Readonly<Record<string, unknown>>,\n operators: MemoryOperators,\n): Option.Option<unknown> =>\n Computation.$match(computation, {\n Field: ({ key }) => (Object.hasOwn(row, key) ? Option.some(row[key]) : Option.none()),\n Value: ({ value }) => Option.some(value),\n Call: ({ operator, args, collation }) =>\n Option.flatMap(operators.named(operator), descriptor =>\n Option.map(Option.all(args.map(arg => computed(arg, row, operators))), values =>\n applyOperator(descriptor, values, {\n collation: Option.fromNullOr(collation),\n operators,\n }),\n ),\n ),\n })\n\n/** Whether a value's stored form is a plain number. */\nconst storedAsNumber = (schema: Schema.Top): boolean =>\n SchemaAST.isNumber(Schema.toEncoded(schema).ast)\n\n/** Whether a value's stored form is checked to be an integer. */\nconst storedAsInteger = (schema: Schema.Top): boolean =>\n SchemaShape.holdsIntegers(Schema.toEncoded(schema).ast)\n\n/**\n * One increment, checked: a field of the Entity stored as a number, written\n * by nothing else, moved by an unmapped input key whose value is a number, and\n * an integer when the field is one.\n */\nconst incrementOf = (\n input: EntityInput<AnyEntity, any, any>,\n fieldKey: string,\n key: string,\n declared: { readonly expect: WriteSet | undefined; readonly sets: ReadonlyArray<WriteSet> },\n): WriteIncrement => {\n const fields = input.entity.fields as Readonly<Record<string, AnyField>>\n const field = Object.hasOwn(fields, fieldKey) ? fields[fieldKey] : undefined\n if (field === undefined) return fail(input, `increments \"${fieldKey}\", which is not a field`)\n if (fieldKey === 'id') return fail(input, 'increments the id')\n if (declared.expect?.field.key === fieldKey) {\n return fail(input, `increments \"${fieldKey}\", the revision it expects`)\n }\n if (declared.sets.some(set => set.field.key === fieldKey)) {\n return fail(input, `increments \"${fieldKey}\", which a key also sets`)\n }\n if (!storedAsNumber(field.schema as unknown as Schema.Top)) {\n return fail(input, `increments \"${fieldKey}\", which is not stored as a number`)\n }\n const member = own(membersOf(input), key)\n if (member === undefined) return fail(input, `\"${key}\" is not a key of the input`)\n if (member._tag !== 'Unmapped') {\n return fail(input, `\"${key}\" is an amount, so it must be unmapped, not written as a member`)\n }\n const amount = input.schema.fields[key] as unknown as Schema.Top\n if (!storedAsNumber(amount)) return fail(input, `\"${key}\" is an amount, but not a number`)\n // The databases split over a fraction added to an integer: Postgres refuses the\n // statement, SQLite stores it in the integer column. So it never reaches either.\n const integer = storedAsInteger(amount)\n if (storedAsInteger(field.schema as unknown as Schema.Top) && !integer) {\n return fail(\n input,\n `\"${key}\" may not be an integer, but \"${fieldKey}\" is one; give the amount Schema.Int`,\n )\n }\n return Object.freeze({ key, field, integer })\n}\n\n/** An update or an insert that would write nothing is refused where it is declared. */\nconst settingSomething = <\n E extends AnyEntity,\n Fields extends Schema.Struct.Fields,\n Kind extends WriteKind,\n>(\n write: Write<E, Fields, Kind>,\n): Write<E, Fields, Kind> =>\n write.sets.length === 0 &&\n write.links.length === 0 &&\n write.increments.length === 0 &&\n write.assignments.length === 0 &&\n write.context.length === 0\n ? fail(write.input, 'it sets no field')\n : write\n\nexport const Write = {\n /**\n * Sets, on the row whose id is the input's `id` key, every field the input\n * maps a key to. `expect` names the key holding the revision the row was\n * read at: an interpreter writes only a row still at that revision.\n *\n * ```ts\n * const EditPost = Write.update(EditPostInput, { id: 'id' })\n * ```\n *\n * A key mapped to a `one` relation (`Relation.input`) points the row at the\n * id it holds. Refused where it is written: an `id` that is not the Entity's\n * `id` field, a `many` relation or a nested key (not written by an update\n * yet), or nothing to set.\n *\n * `when` is what the row must hold for the update to land, checked where it\n * commits (`({ input }) => Expr.eq(Post.fields.status, 'draft')`); a row it\n * does not hold of is refused as unmet.\n *\n * `compute` sets a field to what an expression comes to over the row before\n * the write and the input, run where it commits\n * (`{ label: ({ row }) => concat(row.title, ' draft') }`). It is refused for\n * a field another key writes, an expression over the row after the write or\n * another Entity, or one of a kind the field does not store.\n */\n update,\n\n /**\n * Inserts the row whose id is the input's `id` key, an id the client chose,\n * with every field and `one` relation the input maps a key to. An insert\n * expects no revision: there is no row to have read. A row already there\n * under that id (a retry) is left as it is.\n *\n * What its Entity sets from the write's context (`Entity.sets`: who asks,\n * when) it writes too, never from the input: a server fills them, a client\n * predicts them. An update writes those set `on: 'write'`.\n */\n insert: <E extends AnyEntity, Fields extends Schema.Struct.Fields>(\n input: EntityInput<E, Fields, any>,\n options: { readonly id: keyof Fields & string },\n ): Write<E, Fields, 'Insert'> =>\n settingSomething(\n Object.freeze({\n ...declared('Insert', input, options),\n context: contextOf(input.entity, 'create'),\n }),\n ),\n\n /**\n * Deletes the row whose id is the input's `id` key, only while it is still at\n * the revision `expect` names, when it names one. It sets nothing, so a key\n * mapped to a member is refused where it is written. A row already gone (a\n * retry) is gone. `when`, as an update's, is what the row must hold.\n */\n delete: remove,\n\n /**\n * The revision this input says the row was read at, encoded, with its field:\n * what an interpreter writes only a row still at. None for a write that\n * expects none.\n */\n expected: <Fields extends Schema.Struct.Fields>(\n write: Write<AnyEntity, Fields>,\n value: Schema.Struct.Type<Fields>,\n ): Option.Option<{ readonly field: string; readonly revision: unknown }> =>\n Option.map(Option.fromUndefinedOr(write.expect), ({ key, field }) => ({\n field: field.key,\n revision: encoded(field, (value as Readonly<Record<string, unknown>>)[key]),\n })),\n\n /** The members any invocation may write: what a query reading none of them cannot be changed by. */\n writes: (write: AnyWrite): ReadonlyArray<AnyField | OneRelation> => [\n ...write.sets.map(set => set.field),\n ...write.increments.map(increment => increment.field),\n ...write.assignments.map(assignment => assignment.field),\n ...write.links.map(link => link.relation),\n ...write.context.map(set => set.member),\n ],\n\n /**\n * What the context sets come to, encoded as the store holds them: each\n * field's value and each relation's link. A path from the viewer is read\n * through `read`, one row at a time. `unfilled` names each member there is\n * no value for: none to read (no reading of the time, a row or field\n * `read` does not hold), or a null where the member holds none (a visitor,\n * a viewer of another Entity, a path reaching no row). A server refuses such\n * a write; a client predicts nothing for the member.\n */\n contextValues: <E, R>(\n write: AnyWrite,\n by: ExprContext,\n read: ContextRead<E, R>,\n ): Effect.Effect<ContextValues, E, R> =>\n Effect.gen(function* () {\n const values: Record<string, unknown> = {}\n const links: Record<string, Option.Option<EntityRef>> = {}\n const unfilled: Array<string> = []\n for (const { member, value } of write.context) {\n const comes: Option.Option<unknown> = yield* Match.value(value).pipe(\n Match.tag('ViewerPath', path =>\n Effect.map(walked(path, by, read), held =>\n Held.match(held, {\n onHeld: some => Option.some(some),\n onUnheld: () => Option.none(),\n }),\n ),\n ),\n Match.orElse(other => Effect.succeed(contextValue(other, by))),\n )\n if (Option.isNone(comes)) {\n unfilled.push(member.key)\n } else {\n const given = comes.value\n Match.valueTags(member, {\n // `Entity.sets` points a relation only at the viewer of its target.\n Relation: relation => {\n if (given !== null)\n put(\n links,\n relation.key,\n Option.some({ entity: relation.target().name, id: String(given) }),\n )\n else if (relation.optional) put(links, relation.key, Option.none())\n else unfilled.push(relation.key)\n },\n Field: field => {\n if (given !== null || holdsNull(field)) put(values, field.key, given)\n else unfilled.push(field.key)\n },\n })\n }\n }\n return { values, links, unfilled }\n }),\n\n /** The Entities a write's paths from the viewer read, by name: what a server must have a source of. */\n contextReads: (write: AnyWrite): ReadonlyArray<string> => [\n ...new Set(\n write.context.flatMap(({ value }) => {\n if (value._tag !== 'ViewerPath') return []\n const { viewer, via } = value\n return [viewer.entity.name, ...via.map(relation => relation.target().name)]\n }),\n ),\n ],\n\n /**\n * The fields a write reads from the row it writes: those its `when` reads,\n * each it increments, each its assignments read, and the revision it\n * expects. What an interpreter must read, and what a startup check judges\n * it could disclose.\n */\n reads: (write: AnyWrite): ReadonlyArray<AnyField> => {\n const fields = new Map<string, AnyField>()\n const add = (field: AnyField) => fields.set(field.key, field)\n Option.map(write.when, body =>\n Query.dependencies(body).predicate.forEach(field => add(field as unknown as AnyField)),\n )\n write.increments.forEach(increment => add(increment.field))\n write.assignments.forEach(assignment =>\n dependenciesOf(assignment.expr).fields.forEach(field => add(field as unknown as AnyField)),\n )\n if (write.expect !== undefined) add(write.expect.field)\n return [...fields.values()]\n },\n\n /**\n * The row this input writes and its new values, encoded as the store holds\n * them: the patch a client shows before the server answers. `keys`, when\n * given, narrows it to those input keys (what an author changed); the rest of\n * the input is still the whole, validated value.\n */\n bind: <Fields extends Schema.Struct.Fields>(\n write: Write<AnyEntity, Fields>,\n value: Schema.Struct.Type<Fields>,\n keys?: ReadonlyArray<string>,\n ): BoundWrite => {\n const input = value as Readonly<Record<string, unknown>>\n const wanted = keys === undefined ? undefined : new Set(keys)\n const asked = (key: string) => wanted === undefined || wanted.has(key)\n const values: Record<string, unknown> = {}\n for (const { key, field } of write.sets) {\n if (!asked(key)) continue\n // A key the input leaves out, or an `Option` holding nothing, writes nothing.\n Option.map(given(own(input, key)), held => put(values, field.key, encoded(field, held)))\n }\n const links: Record<string, Option.Option<EntityRef>> = {}\n for (const { key, relation } of write.links) {\n if (!asked(key)) continue\n const target = relation.target()\n put(\n links,\n relation.key,\n Option.map(Option.flatMap(given(own(input, key)), Option.fromNullishOr), id => ({\n entity: target.name,\n id: String(encoded(target.fields.id as AnyField, id)),\n })),\n )\n }\n const increments: Record<string, number> = {}\n for (const { key, field } of write.increments) {\n // `keys` names input keys, so an increment is asked for by its amount's key.\n if (!asked(key)) continue\n const schema = write.input.schema.fields[key] as unknown as Schema.Codec<unknown, number>\n Option.map(given(own(input, key)), held =>\n put(increments, field.key, Schema.encodeUnknownSync(schema)(held)),\n )\n }\n // Every assignment: it reads the row as well as the input, so no key alone asks for it.\n const assignments: Record<string, Computation> = {}\n for (const { field, expr } of write.assignments) {\n put(assignments, field.key, computationOf(write, expr, input))\n }\n return {\n entity: write.input.entity.name,\n id: String(encoded(write.id.field, input[write.id.key])),\n values,\n links,\n increments,\n assignments,\n }\n },\n\n /**\n * The fields of the row before it that a bound write's amounts and\n * assignments read: what to read to work out the row it leaves.\n */\n boundReads: (bound: Pick<BoundWrite, 'increments' | 'assignments'>): ReadonlyArray<string> => {\n const keys = new Set(Object.keys(bound.increments))\n const walk = (computation: Computation): void =>\n Computation.$match(computation, {\n Field: ({ key }) => void keys.add(key),\n Value: () => undefined,\n Call: ({ args }) => args.forEach(walk),\n })\n Object.values(bound.assignments).forEach(walk)\n return [...keys]\n },\n\n /**\n * The row a bound write leaves over `row`, the row before it: its values\n * set, each amount added (a null stays null, as in SQL), and each\n * assignment computed by `operators` over the row before. `unknown` names\n * each field it could not work out: one `row` lacks, or holds as no number\n * for an amount, or an assignment calling an operator the table lacks. That\n * field keeps what `row` holds. The one computation of this row: a guard\n * judges it, a writer explains a miss with it, a client predicts it.\n */\n apply: (\n row: Readonly<Record<string, unknown>>,\n bound: Pick<BoundWrite, 'values' | 'increments' | 'assignments'>,\n options: { readonly operators: MemoryOperators },\n ): {\n readonly row: Readonly<Record<string, unknown>>\n readonly unknown: ReadonlyArray<string>\n } => {\n const after: Record<string, unknown> = { ...row, ...bound.values }\n const unknown: Array<string> = []\n for (const [field, amount] of Object.entries(bound.increments)) {\n const held = Object.hasOwn(row, field) ? row[field] : undefined\n if (held === null) after[field] = null\n else if (typeof held === 'number') after[field] = held + amount\n else unknown.push(field)\n }\n for (const [field, computation] of Object.entries(bound.assignments)) {\n Option.match(computed(computation, row, options.operators), {\n onNone: () => unknown.push(field),\n onSome: value => put(after, field, value),\n })\n }\n return { row: after, unknown }\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiGA,MAAa,cAAc,KAAK,WAAwB;AA2IxD,MAAM,UACJ,OACA,YAEA,iBACE,OAAO,OAAO;CACZ,GAAG,SAAS,UAAU,OAAO,OAAO;CACpC,SAAS,UAAU,MAAM,QAAQ,OAAO;AAC1C,CAAC,CACH;AAEF,MAAM,UAAuB,OAAyC,YAA6B;CACjG,MAAM,QAAQ,SAAS,UAAU,OAAO,OAAO;CAC/C,MAAM,QAAQ,CAAC,GAAG,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,CAAC;CAC9C,IAAI,UAAU,KAAA,GAAW,KAAK,OAAO,+BAA+B,MAAM,IAAI,YAAY;CAC1F,OAAO;AACT;AA8BA,MAAM,QAAQ,OAAyC,YAA2B;CAChF,MAAM,IAAI,MAAM,6BAA6B,MAAM,OAAO,KAAK,IAAI,SAAS;AAC9E;AAEA,MAAM,aACJ,UACsD,MAAM;;AAG9D,MAAM,WAAW,OAAiB,UAChC,OAAO,kBAAkB,MAAM,MAAmD,CAAC,CAAC,KAAK;;;;;;AAO3F,MA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