foldkit-entity 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +139 -5
- package/dist/collation.d.ts +33 -2
- package/dist/collation.d.ts.map +1 -0
- package/dist/conformance/index.d.ts +4 -4
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +5 -7
- package/dist/conformance/index.js.map +1 -1
- package/dist/conformance/operator.d.ts +1 -2
- package/dist/conformance/operator.d.ts.map +1 -1
- package/dist/conformance/operator.js +2 -6
- package/dist/conformance/operator.js.map +1 -1
- package/dist/conformance/relational.d.ts +17 -17
- package/dist/conformance/relational.d.ts.map +1 -1
- package/dist/conformance/relational.js +1 -3
- package/dist/conformance/relational.js.map +1 -1
- package/dist/conformance/variants.d.ts +47 -0
- package/dist/conformance/variants.d.ts.map +1 -0
- package/dist/conformance/variants.js +126 -0
- package/dist/conformance/variants.js.map +1 -0
- package/dist/context.d.ts +60 -2
- package/dist/context.d.ts.map +1 -0
- package/dist/descriptor.d.ts +33 -0
- package/dist/descriptor.d.ts.map +1 -0
- package/dist/descriptor.js +137 -0
- package/dist/descriptor.js.map +1 -0
- package/dist/entity.d.ts +231 -0
- package/dist/entity.d.ts.map +1 -0
- package/dist/entity.js +435 -0
- package/dist/entity.js.map +1 -0
- package/dist/evaluate.d.ts +204 -2
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +51 -7
- package/dist/evaluate.js.map +1 -1
- package/dist/expr.d.ts +915 -2
- package/dist/expr.d.ts.map +1 -0
- package/dist/expr.js +50 -10
- package/dist/expr.js.map +1 -1
- package/dist/held.d.ts +112 -2
- package/dist/held.d.ts.map +1 -0
- package/dist/held.js +13 -2
- package/dist/held.js.map +1 -1
- package/dist/ids.d.ts +8 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +8 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +380 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -12
- package/dist/index.js.map +1 -0
- package/dist/invariant.d.ts +123 -2
- package/dist/invariant.d.ts.map +1 -0
- package/dist/invariant.js +236 -1
- package/dist/invariant.js.map +1 -0
- package/dist/law.d.ts +264 -2
- package/dist/law.d.ts.map +1 -0
- package/dist/law.js +1 -1
- package/dist/operators.d.ts +170 -2
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +3 -3
- package/dist/readFilter.d.ts +8 -2
- package/dist/readFilter.d.ts.map +1 -0
- package/dist/sets.d.ts +18 -2
- package/dist/sets.d.ts.map +1 -0
- package/dist/sets.js +1 -1
- package/dist/shape.d.ts +23 -2
- package/dist/shape.d.ts.map +1 -0
- package/dist/tested.d.ts +8 -2
- package/dist/tested.d.ts.map +1 -0
- package/dist/text.d.ts +27 -2
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +1 -1
- package/dist/variants.d.ts +72 -0
- package/dist/variants.d.ts.map +1 -0
- package/dist/variants.js +76 -0
- package/dist/variants.js.map +1 -0
- package/dist/words.d.ts +17 -2
- package/dist/words.d.ts.map +1 -0
- package/dist/write.d.ts +312 -2
- package/dist/write.d.ts.map +1 -0
- package/dist/write.js +3 -3
- package/package.json +3 -3
- package/dist/collation-BB4YTIbY.d.ts +0 -34
- package/dist/collation-BB4YTIbY.d.ts.map +0 -1
- package/dist/index-48JeXG0A.d.ts +0 -2585
- package/dist/index-48JeXG0A.d.ts.map +0 -1
- package/dist/readFilter-CWMnFRVw.d.ts +0 -9
- package/dist/readFilter-CWMnFRVw.d.ts.map +0 -1
- package/dist/shape-LAowRf9f.d.ts +0 -23
- package/dist/shape-LAowRf9f.d.ts.map +0 -1
- package/dist/src-DKhUqLoC.js +0 -731
- package/dist/src-DKhUqLoC.js.map +0 -1
- package/dist/words-a4e9o-TK.d.ts +0 -18
- package/dist/words-a4e9o-TK.d.ts.map +0 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
/** What `Entity.defineVariants` takes: each case's fields, by its tag. */
|
|
3
|
+
export type VariantCases = Readonly<Record<string, Schema.Struct.Fields>>;
|
|
4
|
+
/** The cases as Effect's tagged union, with its own `match`, `guards`, `cases` and `isAnyOf`. */
|
|
5
|
+
export type VariantUnion<Cases extends VariantCases> = Schema.TaggedUnion<{ readonly [T in keyof Cases & string]: Schema.TaggedStruct<T, Cases[T]>; }>;
|
|
6
|
+
/**
|
|
7
|
+
* Any Entity's variants, as generic code holds them: a union keyed by `_tag`
|
|
8
|
+
* with its cases' fields. Its `match` is typed only on a union of known cases.
|
|
9
|
+
*/
|
|
10
|
+
export type AnyVariantUnion = Schema.Top & {
|
|
11
|
+
readonly tag: '_tag';
|
|
12
|
+
readonly cases: Readonly<Record<string, {
|
|
13
|
+
readonly fields: Schema.Struct.Fields;
|
|
14
|
+
}>>;
|
|
15
|
+
};
|
|
16
|
+
/** The keys every case has: `keyof` a union of records is the keys they share. */
|
|
17
|
+
type SharedKeys<Cases extends VariantCases> = keyof Cases[keyof Cases] & string;
|
|
18
|
+
type AllKeys<Cases extends VariantCases> = Cases[keyof Cases] extends (infer Fields) ? Fields extends unknown ? keyof Fields & string : never : never;
|
|
19
|
+
/** The schema the cases holding `K` declare for it: one schema, or a union of those that differ. */
|
|
20
|
+
type SchemaAt<Cases extends VariantCases, K> = { [T in keyof Cases]: K extends keyof Cases[T] ? Cases[T][K] : never; }[keyof Cases];
|
|
21
|
+
/** A case's own field as the stored row holds it: null on a row of another case. */
|
|
22
|
+
type Absent<S> = S extends Schema.Top ? (null extends S['Encoded'] ? S : Schema.NullOr<S>) : never;
|
|
23
|
+
/**
|
|
24
|
+
* The stored row's fields: `_tag`, every shared field as declared, and every
|
|
25
|
+
* field only some cases have as nullable, since the row of another case holds
|
|
26
|
+
* null there.
|
|
27
|
+
*/
|
|
28
|
+
export type StoredFields<Cases extends VariantCases> = { readonly [K in AllKeys<Cases> | '_tag']: K extends '_tag' ? Schema.Literals<ReadonlyArray<keyof Cases & string>> : K extends SharedKeys<Cases> ? SchemaAt<Cases, K> : Absent<SchemaAt<Cases, K>>; };
|
|
29
|
+
type IsUnion<T, Whole = T> = T extends unknown ? ([Whole] extends [T] ? false : true) : never;
|
|
30
|
+
/**
|
|
31
|
+
* Unsatisfiable, naming the mistake, for cases that cannot be one stored row:
|
|
32
|
+
* fewer than two, a case declaring `_tag` itself, or one key declared with two
|
|
33
|
+
* different schemas (one column cannot hold both).
|
|
34
|
+
*/
|
|
35
|
+
export type CheckCases<Cases extends VariantCases> = true extends IsUnion<keyof Cases> ? '_tag' extends AllKeys<Cases> ? {
|
|
36
|
+
readonly '~invalid': 'foldkit-entity: a case cannot declare _tag; it is the case name';
|
|
37
|
+
} : { [K in AllKeys<Cases>]: true extends IsUnion<SchemaAt<Cases, K>> ? K : never; }[AllKeys<Cases>] extends (infer Clash) ? [Clash] extends [never] ? unknown : {
|
|
38
|
+
readonly '~invalid': `foldkit-entity: "${Clash & string}" is declared with different schemas; declare it once and spread it into each case`;
|
|
39
|
+
} : never : {
|
|
40
|
+
readonly '~invalid': 'foldkit-entity: variants need two cases or more; one shape is Entity.define';
|
|
41
|
+
};
|
|
42
|
+
/** Each case's fields, `_tag` aside, by tag: the case's own schemas, never the stored null. */
|
|
43
|
+
export type CaseSchemas = Readonly<Record<string, Schema.Struct.Fields>>;
|
|
44
|
+
/** What an interpreter reads of an Entity's variants (`Entity.variantsOf`). */
|
|
45
|
+
export interface EntityVariants {
|
|
46
|
+
/** Effect's tagged union of the cases: what `entity.variants` is. */
|
|
47
|
+
readonly union: AnyVariantUnion;
|
|
48
|
+
/** The case names, in the order declared. */
|
|
49
|
+
readonly tags: ReadonlyArray<string>;
|
|
50
|
+
readonly cases: CaseSchemas;
|
|
51
|
+
/**
|
|
52
|
+
* A stored row, encoded as the store holds it (every field, null where the
|
|
53
|
+
* row is of another case), decoded to its case; and back.
|
|
54
|
+
*/
|
|
55
|
+
readonly stored: Schema.Codec<unknown, unknown>;
|
|
56
|
+
}
|
|
57
|
+
/** The variants registered for an identity, or none for an Entity of one shape. */
|
|
58
|
+
export declare const variantsOf: (identity: object) => Option.Option<EntityVariants>;
|
|
59
|
+
/** Whether a field's stored form holds null already, so the stored row needs no second null. */
|
|
60
|
+
export declare const storesNull: (schema: Schema.Constraint) => boolean;
|
|
61
|
+
/**
|
|
62
|
+
* The stored row's fields, and the variants, for `cases`; refused where a
|
|
63
|
+
* stored row could not hold them.
|
|
64
|
+
*/
|
|
65
|
+
export declare const declareVariants: (name: string, cases: VariantCases) => {
|
|
66
|
+
readonly fields: Schema.Struct.Fields;
|
|
67
|
+
readonly variants: Omit<EntityVariants, 'stored'>;
|
|
68
|
+
};
|
|
69
|
+
/** Registers an identity's variants, with the codec between its stored row and its case. */
|
|
70
|
+
export declare const registerVariants: (identity: object, stored: Schema.Struct<Schema.Struct.Fields>, variants: Omit<EntityVariants, 'stored'>) => void;
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=variants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variants.d.ts","names":[],"sources":["../src/variants.ts"],"mappings":";;YAYY,eAAe,SAAS,eAAe,OAAO,OAAO;;YAGrD,aAAa,cAAc,gBAAgB,OAAO,wBAClD,WAAW,iBAAiB,OAAO,aAAa,GAAG,MAAM;;;;;YAOzD,kBAAkB,OAAO;WAC1B;WACA,OAAO,SAAS;aAA0B,QAAQ,OAAO,OAAO;;;;KAItE,WAAW,cAAc,sBAAsB,YAAY;KAE3D,QAAQ,cAAc,gBAAgB,YAAY,sBAAqB,UACxE,+BACQ;;KAKP,SAAS,cAAc,cAAc,QACvC,WAAW,QAAQ,gBAAgB,MAAM,KAAK,MAAM,GAAG,oBAClD;;KAGH,OAAO,KAAK,UAAU,OAAO,oBAAoB,eAAe,IAAI,OAAO,OAAO;;;;;;YAO3E,aAAa,cAAc,4BAC3B,KAAK,QAAQ,kBAAkB,mBACrC,OAAO,SAAS,oBAAoB,mBACpC,UAAU,WAAW,SACnB,SAAS,OAAO,KAChB,OAAO,SAAS,OAAO;KAG1B,QAAQ,GAAG,QAAQ,KAAK,sBAAsB,gBAAgB;;;;;;YAOvD,WAAW,cAAc,6BACtB,cAAc,wBACR,QAAQ;WACV;OAEN,KAAK,QAAQ,sBAAsB,QAAQ,SAAS,OAAO,MAAM,aAClE,QAAQ,uBAAsB,UAC/B;WAGY,gCAAgC;;WAItC;;;YAIL,cAAc,SAAS,eAAe,OAAO,OAAO;;iBAG/C;;WAEN,OAAO;;WAEP,MAAM;WACN,OAAO;;;;;WAKP,QAAQ,OAAO;;;qBAMb,aAAU,qBAAuB,OAAO,OAAO;;qBAU/C,aAAU,QAAY,OAAO;;;;;qBAO7B,kBAAe,cACd,OACL;WACK,QAAQ,OAAO,OAAO;WAAiB,UAAU,KAAK;;;qBA+CvD,mBAAgB,kBACX,QACR,OAAO,OAAO,OAAO,OAAO,SAAO,UACjC,KAAK"}
|
package/dist/variants.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Option, Schema, SchemaAST, SchemaTransformation } from "effect";
|
|
2
|
+
import { put } from "./record.js";
|
|
3
|
+
// src/variants.ts
|
|
4
|
+
/**
|
|
5
|
+
* Variants of an Entity (#357): rows that are one of several shapes, told
|
|
6
|
+
* apart by `_tag`. The row is Effect's tagged union of the cases; the stored
|
|
7
|
+
* row is one record holding every field of every case, null where the row is
|
|
8
|
+
* of another case. Every interpreter that does not read variants sees that
|
|
9
|
+
* stored row, an Entity with a `_tag` field and nullable fields, and keeps
|
|
10
|
+
* working; the ones that do read the union through its own `match`.
|
|
11
|
+
*/
|
|
12
|
+
const registry = /* @__PURE__ */ new WeakMap();
|
|
13
|
+
/** The variants registered for an identity, or none for an Entity of one shape. */
|
|
14
|
+
const variantsOf = (identity) => Option.fromUndefinedOr(registry.get(identity));
|
|
15
|
+
const acceptsNull = (ast) => SchemaAST.isNull(ast) || SchemaAST.isUnknown(ast) || SchemaAST.isAny(ast) || SchemaAST.isUnion(ast) && ast.types.some(acceptsNull);
|
|
16
|
+
/** Whether a field's stored form holds null already, so the stored row needs no second null. */
|
|
17
|
+
const storesNull = (schema) => acceptsNull(SchemaAST.toEncoded(schema.ast));
|
|
18
|
+
/**
|
|
19
|
+
* The stored row's fields, and the variants, for `cases`; refused where a
|
|
20
|
+
* stored row could not hold them.
|
|
21
|
+
*/
|
|
22
|
+
const declareVariants = (name, cases) => {
|
|
23
|
+
const fail = (message) => {
|
|
24
|
+
throw new Error(`Entity "${name}": ${message}`);
|
|
25
|
+
};
|
|
26
|
+
const tags = Object.keys(cases);
|
|
27
|
+
if (tags.length < 2) fail("variants need two cases or more; one shape is Entity.define");
|
|
28
|
+
const declared = /* @__PURE__ */ new Map();
|
|
29
|
+
for (const tag of tags) for (const [key, schema] of Object.entries(cases[tag])) {
|
|
30
|
+
if (key === "_tag") fail(`case "${tag}" declares _tag; it is the case name`);
|
|
31
|
+
if (SchemaAST.isOptional(schema.ast)) fail(`"${key}" of case "${tag}" is an optional key; a stored row holds every field`);
|
|
32
|
+
const earlier = declared.get(key);
|
|
33
|
+
if (earlier === void 0) declared.set(key, {
|
|
34
|
+
schema,
|
|
35
|
+
holders: /* @__PURE__ */ new Set([tag])
|
|
36
|
+
});
|
|
37
|
+
else if (earlier.schema !== schema) fail(`"${key}" is declared with different schemas; declare it once and spread it into each case`);
|
|
38
|
+
else earlier.holders.add(tag);
|
|
39
|
+
}
|
|
40
|
+
const fields = {};
|
|
41
|
+
put(fields, "_tag", Schema.Literals(tags));
|
|
42
|
+
for (const [key, { schema, holders }] of declared) put(fields, key, holders.size === tags.length || storesNull(schema) ? schema : Schema.NullOr(schema));
|
|
43
|
+
const caseSchemas = {};
|
|
44
|
+
for (const tag of tags) put(caseSchemas, tag, Object.freeze({ ...cases[tag] }));
|
|
45
|
+
return {
|
|
46
|
+
fields,
|
|
47
|
+
variants: {
|
|
48
|
+
union: Schema.TaggedUnion(cases),
|
|
49
|
+
tags: Object.freeze(tags),
|
|
50
|
+
cases: Object.freeze(caseSchemas)
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/** Registers an identity's variants, with the codec between its stored row and its case. */
|
|
55
|
+
const registerVariants = (identity, stored, variants) => {
|
|
56
|
+
const keys = Object.keys(stored.fields);
|
|
57
|
+
const keysOf = new Map(variants.tags.map((tag) => [tag, ["_tag", ...Object.keys(variants.cases[tag])]]));
|
|
58
|
+
const ownOf = new Map([...keysOf].map(([tag, keys]) => [tag, new Set(keys)]));
|
|
59
|
+
const caseKeys = (tag) => keysOf.get(tag);
|
|
60
|
+
const foreignNull = Schema.makeFilter((row) => {
|
|
61
|
+
const own = ownOf.get(row._tag);
|
|
62
|
+
const held = keys.filter((key) => !own.has(key) && row[key] !== null);
|
|
63
|
+
return held.length === 0 ? void 0 : `a row that is ${String(row._tag)} holds ${held.join(", ")}, which only another case has`;
|
|
64
|
+
});
|
|
65
|
+
const codec = Schema.toEncoded(stored).check(foreignNull).pipe(Schema.decodeTo(variants.union, SchemaTransformation.transform({
|
|
66
|
+
decode: (row) => Object.fromEntries(caseKeys(row._tag).map((key) => [key, row[key]])),
|
|
67
|
+
encode: (row) => Object.fromEntries(keys.map((key) => [key, Object.hasOwn(row, key) ? row[key] : null]))
|
|
68
|
+
})));
|
|
69
|
+
registry.set(identity, Object.freeze({
|
|
70
|
+
...variants,
|
|
71
|
+
stored: codec
|
|
72
|
+
}));
|
|
73
|
+
};
|
|
74
|
+
export { declareVariants, registerVariants, storesNull, variantsOf };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=variants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variants.js","names":[],"sources":["../src/variants.ts"],"sourcesContent":["/**\n * Variants of an Entity (#357): rows that are one of several shapes, told\n * apart by `_tag`. The row is Effect's tagged union of the cases; the stored\n * row is one record holding every field of every case, null where the row is\n * of another case. Every interpreter that does not read variants sees that\n * stored row, an Entity with a `_tag` field and nullable fields, and keeps\n * working; the ones that do read the union through its own `match`.\n */\nimport { Option, Schema, SchemaAST, SchemaTransformation } from 'effect'\nimport { put } from './record.js'\n\n/** What `Entity.defineVariants` takes: each case's fields, by its tag. */\nexport type VariantCases = Readonly<Record<string, Schema.Struct.Fields>>\n\n/** The cases as Effect's tagged union, with its own `match`, `guards`, `cases` and `isAnyOf`. */\nexport type VariantUnion<Cases extends VariantCases> = Schema.TaggedUnion<{\n readonly [T in keyof Cases & string]: Schema.TaggedStruct<T, Cases[T]>\n}>\n\n/**\n * Any Entity's variants, as generic code holds them: a union keyed by `_tag`\n * with its cases' fields. Its `match` is typed only on a union of known cases.\n */\nexport type AnyVariantUnion = Schema.Top & {\n readonly tag: '_tag'\n readonly cases: Readonly<Record<string, { readonly fields: Schema.Struct.Fields }>>\n}\n\n/** The keys every case has: `keyof` a union of records is the keys they share. */\ntype SharedKeys<Cases extends VariantCases> = keyof Cases[keyof Cases] & string\n\ntype AllKeys<Cases extends VariantCases> = Cases[keyof Cases] extends infer Fields\n ? Fields extends unknown\n ? keyof Fields & string\n : never\n : never\n\n/** The schema the cases holding `K` declare for it: one schema, or a union of those that differ. */\ntype SchemaAt<Cases extends VariantCases, K> = {\n [T in keyof Cases]: K extends keyof Cases[T] ? Cases[T][K] : never\n}[keyof Cases]\n\n/** A case's own field as the stored row holds it: null on a row of another case. */\ntype Absent<S> = S extends Schema.Top ? (null extends S['Encoded'] ? S : Schema.NullOr<S>) : never\n\n/**\n * The stored row's fields: `_tag`, every shared field as declared, and every\n * field only some cases have as nullable, since the row of another case holds\n * null there.\n */\nexport type StoredFields<Cases extends VariantCases> = {\n readonly [K in AllKeys<Cases> | '_tag']: K extends '_tag'\n ? Schema.Literals<ReadonlyArray<keyof Cases & string>>\n : K extends SharedKeys<Cases>\n ? SchemaAt<Cases, K>\n : Absent<SchemaAt<Cases, K>>\n}\n\ntype IsUnion<T, Whole = T> = T extends unknown ? ([Whole] extends [T] ? false : true) : never\n\n/**\n * Unsatisfiable, naming the mistake, for cases that cannot be one stored row:\n * fewer than two, a case declaring `_tag` itself, or one key declared with two\n * different schemas (one column cannot hold both).\n */\nexport type CheckCases<Cases extends VariantCases> =\n true extends IsUnion<keyof Cases>\n ? '_tag' extends AllKeys<Cases>\n ? { readonly '~invalid': 'foldkit-entity: a case cannot declare _tag; it is the case name' }\n : {\n [K in AllKeys<Cases>]: true extends IsUnion<SchemaAt<Cases, K>> ? K : never\n }[AllKeys<Cases>] extends infer Clash\n ? [Clash] extends [never]\n ? unknown\n : {\n readonly '~invalid': `foldkit-entity: \"${Clash & string}\" is declared with different schemas; declare it once and spread it into each case`\n }\n : never\n : {\n readonly '~invalid': 'foldkit-entity: variants need two cases or more; one shape is Entity.define'\n }\n\n/** Each case's fields, `_tag` aside, by tag: the case's own schemas, never the stored null. */\nexport type CaseSchemas = Readonly<Record<string, Schema.Struct.Fields>>\n\n/** What an interpreter reads of an Entity's variants (`Entity.variantsOf`). */\nexport interface EntityVariants {\n /** Effect's tagged union of the cases: what `entity.variants` is. */\n readonly union: AnyVariantUnion\n /** The case names, in the order declared. */\n readonly tags: ReadonlyArray<string>\n readonly cases: CaseSchemas\n /**\n * A stored row, encoded as the store holds it (every field, null where the\n * row is of another case), decoded to its case; and back.\n */\n readonly stored: Schema.Codec<unknown, unknown>\n}\n\nconst registry = new WeakMap<object, EntityVariants>()\n\n/** The variants registered for an identity, or none for an Entity of one shape. */\nexport const variantsOf = (identity: object): Option.Option<EntityVariants> =>\n Option.fromUndefinedOr(registry.get(identity))\n\nconst acceptsNull = (ast: SchemaAST.AST): boolean =>\n SchemaAST.isNull(ast) ||\n SchemaAST.isUnknown(ast) ||\n SchemaAST.isAny(ast) ||\n (SchemaAST.isUnion(ast) && ast.types.some(acceptsNull))\n\n/** Whether a field's stored form holds null already, so the stored row needs no second null. */\nexport const storesNull = (schema: Schema.Constraint): boolean =>\n acceptsNull(SchemaAST.toEncoded(schema.ast))\n\n/**\n * The stored row's fields, and the variants, for `cases`; refused where a\n * stored row could not hold them.\n */\nexport const declareVariants = (\n name: string,\n cases: VariantCases,\n): { readonly fields: Schema.Struct.Fields; readonly variants: Omit<EntityVariants, 'stored'> } => {\n const fail = (message: string): never => {\n throw new Error(`Entity \"${name}\": ${message}`)\n }\n const tags = Object.keys(cases)\n if (tags.length < 2) fail('variants need two cases or more; one shape is Entity.define')\n const declared = new Map<\n string,\n { readonly schema: Schema.Constraint; readonly holders: Set<string> }\n >()\n for (const tag of tags) {\n for (const [key, schema] of Object.entries(cases[tag]!)) {\n if (key === '_tag') fail(`case \"${tag}\" declares _tag; it is the case name`)\n if (SchemaAST.isOptional(schema.ast))\n fail(`\"${key}\" of case \"${tag}\" is an optional key; a stored row holds every field`)\n const earlier = declared.get(key)\n if (earlier === undefined) declared.set(key, { schema, holders: new Set([tag]) })\n else if (earlier.schema !== schema)\n fail(\n `\"${key}\" is declared with different schemas; declare it once and spread it into each case`,\n )\n else earlier.holders.add(tag)\n }\n }\n const fields: Record<string, Schema.Constraint> = {}\n put(fields, '_tag', Schema.Literals(tags))\n for (const [key, { schema, holders }] of declared)\n put(\n fields,\n key,\n holders.size === tags.length || storesNull(schema)\n ? schema\n : Schema.NullOr(schema as Schema.Top),\n )\n const caseSchemas: Record<string, Schema.Struct.Fields> = {}\n for (const tag of tags) put(caseSchemas, tag, Object.freeze({ ...cases[tag] }))\n return {\n fields,\n variants: {\n union: Schema.TaggedUnion(cases) as unknown as AnyVariantUnion,\n tags: Object.freeze(tags),\n cases: Object.freeze(caseSchemas),\n },\n }\n}\n\n/** Registers an identity's variants, with the codec between its stored row and its case. */\nexport const registerVariants = (\n identity: object,\n stored: Schema.Struct<Schema.Struct.Fields>,\n variants: Omit<EntityVariants, 'stored'>,\n): void => {\n const keys = Object.keys(stored.fields)\n const keysOf = new Map(\n variants.tags.map(tag => [tag, ['_tag', ...Object.keys(variants.cases[tag]!)]] as const),\n )\n const ownOf = new Map([...keysOf].map(([tag, keys]) => [tag, new Set(keys)] as const))\n // The stored row's own `_tag` check runs first, so the tag is one of the cases.\n const caseKeys = (tag: unknown): ReadonlyArray<string> => keysOf.get(tag as string)!\n type Encoded = Readonly<Record<string, unknown>>\n // A field of another case holding a value is a row no case describes: refused, never dropped.\n const foreignNull = Schema.makeFilter<Encoded>(row => {\n const own = ownOf.get(row._tag as string)!\n const held = keys.filter(key => !own.has(key) && row[key] !== null)\n return held.length === 0\n ? undefined\n : `a row that is ${String(row._tag)} holds ${held.join(', ')}, which only another case has`\n })\n const codec = (Schema.toEncoded(stored) as unknown as Schema.Codec<Encoded>)\n .check(foreignNull)\n .pipe(\n Schema.decodeTo(\n variants.union as unknown as Schema.Codec<unknown, Encoded>,\n SchemaTransformation.transform<Encoded, Encoded>({\n decode: row => Object.fromEntries(caseKeys(row._tag).map(key => [key, row[key]])),\n encode: row =>\n Object.fromEntries(keys.map(key => [key, Object.hasOwn(row, key) ? row[key] : null])),\n }),\n ),\n )\n registry.set(\n identity,\n Object.freeze({ ...variants, stored: codec as Schema.Codec<unknown, unknown> }),\n )\n}\n"],"mappings":"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"}
|
package/dist/words.d.ts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
/** What a schema says of itself to a person: its `title` and `description`. */
|
|
3
|
+
export interface SchemaWords {
|
|
4
|
+
readonly title: Option.Option<string>;
|
|
5
|
+
readonly description: Option.Option<string>;
|
|
6
|
+
}
|
|
7
|
+
export declare const Words: {
|
|
8
|
+
/**
|
|
9
|
+
* A schema's words, as a label and its help text. Under Effect 4 a checked
|
|
10
|
+
* schema resolves to its last check's annotations, which say what the check
|
|
11
|
+
* expects and carry no `title`, so a title given before the check is read from
|
|
12
|
+
* the schema's own annotations. Read words through this, never through
|
|
13
|
+
* `Schema.resolveAnnotations` directly.
|
|
14
|
+
*/
|
|
15
|
+
of: (schema: Schema.Top) => SchemaWords;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=words.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"words.d.ts","names":[],"sources":["../src/words.ts"],"mappings":";;iBAGiB;WACN,OAAO,OAAO;WACd,aAAa,OAAO;;qBAMlB;;;;;;;;EAQX,KAAE,QAAW,OAAO,QAAM"}
|
package/dist/write.d.ts
CHANGED
|
@@ -1,2 +1,312 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Data, Effect, Option, Schema } from "effect";
|
|
2
|
+
import { AnyEntity, EntityField, EntityInput, EntityRef, EntityRelation } from "./index.js";
|
|
3
|
+
import { ContextValue } from "./sets.js";
|
|
4
|
+
import { BeforeRow } from "./invariant.js";
|
|
5
|
+
import { Collation as Collation$1 } from "./collation.js";
|
|
6
|
+
import { AnyExpr, AnyQuery as AnyQuery$1, InputExpr, Predicate, Reads } from "./expr.js";
|
|
7
|
+
import { Held } from "./held.js";
|
|
8
|
+
import { MemoryOperators as MemoryOperators$1 } from "./operators.js";
|
|
9
|
+
import { ExprContext as ExprContext$1 } from "./context.js";
|
|
10
|
+
type AnyField = EntityField<string, string, Schema.Constraint>;
|
|
11
|
+
type OneRelation$1 = EntityRelation<string, string, AnyEntity, 'one', boolean>;
|
|
12
|
+
/** One input key the write sets, and the field it sets. */
|
|
13
|
+
export interface WriteSet {
|
|
14
|
+
readonly key: string;
|
|
15
|
+
readonly field: AnyField;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* One input key holding an amount, and the field it moves. `integer` is whether
|
|
19
|
+
* the amount's schema holds it to an integer (`Schema.isInt`): a writer over an
|
|
20
|
+
* integer column refuses an increment without it, which the Entity cannot see.
|
|
21
|
+
*/
|
|
22
|
+
export interface WriteIncrement extends WriteSet {
|
|
23
|
+
readonly integer: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* One field the write computes (`label := concat(title, ' draft')`): an
|
|
27
|
+
* expression over the row before the write and the input, every defined
|
|
28
|
+
* operator in it expanded, so what is carried is primitive calls alone.
|
|
29
|
+
*/
|
|
30
|
+
export interface WriteAssignment {
|
|
31
|
+
readonly field: AnyField;
|
|
32
|
+
readonly expr: AnyExpr;
|
|
33
|
+
/**
|
|
34
|
+
* Whether `expr` comes to an integer, by the schemas of what it reads and
|
|
35
|
+
* calls: a writer over an integer column refuses an assignment without it,
|
|
36
|
+
* which the Entity cannot see.
|
|
37
|
+
*/
|
|
38
|
+
readonly integer: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A computed assignment as data, bound to one input: what travels to a server
|
|
42
|
+
* and sits in a client's pending patch, which holds no descriptor and no
|
|
43
|
+
* function. A field of the row before the write, a stored value, or a call of
|
|
44
|
+
* a primitive operator by name (`id`, or `id@version`), with the collation it
|
|
45
|
+
* compares text under (`null` for none: this is JSON).
|
|
46
|
+
*/
|
|
47
|
+
export type Computation = Data.TaggedEnum<{
|
|
48
|
+
Field: {
|
|
49
|
+
readonly key: string;
|
|
50
|
+
};
|
|
51
|
+
Value: {
|
|
52
|
+
readonly value: unknown;
|
|
53
|
+
};
|
|
54
|
+
Call: {
|
|
55
|
+
readonly operator: string;
|
|
56
|
+
readonly args: ReadonlyArray<Computation>;
|
|
57
|
+
readonly collation: Collation$1 | null;
|
|
58
|
+
};
|
|
59
|
+
}>;
|
|
60
|
+
export declare const Computation: Data.TaggedEnum.Constructor<{
|
|
61
|
+
readonly _tag: "Call";
|
|
62
|
+
readonly operator: string;
|
|
63
|
+
readonly args: ReadonlyArray<Computation>;
|
|
64
|
+
readonly collation: Collation$1 | null;
|
|
65
|
+
} | {
|
|
66
|
+
readonly _tag: "Field";
|
|
67
|
+
readonly key: string;
|
|
68
|
+
} | {
|
|
69
|
+
readonly _tag: "Value";
|
|
70
|
+
readonly value: unknown;
|
|
71
|
+
}>;
|
|
72
|
+
/** One input key the write points at another row with: a `one` relation, by the target's id. */
|
|
73
|
+
export interface WriteLink {
|
|
74
|
+
readonly key: string;
|
|
75
|
+
readonly relation: OneRelation$1;
|
|
76
|
+
}
|
|
77
|
+
/** One member the write's context sets (`Entity.sets`): a field or a `one` relation, and its value. */
|
|
78
|
+
export interface ContextSet {
|
|
79
|
+
readonly member: AnyField | OneRelation$1;
|
|
80
|
+
readonly value: ContextValue;
|
|
81
|
+
}
|
|
82
|
+
/** What a declared write does to its row. */
|
|
83
|
+
export type WriteKind = 'Update' | 'Insert' | 'Delete';
|
|
84
|
+
/**
|
|
85
|
+
* One row of an Entity, by id, from an operation's input: updated, inserted
|
|
86
|
+
* with an id the client chose, or deleted. `Kind` is the one its constructor
|
|
87
|
+
* made, so what it may be refused with can follow it.
|
|
88
|
+
*/
|
|
89
|
+
export interface Write<E extends AnyEntity, Fields extends Schema.Struct.Fields, Kind extends WriteKind = WriteKind> {
|
|
90
|
+
readonly _tag: Kind;
|
|
91
|
+
readonly input: EntityInput<E, Fields, any>;
|
|
92
|
+
/** The input key holding the row's id, and the Entity's `id` field it is. */
|
|
93
|
+
readonly id: WriteSet & {
|
|
94
|
+
readonly key: keyof Fields & string;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* The input key holding the revision the row was read at, and its field, when
|
|
98
|
+
* the write is refused for a row that moved on since.
|
|
99
|
+
*/
|
|
100
|
+
readonly expect: (WriteSet & {
|
|
101
|
+
readonly key: keyof Fields & string;
|
|
102
|
+
}) | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* What the row must hold for the write to land, over the Entity, read
|
|
105
|
+
* before the write and checked where it commits: an interpreter writes only
|
|
106
|
+
* a row it holds of, and refuses the rest as unmet.
|
|
107
|
+
*/
|
|
108
|
+
readonly when: Option.Option<AnyQuery$1>;
|
|
109
|
+
/** The keys it sets, in the input's order. */
|
|
110
|
+
readonly sets: ReadonlyArray<WriteSet>;
|
|
111
|
+
/**
|
|
112
|
+
* The fields it moves by an amount the input holds (`stock + quantity`),
|
|
113
|
+
* computed where the write commits: the input key, and the field it moves.
|
|
114
|
+
*/
|
|
115
|
+
readonly increments: ReadonlyArray<WriteIncrement>;
|
|
116
|
+
/** The fields it computes, each from the row before the write and the input. */
|
|
117
|
+
readonly assignments: ReadonlyArray<WriteAssignment>;
|
|
118
|
+
/** The `one` relations it points elsewhere, in the input's order. */
|
|
119
|
+
readonly links: ReadonlyArray<WriteLink>;
|
|
120
|
+
/**
|
|
121
|
+
* What the write's context sets where it lands (`Entity.sets`): an insert
|
|
122
|
+
* every such member, an update those set `on: 'write'`, a delete none.
|
|
123
|
+
*/
|
|
124
|
+
readonly context: ReadonlyArray<ContextSet>;
|
|
125
|
+
}
|
|
126
|
+
export type AnyWrite = Write<AnyEntity, any>;
|
|
127
|
+
/** A write declared with a precondition, so it may be refused as unmet. */
|
|
128
|
+
export type ConditionedWrite<E extends AnyEntity, Fields extends Schema.Struct.Fields, Kind extends WriteKind = WriteKind> = Write<E, Fields, Kind> & {
|
|
129
|
+
readonly when: Option.Some<AnyQuery$1>;
|
|
130
|
+
};
|
|
131
|
+
interface RowOptions<Fields extends Schema.Struct.Fields> {
|
|
132
|
+
readonly id: keyof Fields & string;
|
|
133
|
+
readonly expect?: keyof Fields & string;
|
|
134
|
+
}
|
|
135
|
+
/** The keys of `Fields` whose values are numbers. */
|
|
136
|
+
type NumberKeys<Fields> = { [K in keyof Fields & string]: Fields[K] extends Schema.Top ? Schema.Schema.Type<Fields[K]> extends number ? K : never : never; }[keyof Fields & string];
|
|
137
|
+
/** An update's increments: a number field of the Entity, by the input key holding the amount. */
|
|
138
|
+
export type WriteIncrements<E extends AnyEntity, Fields extends Schema.Struct.Fields> = { readonly [K in NumberKeys<{ [F in keyof E['fields']]: E['fields'][F]['schema']; }>]?: NumberKeys<Fields>; };
|
|
139
|
+
/**
|
|
140
|
+
* An update's computed fields: each a field of the Entity, by what it comes
|
|
141
|
+
* to over the row before the write (`row`, each field as `Expr.field`) and
|
|
142
|
+
* the input. Its kind is checked against the field's where it is declared.
|
|
143
|
+
*/
|
|
144
|
+
export type WriteComputations<E extends AnyEntity, Fields extends Schema.Struct.Fields> = { readonly [F in keyof E['fields'] & string]?: (context: {
|
|
145
|
+
readonly row: BeforeRow<E>;
|
|
146
|
+
readonly input: { readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>; };
|
|
147
|
+
}) => AnyExpr; };
|
|
148
|
+
/** An update's options: a row's, what it moves by an amount, and what it computes. */
|
|
149
|
+
type UpdateOptions<E extends AnyEntity, Fields extends Schema.Struct.Fields> = RowOptions<Fields> & {
|
|
150
|
+
readonly increment?: WriteIncrements<E, Fields>;
|
|
151
|
+
readonly compute?: WriteComputations<E, Fields>;
|
|
152
|
+
};
|
|
153
|
+
/** `update`: conditioned when given `when`, which the overloads tell apart. */
|
|
154
|
+
interface UpdateWrite {
|
|
155
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
156
|
+
readonly when: WriteCondition<E, Fields>;
|
|
157
|
+
}): ConditionedWrite<E, Fields, 'Update'>;
|
|
158
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
159
|
+
readonly when?: undefined;
|
|
160
|
+
}): Write<E, Fields, 'Update'>;
|
|
161
|
+
}
|
|
162
|
+
/** `delete`: conditioned when given `when`, as an update is. */
|
|
163
|
+
interface DeleteWrite {
|
|
164
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
165
|
+
readonly when: WriteCondition<E, Fields>;
|
|
166
|
+
}): ConditionedWrite<E, Fields, 'Delete'>;
|
|
167
|
+
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
168
|
+
readonly when?: undefined;
|
|
169
|
+
}): Write<E, Fields, 'Delete'>;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A write's precondition: a predicate over its Entity's row, given one
|
|
173
|
+
* placeholder per input key, as a `Query.define` body is. It is built once,
|
|
174
|
+
* so `input` stands for what the write will be given.
|
|
175
|
+
*/
|
|
176
|
+
export type WriteCondition<E extends AnyEntity, Fields extends Schema.Struct.Fields> = (context: {
|
|
177
|
+
readonly input: { readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>; };
|
|
178
|
+
}) => Predicate & Reads<E['name']>;
|
|
179
|
+
/** One row's new values, in the store's wire shape: what a client patches and a server writes. */
|
|
180
|
+
export interface BoundWrite {
|
|
181
|
+
readonly entity: string;
|
|
182
|
+
readonly id: string;
|
|
183
|
+
readonly values: Readonly<Record<string, unknown>>;
|
|
184
|
+
/**
|
|
185
|
+
* Each `one` relation it points, by the relation's key: the row it points at,
|
|
186
|
+
* or none for an optional relation set to nothing. How a ref is carried (a
|
|
187
|
+
* key on the wire, a foreign key in a table) is each interpreter's.
|
|
188
|
+
*/
|
|
189
|
+
readonly links: Readonly<Record<string, Option.Option<EntityRef>>>;
|
|
190
|
+
/** Each field it moves, by the amount, encoded: added by the server, and predicted over what a client holds. */
|
|
191
|
+
readonly increments: Readonly<Record<string, number>>;
|
|
192
|
+
/** Each field it computes, as data: run by the server where it writes, predicted by a client. */
|
|
193
|
+
readonly assignments: Readonly<Record<string, Computation>>;
|
|
194
|
+
}
|
|
195
|
+
/** What a write's context comes to: see `Write.contextValues`. */
|
|
196
|
+
export interface ContextValues {
|
|
197
|
+
readonly values: Readonly<Record<string, unknown>>;
|
|
198
|
+
readonly links: Readonly<Record<string, Option.Option<EntityRef>>>;
|
|
199
|
+
readonly unfilled: ReadonlyArray<string>;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* One member of one row, as a holder has it: a field's encoded value, or a
|
|
203
|
+
* `one` relation's ref key (`'User:ada'`) or null; unheld where it lacks it. A
|
|
204
|
+
* server reads every row its asker may see, and reads a hidden one as null.
|
|
205
|
+
*/
|
|
206
|
+
export type ContextRead<E, R> = (row: EntityRef, key: string) => Effect.Effect<Held<unknown>, E, R>;
|
|
207
|
+
export declare const Write: {
|
|
208
|
+
/**
|
|
209
|
+
* Sets, on the row whose id is the input's `id` key, every field the input
|
|
210
|
+
* maps a key to. `expect` names the key holding the revision the row was
|
|
211
|
+
* read at: an interpreter writes only a row still at that revision.
|
|
212
|
+
*
|
|
213
|
+
* ```ts
|
|
214
|
+
* const EditPost = Write.update(EditPostInput, { id: 'id' })
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* A key mapped to a `one` relation (`Relation.input`) points the row at the
|
|
218
|
+
* id it holds. Refused where it is written: an `id` that is not the Entity's
|
|
219
|
+
* `id` field, a `many` relation or a nested key (not written by an update
|
|
220
|
+
* yet), or nothing to set.
|
|
221
|
+
*
|
|
222
|
+
* `when` is what the row must hold for the update to land, checked where it
|
|
223
|
+
* commits (`({ input }) => Expr.eq(Post.fields.status, 'draft')`); a row it
|
|
224
|
+
* does not hold of is refused as unmet.
|
|
225
|
+
*
|
|
226
|
+
* `compute` sets a field to what an expression comes to over the row before
|
|
227
|
+
* the write and the input, run where it commits
|
|
228
|
+
* (`{ label: ({ row }) => concat(row.title, ' draft') }`). It is refused for
|
|
229
|
+
* a field another key writes, an expression over the row after the write or
|
|
230
|
+
* another Entity, or one of a kind the field does not store.
|
|
231
|
+
*/
|
|
232
|
+
update: UpdateWrite;
|
|
233
|
+
/**
|
|
234
|
+
* Inserts the row whose id is the input's `id` key, an id the client chose,
|
|
235
|
+
* with every field and `one` relation the input maps a key to. An insert
|
|
236
|
+
* expects no revision: there is no row to have read. A row already there
|
|
237
|
+
* under that id (a retry) is left as it is.
|
|
238
|
+
*
|
|
239
|
+
* What its Entity sets from the write's context (`Entity.sets`: who asks,
|
|
240
|
+
* when) it writes too, never from the input: a server fills them, a client
|
|
241
|
+
* predicts them. An update writes those set `on: 'write'`.
|
|
242
|
+
*/
|
|
243
|
+
insert: <E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: {
|
|
244
|
+
readonly id: keyof Fields & string;
|
|
245
|
+
}) => Write<E, Fields, 'Insert'>;
|
|
246
|
+
/**
|
|
247
|
+
* Deletes the row whose id is the input's `id` key, only while it is still at
|
|
248
|
+
* the revision `expect` names, when it names one. It sets nothing, so a key
|
|
249
|
+
* mapped to a member is refused where it is written. A row already gone (a
|
|
250
|
+
* retry) is gone. `when`, as an update's, is what the row must hold.
|
|
251
|
+
*/
|
|
252
|
+
delete: DeleteWrite;
|
|
253
|
+
/**
|
|
254
|
+
* The revision this input says the row was read at, encoded, with its field:
|
|
255
|
+
* what an interpreter writes only a row still at. None for a write that
|
|
256
|
+
* expects none.
|
|
257
|
+
*/
|
|
258
|
+
expected: <Fields extends Schema.Struct.Fields>(write: Write<AnyEntity, Fields>, value: Schema.Struct.Type<Fields>) => Option.Option<{
|
|
259
|
+
readonly field: string;
|
|
260
|
+
readonly revision: unknown;
|
|
261
|
+
}>;
|
|
262
|
+
/** The members any invocation may write: what a query reading none of them cannot be changed by. */
|
|
263
|
+
writes: (write: AnyWrite) => ReadonlyArray<AnyField | OneRelation$1>;
|
|
264
|
+
/**
|
|
265
|
+
* What the context sets come to, encoded as the store holds them: each
|
|
266
|
+
* field's value and each relation's link. A path from the viewer is read
|
|
267
|
+
* through `read`, one row at a time. `unfilled` names each member there is
|
|
268
|
+
* no value for: none to read (no reading of the time, a row or field
|
|
269
|
+
* `read` does not hold), or a null where the member holds none (a visitor,
|
|
270
|
+
* a viewer of another Entity, a path reaching no row). A server refuses such
|
|
271
|
+
* a write; a client predicts nothing for the member.
|
|
272
|
+
*/
|
|
273
|
+
contextValues: <E, R>(write: AnyWrite, by: ExprContext$1, read: ContextRead<E, R>) => Effect.Effect<ContextValues, E, R>;
|
|
274
|
+
/** The Entities a write's paths from the viewer read, by name: what a server must have a source of. */
|
|
275
|
+
contextReads: (write: AnyWrite) => ReadonlyArray<string>;
|
|
276
|
+
/**
|
|
277
|
+
* The fields a write reads from the row it writes: those its `when` reads,
|
|
278
|
+
* each it increments, each its assignments read, and the revision it
|
|
279
|
+
* expects. What an interpreter must read, and what a startup check judges
|
|
280
|
+
* it could disclose.
|
|
281
|
+
*/
|
|
282
|
+
reads: (write: AnyWrite) => ReadonlyArray<AnyField>;
|
|
283
|
+
/**
|
|
284
|
+
* The row this input writes and its new values, encoded as the store holds
|
|
285
|
+
* them: the patch a client shows before the server answers. `keys`, when
|
|
286
|
+
* given, narrows it to those input keys (what an author changed); the rest of
|
|
287
|
+
* the input is still the whole, validated value.
|
|
288
|
+
*/
|
|
289
|
+
bind: <Fields extends Schema.Struct.Fields>(write: Write<AnyEntity, Fields>, value: Schema.Struct.Type<Fields>, keys?: ReadonlyArray<string>) => BoundWrite;
|
|
290
|
+
/**
|
|
291
|
+
* The fields of the row before it that a bound write's amounts and
|
|
292
|
+
* assignments read: what to read to work out the row it leaves.
|
|
293
|
+
*/
|
|
294
|
+
boundReads: (bound: Pick<BoundWrite, 'increments' | 'assignments'>) => ReadonlyArray<string>;
|
|
295
|
+
/**
|
|
296
|
+
* The row a bound write leaves over `row`, the row before it: its values
|
|
297
|
+
* set, each amount added (a null stays null, as in SQL), and each
|
|
298
|
+
* assignment computed by `operators` over the row before. `unknown` names
|
|
299
|
+
* each field it could not work out: one `row` lacks, or holds as no number
|
|
300
|
+
* for an amount, or an assignment calling an operator the table lacks. That
|
|
301
|
+
* field keeps what `row` holds. The one computation of this row: a guard
|
|
302
|
+
* judges it, a writer explains a miss with it, a client predicts it.
|
|
303
|
+
*/
|
|
304
|
+
apply: (row: Readonly<Record<string, unknown>>, bound: Pick<BoundWrite, 'values' | 'increments' | 'assignments'>, options: {
|
|
305
|
+
readonly operators: MemoryOperators$1;
|
|
306
|
+
}) => {
|
|
307
|
+
readonly row: Readonly<Record<string, unknown>>;
|
|
308
|
+
readonly unknown: ReadonlyArray<string>;
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
export {};
|
|
312
|
+
//# sourceMappingURL=write.d.ts.map
|
|
@@ -0,0 +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;;;;;WAKpB,YAAY,cAAc;;WAE1B,aAAa,cAAc;;WAE3B,OAAO,cAAc;;;;;WAKrB,SAAS,cAAc;;YAGtB,WAAW,MAAM;;YAGjB,iBACV,UAAU,WACV,eAAe,OAAO,OAAO,QAC7B,aAAa,YAAY,aACvB,MAAM,GAAG,QAAQ;WAAmB,MAAM,OAAO,KAAKA;;UAEhD,WAAW,eAAe,OAAO,OAAO;WACvC,UAAU;WACV,eAAe;;;KAIrB,WAAW,aACb,WAAW,kBAAkB,OAAO,WAAW,OAAO,MACnD,OAAO,OAAO,KAAK,OAAO,qBACxB,2BAGA;;YAGI,gBAAgB,UAAU,WAAW,eAAe,OAAO,OAAO,sBAE1E,KAAK,cAAc,WAAW,cAAc,YAAY,oBACtD,WAAW;;;;;;YAQL,kBAAkB,UAAU,WAAW,eAAe,OAAO,OAAO,sBACpE,WAAW,yBAAyB;WACnC,KAAK,UAAU;WACf,mBACG,WAAW,kBAAkB,UAAU,OAAO,OAAO,KAAK,OAAO;MAEzE;;KAIH,cACH,UAAU,WACV,eAAe,OAAO,OAAO,UAC3B,WAAW;WACJ,YAAY,gBAAgB,GAAG;WAC/B,UAAU,kBAAkB,GAAG;;;UAIhC;GACP,UAAU,WAAW,eAAe,OAAO,OAAO,QACjD,OAAO,YAAY,GAAG,cACtB,SAAS,cAAc,GAAG;aAAqB,MAAM,eAAe,GAAG;MACtE,iBAAiB,GAAG;GAEtB,UAAU,WAAW,eAAe,OAAO,OAAO,QACjD,OAAO,YAAY,GAAG,cACtB,SAAS,cAAc,GAAG;aAAqB;MAC9C,MAAM,GAAG;;;UAIJ;GACP,UAAU,WAAW,eAAe,OAAO,OAAO,QACjD,OAAO,YAAY,GAAG,cACtB,SAAS,WAAW;aAAqB,MAAM,eAAe,GAAG;MAChE,iBAAiB,GAAG;GACtB,UAAU,WAAW,eAAe,OAAO,OAAO,QACjD,OAAO,YAAY,GAAG,cACtB,SAAS,WAAW;aAAqB;MACxC,MAAM,GAAG;;;;;;;YA2BF,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;;;iBAqK/B;WACN,QAAQ,SAAS;WACjB,OAAO,SAAS,eAAe,OAAO,OAAO;WAC7C,UAAU;;;;;;;YAQT,YAAY,GAAG,MAAM,KAAK,WAAW,gBAAgB,OAAO,OAAO,eAAe,GAAG;qBAiUpF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;EAgCb,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
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SchemaShape } from "./shape.js";
|
|
1
|
+
import { Data, Effect, Match, Option, Schema, SchemaAST } from "effect";
|
|
3
2
|
import { own, put } from "./record.js";
|
|
3
|
+
import { SchemaShape } from "./shape.js";
|
|
4
4
|
import { Expr, Query, dependenciesOf, isPredicate } from "./expr.js";
|
|
5
5
|
import { Held, idOfRef } from "./held.js";
|
|
6
|
+
import { contextValue, viewerIdOf } from "./context.js";
|
|
6
7
|
import { applyOperator } from "./evaluate.js";
|
|
7
|
-
import { Data, Effect, Match, Option, Schema, SchemaAST } from "effect";
|
|
8
8
|
// src/write.ts
|
|
9
9
|
/**
|
|
10
10
|
* A write as a value: what an operation changes, declared over the input it
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foldkit-entity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Domain structure for Foldkit Plus: an Entity's fields, relations, and derived members as typed values, and Selections of them.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"foldkit",
|
|
@@ -49,12 +49,12 @@
|
|
|
49
49
|
"effect": "^4.1.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"effect": "^4.1.
|
|
52
|
+
"effect": "^4.1.1",
|
|
53
53
|
"tsdown": "^0.23.1",
|
|
54
54
|
"typescript": "7.0.2"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
|
-
"build": "tsdown",
|
|
57
|
+
"build": "node ../../scripts/declarations.mjs . && tsdown && node ../../scripts/declared-any.mjs .",
|
|
58
58
|
"typecheck": "tsc -b"
|
|
59
59
|
}
|
|
60
60
|
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Option } from "effect";
|
|
2
|
-
import { Metadata } from "foldkit-metadata";
|
|
3
|
-
type Collation =
|
|
4
|
-
/** By code point: SQLite `BINARY`, Postgres `COLLATE "C"`. */
|
|
5
|
-
{
|
|
6
|
-
readonly _tag: 'Binary';
|
|
7
|
-
} |
|
|
8
|
-
/** ASCII letters folded to lower case, then by code point: SQLite `NOCASE`, Postgres `lower(x COLLATE "C")`. */
|
|
9
|
-
{
|
|
10
|
-
readonly _tag: 'AsciiFold';
|
|
11
|
-
} |
|
|
12
|
-
/** The backend's collation by this name: ordered by the server, never by a client. */
|
|
13
|
-
{
|
|
14
|
-
readonly _tag: 'Locale';
|
|
15
|
-
readonly locale: string;
|
|
16
|
-
};
|
|
17
|
-
declare const Collation: {
|
|
18
|
-
binary: Collation;
|
|
19
|
-
asciiFold: Collation;
|
|
20
|
-
locale: (locale: string) => Collation;
|
|
21
|
-
/** Member metadata: how a text field orders, wherever a query orders by it. */
|
|
22
|
-
of: (collation: Collation) => Metadata;
|
|
23
|
-
/** The collation a member's metadata declares, if any. */
|
|
24
|
-
declared: (metadata: Metadata) => Option.Option<Collation>;
|
|
25
|
-
/**
|
|
26
|
-
* Text as `asciiFold` compares it: ASCII letters lowercased, every other
|
|
27
|
-
* character as it is, which is what SQLite's `lower` does without ICU and so
|
|
28
|
-
* what compiled SQL folds to. `toLowerCase` would fold `É` to `é` here and not
|
|
29
|
-
* there. An operator implementing `asciiFold` folds with this.
|
|
30
|
-
*/
|
|
31
|
-
foldAscii: (text: string) => string;
|
|
32
|
-
};
|
|
33
|
-
export { Collation as t };
|
|
34
|
-
//# sourceMappingURL=collation-BB4YTIbY.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collation-BB4YTIbY.d.ts","names":[],"sources":["../src/collation.ts"],"mappings":";;KAeY;;;WAEG;;;;WAEA;;;;WAEA;WAAyB;;cAW3B;EACX,QAA6C;EAC7C,WAAmD;EACnD,SAAM,mBAAoB;;EAQ1B,KAAE,WAAc,cAAY;;EAG5B,WAAQ,UAAa,aAAW,OAAO,OAAO;;;;;;;EAS9C,YAAS"}
|