foldkit-entity 0.14.0 → 0.16.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 +147 -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 +436 -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 +400 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -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
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,400 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
1
|
+
import * as EntityMembers from "./entity.js";
|
|
2
|
+
import { Data, Option, Pipeable, Schema } from "effect";
|
|
3
|
+
import { Metadata } from "foldkit-metadata";
|
|
4
|
+
import { EntityTypeId, EntityTypeId as EntityTypeId$1, SelectionPageTypeId, SelectionPageTypeId as SelectionPageTypeId$1, SelectionTypeId, SelectionTypeId as SelectionTypeId$1 } from "./ids.js";
|
|
5
|
+
import { AnyVariantUnion, AnyVariantUnion as AnyVariantUnion$1, CheckCases, EntityVariants, StoredFields, VariantCases, VariantUnion } from "./variants.js";
|
|
6
|
+
import { ContextValue, EntitySet, EntitySet as EntitySet$1, SetOn } from "./sets.js";
|
|
7
|
+
import { AfterRow, BeforeRow, Change, DeclaredInvariant, Invariant, Judgement } from "./invariant.js";
|
|
8
|
+
import { HeldRow, HeldRows } from "./held.js";
|
|
9
|
+
import { ExprContext, ExprContext as ExprContext$1, NowExpr, NowExpr as NowExpr$1, ViewerExpr, ViewerExpr as ViewerExpr$1, ViewerPathExpr, ViewerPathExpr as ViewerPathExpr$1, contextValue, viewerIdOf } from "./context.js";
|
|
10
|
+
import { InputMembers, WrittenAs, WrittenKey, WrittenSpec } from "./entity.js";
|
|
11
|
+
import { AnyWrite, BoundWrite, Computation, ConditionedWrite, ContextRead, ContextSet, ContextValues, Write, WriteAssignment, WriteComputations, WriteCondition, WriteIncrement, WriteKind, WriteLink, WriteSet } from "./write.js";
|
|
12
|
+
export * from "./shape.js";
|
|
13
|
+
export * from "./collation.js";
|
|
14
|
+
export * from "./expr.js";
|
|
15
|
+
export * from "./held.js";
|
|
16
|
+
export * from "./operators.js";
|
|
17
|
+
export * from "./law.js";
|
|
18
|
+
export * from "./text.js";
|
|
19
|
+
export * from "./readFilter.js";
|
|
20
|
+
export * from "./words.js";
|
|
21
|
+
export * from "./evaluate.js";
|
|
22
|
+
/**
|
|
23
|
+
* What makes two descriptors the same Entity. A pipe step returns a new
|
|
24
|
+
* descriptor with the same identity, so compare `identity`, never the objects.
|
|
25
|
+
*/
|
|
26
|
+
export interface EntityIdentity<Name extends string> {
|
|
27
|
+
readonly name: Name;
|
|
28
|
+
/** Distinguishes two Entities that were defined with the same name. */
|
|
29
|
+
readonly token: symbol;
|
|
30
|
+
}
|
|
31
|
+
/** An intrinsic value of the Entity: one property of `entity.schema`. */
|
|
32
|
+
export interface EntityField<Name extends string, Key extends string, S extends Schema.Constraint> {
|
|
33
|
+
readonly _tag: 'Field';
|
|
34
|
+
readonly owner: EntityIdentity<Name>;
|
|
35
|
+
readonly key: Key;
|
|
36
|
+
readonly schema: S;
|
|
37
|
+
readonly metadata: Metadata;
|
|
38
|
+
}
|
|
39
|
+
export type Cardinality = 'one' | 'many';
|
|
40
|
+
/**
|
|
41
|
+
* What happens to a row when the row its `one` relation points at is deleted
|
|
42
|
+
* (`Relation.one(Post, { onDelete: 'cascade' })`): `cascade`, it is deleted
|
|
43
|
+
* with it, by the server in the write that deletes, and foreseen by a client.
|
|
44
|
+
*/
|
|
45
|
+
export type OnDelete = 'cascade';
|
|
46
|
+
/**
|
|
47
|
+
* Who may let a cascade take a row (#348). `child`, the default: the row's own
|
|
48
|
+
* `Delete` rule, judged for whoever deletes the row it points at, so a delete
|
|
49
|
+
* that would take a row they may not delete is refused whole. `parent`: whoever
|
|
50
|
+
* may delete the row it points at may take it too (a post's author removing
|
|
51
|
+
* the likes under it), declared on the relation so it is never a default.
|
|
52
|
+
*/
|
|
53
|
+
export type AuthorizedBy = 'child' | 'parent';
|
|
54
|
+
/** What `Relation.one` / `Relation.many` return, before `Entity.relate` binds it to an owner. */
|
|
55
|
+
export interface RelationSpec<Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
|
|
56
|
+
readonly target: Target;
|
|
57
|
+
readonly cardinality: C;
|
|
58
|
+
readonly optional: Optional;
|
|
59
|
+
readonly onDelete: Option.Option<OnDelete>;
|
|
60
|
+
readonly authorizedBy: Option.Option<AuthorizedBy>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A navigation edge as its owner sees it: one or many of `target`. How the edge
|
|
64
|
+
* is stored (a foreign key, a join table, a nested document) is the
|
|
65
|
+
* interpreter's concern.
|
|
66
|
+
*/
|
|
67
|
+
export interface EntityRelation<Name extends string, Key extends string, Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
|
|
68
|
+
readonly _tag: 'Relation';
|
|
69
|
+
readonly owner: EntityIdentity<Name>;
|
|
70
|
+
readonly key: Key;
|
|
71
|
+
/** The target as `Entity.relate` returned it, so its own relations can be followed. */
|
|
72
|
+
readonly target: () => Target;
|
|
73
|
+
readonly cardinality: C;
|
|
74
|
+
readonly optional: Optional;
|
|
75
|
+
/**
|
|
76
|
+
* What happens to this row when the row a `one` relation points at is
|
|
77
|
+
* deleted; none for a `many`, and for a `one` declared with nothing.
|
|
78
|
+
*/
|
|
79
|
+
readonly onDelete: Option.Option<OnDelete>;
|
|
80
|
+
/** Who may let a cascade through it take this row: some exactly where `onDelete` is. */
|
|
81
|
+
readonly authorizedBy: Option.Option<AuthorizedBy>;
|
|
82
|
+
readonly metadata: Metadata;
|
|
83
|
+
}
|
|
84
|
+
type OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>;
|
|
85
|
+
/**
|
|
86
|
+
* What a derived member is. `Supplied` (`Derived.make`): an interpreter
|
|
87
|
+
* supplies it, and nothing else can say what it is. `Count`
|
|
88
|
+
* (`Derived.count`): how many rows point at this one through `relation`, a
|
|
89
|
+
* `one` relation of the counted Entity. `Exists` (`Derived.exists`): the id of
|
|
90
|
+
* a row pointing here through `relation` whose `viewer` relation points at
|
|
91
|
+
* whoever reads it, or none. A client predicts both from the rows it writes,
|
|
92
|
+
* and a server compiles both from the relations' storage.
|
|
93
|
+
*/
|
|
94
|
+
export type Derivation = Data.TaggedEnum<{
|
|
95
|
+
/**
|
|
96
|
+
* `predict`: how a client foresees it for a row it writes; none, it waits
|
|
97
|
+
* for the server. `forbids`: which write to the row its value refuses, so a
|
|
98
|
+
* client holding it refuses that write where it is made.
|
|
99
|
+
*/
|
|
100
|
+
Supplied: {
|
|
101
|
+
readonly predict: Option.Option<Predict>;
|
|
102
|
+
readonly forbids: Option.Option<Forbids>;
|
|
103
|
+
};
|
|
104
|
+
Count: {
|
|
105
|
+
readonly relation: OneRelation;
|
|
106
|
+
};
|
|
107
|
+
Exists: {
|
|
108
|
+
readonly relation: OneRelation;
|
|
109
|
+
readonly viewer: OneRelation;
|
|
110
|
+
};
|
|
111
|
+
}>;
|
|
112
|
+
export declare const Derivation: Data.TaggedEnum.Constructor<{
|
|
113
|
+
readonly _tag: "Count";
|
|
114
|
+
readonly relation: OneRelation;
|
|
115
|
+
} | {
|
|
116
|
+
readonly _tag: "Exists";
|
|
117
|
+
readonly relation: OneRelation;
|
|
118
|
+
readonly viewer: OneRelation;
|
|
119
|
+
} | {
|
|
120
|
+
readonly _tag: "Supplied";
|
|
121
|
+
readonly predict: Option.Option<Predict>;
|
|
122
|
+
readonly forbids: Option.Option<Forbids>;
|
|
123
|
+
}>;
|
|
124
|
+
/** What a supplied member's prediction is given: the row a write leaves, as its holder has it. */
|
|
125
|
+
export interface Predicting {
|
|
126
|
+
/** The Entity written, as the write declared it. */
|
|
127
|
+
readonly entity: AnyEntity;
|
|
128
|
+
readonly row: HeldRow;
|
|
129
|
+
/** The holder's other rows, where a rule's path reads. */
|
|
130
|
+
readonly rows: HeldRows;
|
|
131
|
+
/** The operation's context: who asks, and the write's time. */
|
|
132
|
+
readonly context: ExprContext$1;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A supplied member foreseen: its value **encoded**, as the store holds it, or
|
|
136
|
+
* none where the holder cannot know it, so nothing is shown before the answer.
|
|
137
|
+
*/
|
|
138
|
+
export type Predict = (predicting: Predicting) => Option.Option<unknown>;
|
|
139
|
+
/**
|
|
140
|
+
* What a write does to a row it finds, as a supplied member's `forbids` is
|
|
141
|
+
* asked: an update's members and the relations it points, or a delete.
|
|
142
|
+
*/
|
|
143
|
+
export interface WriteSurface {
|
|
144
|
+
readonly kind: 'Update' | 'Delete';
|
|
145
|
+
/** The members it sets, moves or computes, relations not among them. */
|
|
146
|
+
readonly members: ReadonlyArray<string>;
|
|
147
|
+
/** Each relation it points: at a row (`Link`), or at none (`Unlink`). */
|
|
148
|
+
readonly relations: ReadonlyArray<{
|
|
149
|
+
readonly relation: string;
|
|
150
|
+
readonly interaction: 'Link' | 'Unlink';
|
|
151
|
+
}>;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Whether a supplied member's value, **encoded** as the store holds it,
|
|
155
|
+
* refuses `surface`, with the reader's words for why when there are any; none
|
|
156
|
+
* where it does not say so for certain. Only a definite refusal: a client
|
|
157
|
+
* refuses the write unseen, and its server would refuse it too.
|
|
158
|
+
*/
|
|
159
|
+
export type Forbids = (value: unknown, surface: WriteSurface) => Option.Option<{
|
|
160
|
+
readonly reason: Option.Option<string>;
|
|
161
|
+
}>;
|
|
162
|
+
/** What `Derived.make`, `Derived.count` or `Derived.exists` returns, before an Entity owns it. */
|
|
163
|
+
export interface DerivedSpec<S extends Schema.Constraint, D extends Derivation = Derivation> {
|
|
164
|
+
readonly schema: S;
|
|
165
|
+
readonly derives: D;
|
|
166
|
+
}
|
|
167
|
+
/** A value consumers may read that is not part of `entity.schema`, and what it derives. */
|
|
168
|
+
export interface EntityDerived<Name extends string, Key extends string, S extends Schema.Constraint, D extends Derivation = Derivation> {
|
|
169
|
+
readonly _tag: 'Derived';
|
|
170
|
+
readonly owner: EntityIdentity<Name>;
|
|
171
|
+
readonly key: Key;
|
|
172
|
+
readonly schema: S;
|
|
173
|
+
readonly derives: D;
|
|
174
|
+
readonly metadata: Metadata;
|
|
175
|
+
}
|
|
176
|
+
export type EntityMember = EntityField<string, string, Schema.Constraint> | EntityRelation<string, string, AnyEntity, Cardinality, boolean> | EntityDerived<string, string, Schema.Constraint>;
|
|
177
|
+
export type RelationSpecs = Readonly<Record<string, RelationSpec<AnyEntity, Cardinality, boolean>>>;
|
|
178
|
+
export type DerivedSpecs = Readonly<Record<string, DerivedSpec<Schema.Constraint>>>;
|
|
179
|
+
type FieldsOf<Name extends string, Fields extends Schema.Struct.Fields> = { readonly [K in keyof Fields & string]: EntityField<Name, K, Fields[K]>; };
|
|
180
|
+
type RelationsOf<Name extends string, Relations extends RelationSpecs> = { readonly [K in keyof Relations & string]: Relations[K] extends RelationSpec<infer Target, infer C, infer Optional> ? EntityRelation<Name, K, Target, C, Optional> : never; };
|
|
181
|
+
type DerivedOf<Name extends string, Derived extends DerivedSpecs> = { readonly [K in keyof Derived & string]: EntityDerived<Name, K, Derived[K]['schema'], Derived[K]['derives']>; };
|
|
182
|
+
export interface Entity<Name extends string, Fields extends Schema.Struct.Fields, Relations extends RelationSpecs = {}, Derived extends DerivedSpecs = {}> extends Pipeable.Pipeable {
|
|
183
|
+
readonly [EntityTypeId$1]: EntityTypeId$1;
|
|
184
|
+
readonly identity: EntityIdentity<Name>;
|
|
185
|
+
readonly name: Name;
|
|
186
|
+
/** The intrinsic value only: no relations, no derived members. */
|
|
187
|
+
readonly schema: Schema.Struct<Fields>;
|
|
188
|
+
readonly fields: FieldsOf<Name, Fields>;
|
|
189
|
+
readonly relations: RelationsOf<Name, Relations>;
|
|
190
|
+
readonly derived: DerivedOf<Name, Derived>;
|
|
191
|
+
/** Every readable member under one namespace; keys never collide. */
|
|
192
|
+
readonly members: FieldsOf<Name, Fields> & RelationsOf<Name, Relations> & DerivedOf<Name, Derived>;
|
|
193
|
+
/** The members whose value is the write's context, never its input (`Entity.sets`), by key. */
|
|
194
|
+
readonly sets: Readonly<Record<string, EntitySet$1>>;
|
|
195
|
+
readonly metadata: Metadata;
|
|
196
|
+
}
|
|
197
|
+
export type AnyEntity = Entity<string, any, any, any>;
|
|
198
|
+
/**
|
|
199
|
+
* An Entity whose rows are one of several shapes (`Entity.defineVariants`).
|
|
200
|
+
* `variants` is Effect's tagged union of them, read with its own `match`;
|
|
201
|
+
* `fields` and `schema` are the stored row, which every case shares.
|
|
202
|
+
*/
|
|
203
|
+
export interface VariantEntity<Name extends string, Fields extends Schema.Struct.Fields, Relations extends RelationSpecs = {}, Derived extends DerivedSpecs = {}, Union extends AnyVariantUnion$1 = AnyVariantUnion$1> extends Entity<Name, Fields, Relations, Derived> {
|
|
204
|
+
readonly variants: Union;
|
|
205
|
+
}
|
|
206
|
+
export type AnyVariantEntity = VariantEntity<string, any, any, any, any>;
|
|
207
|
+
/** The keys of an Entity's `one` relations. */
|
|
208
|
+
type OneRelationKeys<E extends AnyEntity> = { [K in keyof E['relations'] & string]: E['relations'][K]['cardinality'] extends 'one' ? K : never; }[keyof E['relations'] & string];
|
|
209
|
+
/**
|
|
210
|
+
* What a field holding `T` may be set to: the time where it holds a number,
|
|
211
|
+
* the viewer's id where text, and what a path from the viewer reads where it
|
|
212
|
+
* holds that.
|
|
213
|
+
*/
|
|
214
|
+
type ContextValueFor<T> = (number extends T ? NowExpr$1<number> : never) | (T extends string ? ViewerExpr$1<T> : never) | ViewerPathExpr$1<T | null>;
|
|
215
|
+
/** The name of the Entity the `one` relation `K` points at. */
|
|
216
|
+
type TargetName<E extends AnyEntity, K extends keyof E['relations']> = ReturnType<E['relations'][K]['target']>['name'];
|
|
217
|
+
/**
|
|
218
|
+
* What `Entity.sets` takes, by member: a field the value of the write's
|
|
219
|
+
* context it can hold, a `one` relation `Expr.viewer` of its target.
|
|
220
|
+
*/
|
|
221
|
+
export type EntitySets<E extends AnyEntity> = { readonly [K in Exclude<keyof E['fields'] & string, 'id'>]?: ContextValueFor<Schema.Schema.Type<E['fields'][K]['schema']>>; } & { readonly [K in OneRelationKeys<E>]?: ViewerExpr$1<unknown, TargetName<E, K>>; };
|
|
222
|
+
export type Entities = Readonly<Record<string, AnyEntity>>;
|
|
223
|
+
export type RelationMap<Es extends Entities> = { readonly [K in keyof Es]?: RelationSpecs; };
|
|
224
|
+
/** The key in `Es` of the Entity a spec targets, matched by name. */
|
|
225
|
+
export type TargetKey<Es extends Entities, Target extends AnyEntity> = { [K in keyof Es]: [Es[K]['name'], Target['name']] extends [Target['name'], Es[K]['name']] ? K : never; }[keyof Es];
|
|
226
|
+
export type RelatedSpecs<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = { readonly [R in keyof Rs[K]]: Rs[K][R] extends RelationSpec<infer Target, infer C, infer Optional> ? RelationSpec<Related<Es, Rs, TargetKey<Es, Target>>, C, Optional> : never; };
|
|
227
|
+
/**
|
|
228
|
+
* One Entity of an `Entity.relate` result. A relation's target is again a
|
|
229
|
+
* `Related`, so relations can be followed through a cycle; the recursion goes
|
|
230
|
+
* through type arguments, which TypeScript resolves only when asked.
|
|
231
|
+
*/
|
|
232
|
+
export type Related<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = Es[K] extends VariantEntity<infer Name, infer Fields, infer R, infer D, infer U> ? VariantEntity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D, U> : Es[K] extends Entity<infer Name, infer Fields, infer R, infer D> ? Entity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D> : never;
|
|
233
|
+
/**
|
|
234
|
+
* The schema that identifies one of an Entity: its `id` field when that is text
|
|
235
|
+
* (a plain string, a brand, a pattern), and plain text otherwise. An id is text
|
|
236
|
+
* because a ref travels and is stored as text; an Entity keyed by a number still
|
|
237
|
+
* has refs, and their ids are the number as text.
|
|
238
|
+
*/
|
|
239
|
+
export type IdSchemaOf<E> = E extends {
|
|
240
|
+
readonly fields: {
|
|
241
|
+
readonly id: {
|
|
242
|
+
readonly schema: infer S;
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
} ? S extends Schema.Constraint ? [S['Type']] extends [string] ? S : Schema.String : Schema.String : Schema.String;
|
|
246
|
+
/** What identifies one of an Entity: `IdOf<typeof Blog.Post>` is `PostId` when its `id` field is one. */
|
|
247
|
+
export type IdOf<E> = IdSchemaOf<E>['Type'];
|
|
248
|
+
/**
|
|
249
|
+
* What a relation selected with `true` yields: which Entity, and which one. `Id`
|
|
250
|
+
* is the target's own id type, so a ref to an Author cannot be used as a Post's.
|
|
251
|
+
*/
|
|
252
|
+
export interface EntityRef<Name extends string = string, Id extends string = string> {
|
|
253
|
+
readonly entity: Name;
|
|
254
|
+
readonly id: Id;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* A view of the Entity graph: which members to read, and the schema of the
|
|
258
|
+
* value that results. It says nothing about where the value comes from.
|
|
259
|
+
*/
|
|
260
|
+
export interface Selection<Name extends string, Members, S extends Schema.Constraint, Id extends string = string> {
|
|
261
|
+
readonly [SelectionTypeId$1]: SelectionTypeId$1;
|
|
262
|
+
/** Type-only: the id type of the Entity selected, so a reader can ask for the right one. Never set. */
|
|
263
|
+
readonly Id?: Id;
|
|
264
|
+
readonly entity: Entity<Name, any, any, any>;
|
|
265
|
+
/** What was selected, by member key: `true`, or a relation's nested Selection. */
|
|
266
|
+
readonly members: Members;
|
|
267
|
+
readonly schema: S;
|
|
268
|
+
}
|
|
269
|
+
/** The value a Selection reads: `Selected<typeof PostCard>` is one card's fields. */
|
|
270
|
+
export type Selected<S extends Selection<string, unknown, Schema.Constraint>> = S['schema']['Type'];
|
|
271
|
+
/**
|
|
272
|
+
* Which part of a `many` relation to read: the first or last so many, from a
|
|
273
|
+
* cursor or from the end. A cursor is whatever the interpreter handed out with an
|
|
274
|
+
* earlier page; nothing here looks inside it.
|
|
275
|
+
*/
|
|
276
|
+
export interface PageWindow {
|
|
277
|
+
readonly first?: number | undefined;
|
|
278
|
+
readonly last?: number | undefined;
|
|
279
|
+
readonly after?: string | undefined;
|
|
280
|
+
readonly before?: string | undefined;
|
|
281
|
+
}
|
|
282
|
+
/** A Selection of a `many` relation's target, read a page at a time. */
|
|
283
|
+
export interface SelectionPage<Name extends string, S extends Schema.Constraint> {
|
|
284
|
+
readonly [SelectionPageTypeId$1]: SelectionPageTypeId$1;
|
|
285
|
+
readonly selection: Selection<Name, any, S>;
|
|
286
|
+
readonly window: PageWindow;
|
|
287
|
+
}
|
|
288
|
+
/** An input key that carries a relation's target: the id of a `one`, the ids of a `many`. */
|
|
289
|
+
export interface RelationInput<R extends EntityRelation<any, any, any, any, any>> {
|
|
290
|
+
readonly _tag: 'RelationInput';
|
|
291
|
+
readonly relation: R;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* An input key that carries the relation's target itself, written through an
|
|
295
|
+
* input of its own: a post created with a new author, an order with its lines.
|
|
296
|
+
* A `one` holds that input's value, a `many` a list of them.
|
|
297
|
+
*/
|
|
298
|
+
export interface NestedInput<R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>> {
|
|
299
|
+
readonly _tag: 'NestedInput';
|
|
300
|
+
readonly relation: R;
|
|
301
|
+
readonly input: I;
|
|
302
|
+
}
|
|
303
|
+
/** An input key that is about the operation, not the Entity: a reason, a flag, a confirmation. */
|
|
304
|
+
export interface Unmapped {
|
|
305
|
+
readonly _tag: 'Unmapped';
|
|
306
|
+
}
|
|
307
|
+
/** What one key of an operation's input means in terms of the Entity. */
|
|
308
|
+
export type InputMember = EntityField<string, string, Schema.Constraint> | RelationInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>> | NestedInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>, EntityInput<AnyEntity, any, any>> | Unmapped;
|
|
309
|
+
/**
|
|
310
|
+
* An operation's input read against an Entity: which member each input key
|
|
311
|
+
* writes. The operation decides what may be submitted; the Entity only says
|
|
312
|
+
* what each submitted key means.
|
|
313
|
+
*/
|
|
314
|
+
export interface EntityInput<E extends AnyEntity, Fields extends Schema.Struct.Fields, Members> {
|
|
315
|
+
readonly entity: E;
|
|
316
|
+
readonly schema: Schema.Struct<Fields>;
|
|
317
|
+
readonly members: Members;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* How a `one` relation is declared: whether it may point nowhere, and what a
|
|
321
|
+
* delete of its target does. `authorizedBy` is said only beside `onDelete`.
|
|
322
|
+
*/
|
|
323
|
+
export type OneOptions<Optional extends boolean> = {
|
|
324
|
+
readonly optional?: Optional;
|
|
325
|
+
} & ({
|
|
326
|
+
readonly onDelete?: undefined;
|
|
327
|
+
readonly authorizedBy?: undefined;
|
|
328
|
+
} | {
|
|
329
|
+
/** `cascade`: a row pointing here is deleted with the row it points at. */
|
|
330
|
+
readonly onDelete: OnDelete;
|
|
331
|
+
/** Whose `Delete` rule lets the cascade take this row: its own (`child`), or the parent's. */
|
|
332
|
+
readonly authorizedBy?: AuthorizedBy;
|
|
333
|
+
});
|
|
334
|
+
declare function one<Target extends AnyEntity>(target: Target): RelationSpec<Target, 'one', false>;
|
|
335
|
+
declare function one<Target extends AnyEntity, const Optional extends boolean>(target: Target, options: OneOptions<Optional> & {
|
|
336
|
+
readonly optional: Optional;
|
|
337
|
+
}): RelationSpec<Target, 'one', Optional>;
|
|
338
|
+
declare function one<Target extends AnyEntity>(target: Target, options: OneOptions<false>): RelationSpec<Target, 'one', false>;
|
|
339
|
+
/** What the owner sees: one or many of `target`. Used inside `Entity.relate`. */
|
|
340
|
+
export declare const Relation: {
|
|
341
|
+
one: typeof one;
|
|
342
|
+
/** For `Entity.input`: the input key holds this relation's target id, or ids for a `many`. */
|
|
343
|
+
input: <R extends EntityRelation<any, any, any, any, any>>(relation: R) => RelationInput<R>;
|
|
344
|
+
/**
|
|
345
|
+
* For `Entity.input`: the input key holds the relation's target itself, written
|
|
346
|
+
* through `input`, an `Entity.input` of the target. A `many` holds a list.
|
|
347
|
+
*/
|
|
348
|
+
nested: <R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>>(relation: R, input: I) => NestedInput<R, I>;
|
|
349
|
+
many: <Target extends AnyEntity>(target: Target) => RelationSpec<Target, 'many', false>;
|
|
350
|
+
};
|
|
351
|
+
/** A count: never negative, never a fraction. */
|
|
352
|
+
declare const CountSchema: Schema.Number;
|
|
353
|
+
/** The id of the row an exists found, as every Remote id is held: text, or none. */
|
|
354
|
+
declare const WitnessSchema: Schema.OptionFromNullOr<Schema.String>;
|
|
355
|
+
type OwnerName<R> = R extends EntityRelation<infer Name, any, any, any, any> ? Name : never;
|
|
356
|
+
export declare const Derived: {
|
|
357
|
+
/**
|
|
358
|
+
* A member only an interpreter can compute, of type `schema`: it supplies the
|
|
359
|
+
* value. `predict` says how a client foresees it for a row it writes (Guard's
|
|
360
|
+
* `may`); without it the row reads as missing the member until the answer.
|
|
361
|
+
* `forbids` says which write to the row its value refuses (Guard's `may`
|
|
362
|
+
* again), so a client holding it refuses that write where it is made.
|
|
363
|
+
*/
|
|
364
|
+
make: <S extends Schema.Constraint>(schema: S, options?: {
|
|
365
|
+
readonly predict?: Predict | undefined;
|
|
366
|
+
readonly forbids?: Forbids | undefined;
|
|
367
|
+
}) => DerivedSpec<S, Extract<Derivation, {
|
|
368
|
+
readonly _tag: 'Supplied';
|
|
369
|
+
}>>;
|
|
370
|
+
/**
|
|
371
|
+
* How many rows point at this one through `relation`, a `one` relation of
|
|
372
|
+
* the counted Entity: `likeCount: Derived.count(Like.relations.post)` on
|
|
373
|
+
* Post. Added after `Entity.relate`, which makes the relation. A server
|
|
374
|
+
* compiles it from the relation's storage, and a client predicts it from
|
|
375
|
+
* the rows a pending write inserts, deletes or points elsewhere.
|
|
376
|
+
*/
|
|
377
|
+
count: <R extends EntityRelation<string, string, AnyEntity, 'one', boolean>>(relation: R) => DerivedSpec<typeof CountSchema, {
|
|
378
|
+
readonly _tag: 'Count';
|
|
379
|
+
readonly relation: R;
|
|
380
|
+
}>;
|
|
381
|
+
/**
|
|
382
|
+
* Whether a row points at this one through `relation` and at whoever reads
|
|
383
|
+
* it through `viewer`, another `one` relation of the same pointing Entity:
|
|
384
|
+
* `liked: Derived.exists(Like.relations.post, { viewer: Like.relations.user })`
|
|
385
|
+
* on Post. Its value is that row's id as the store holds it, or none, so the
|
|
386
|
+
* reader can take it back (`Write.delete` by that id). It depends on who
|
|
387
|
+
* reads it: a server answers it for the principal it reads as, and a client
|
|
388
|
+
* predicts it for the viewer it is told of.
|
|
389
|
+
*/
|
|
390
|
+
exists: <R extends OneRelation, V extends EntityRelation<OwnerName<R>, string, AnyEntity, 'one', boolean>>(relation: R, options: {
|
|
391
|
+
readonly viewer: V;
|
|
392
|
+
}) => DerivedSpec<typeof WitnessSchema, {
|
|
393
|
+
readonly _tag: 'Exists';
|
|
394
|
+
readonly relation: R;
|
|
395
|
+
readonly viewer: V;
|
|
396
|
+
}>;
|
|
397
|
+
};
|
|
398
|
+
export { type AfterRow, type AnyVariantUnion, type AnyWrite, type BeforeRow, type BoundWrite, type Change, type CheckCases, Computation, type ConditionedWrite, type ContextRead, type ContextSet, type ContextValue, type ContextValues, type DeclaredInvariant, type EntitySet, EntityTypeId, type EntityVariants, type ExprContext, type InputMembers, type Invariant, type Judgement, type NowExpr, SelectionPageTypeId, SelectionTypeId, type SetOn, type StoredFields, type VariantCases, type VariantUnion, type ViewerExpr, type ViewerPathExpr, Write, type WriteAssignment, type WriteComputations, type WriteCondition, type WriteIncrement, type WriteKind, type WriteLink, type WriteSet, type WrittenAs, type WrittenKey, type WrittenSpec, contextValue, viewerIdOf };
|
|
399
|
+
export { EntityMembers as Entity };
|
|
400
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["ExprContext","EntityTypeId","EntitySet","AnyVariantUnion","NowExpr","ViewerExpr","ViewerPathExpr","SelectionTypeId","SelectionPageTypeId"],"sources":["../src/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;iBA2BiB,eAAe;WACrB,MAAM;;WAEN;;;iBAIM,YAAY,qBAAqB,oBAAoB,UAAU,OAAO;WAC5E;WACA,OAAO,eAAe;WACtB,KAAK;WACL,QAAQ;WACR,UAAU;;YAGT;;;;;;YAOA;;;;;;;;YASA;;iBAGK,aACf,eAAe,WACf,UAAU,aACV;WAES,QAAQ;WACR,aAAa;WACb,UAAU;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;;iBAIrB,IAAI,eAAe,WAAW,QAAQ,SAAS,aAAa;iBAC5D,IAAI,eAAe,iBAAiB,0BAC3C,QAAQ,QACR,SAAS,WAAW;WAAuB,UAAU;IACpD,aAAa,eAAe;iBACtB,IAAI,eAAe,WAC1B,QAAQ,QACR,SAAS,oBACR,aAAa;;qBA6BH;;;EAIH,QAAA,UAAU,yCAAuC,UAAY,MAAI,cAAc;;;;;EAO9E,SAAA,UAAU,yCAAyC,UAAU,4BAA0B,UACpF,GAAC,OACJ,MACN,YAAY,GAAG;EAEX,OAAA,eAAe,WAAS,QAAU,WAAS,aAAa;;;cAU3D,aAAW,OAAA;;cAGX,eAAa,OAAA,iBAAA,OAAA;KAEd,UAAU,KAAK,UAAU,qBAAqB,4BAA4B;qBAElE;;;;;;;;EAQX,OAAO,UAAU,OAAO,YAAU,QACxB,GAAC;aAEE,UAAU;aACV,UAAU;QAEpB,YAAY,GAAG,QAAQ;aAAuB;;;;;;;;;EAejD,QAAQ,UAAU,+BAA+B,4BAA0B,UAC/D,MACT,mBAAmB;aAAwB;aAAwB,UAAU;;;;;;;;;;;EAchF,SACE,UAAU,aACV,UAAU,eAAe,UAAU,YAAY,4BAA0B,UAE/D,GAAC;aACS,QAAQ;QAC3B,mBACM;aACI;aAAyB,UAAU;aAAY,QAAQ"}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,115 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Data, Option, Schema } from "effect";
|
|
2
|
+
import { EntityTypeId, SelectionPageTypeId, SelectionTypeId } from "./ids.js";
|
|
2
3
|
import { contextValue, viewerIdOf } from "./context.js";
|
|
3
|
-
import
|
|
4
|
-
import { Law, LawDescription } from "./law.js";
|
|
5
|
-
import { Expr, Order, Query, dependenciesOf, isPredicate, maxPathLength, maxQueryNodes, missingName, noOperators } from "./expr.js";
|
|
6
|
-
import { Held, idOfRef } from "./held.js";
|
|
7
|
-
import { Text } from "./text.js";
|
|
8
|
-
import { Evidence, OperatorDescription, Operators } from "./operators.js";
|
|
9
|
-
import { ContextMissingError, Judge, OperatorMissingError, QueryEvaluateError, RowsMissingError, applyOperator, assertSupported, compareRows, decided, evaluate, evaluateEach, supported, truthOver, valueOfOperand } from "./evaluate.js";
|
|
10
|
-
import { a as Relation, i as EntityTypeId, n as Derived, o as SelectionPageTypeId, r as Entity, s as SelectionTypeId, t as Derivation } from "./src-DKhUqLoC.js";
|
|
11
|
-
import { ReadFilter } from "./readFilter.js";
|
|
12
|
-
import { Words } from "./words.js";
|
|
4
|
+
import * as Entity from "./entity.js";
|
|
13
5
|
import { Computation, Write } from "./write.js";
|
|
14
|
-
export
|
|
6
|
+
export * from "./shape.js";
|
|
7
|
+
export * from "./collation.js";
|
|
8
|
+
export * from "./expr.js";
|
|
9
|
+
export * from "./held.js";
|
|
10
|
+
export * from "./operators.js";
|
|
11
|
+
export * from "./law.js";
|
|
12
|
+
export * from "./text.js";
|
|
13
|
+
export * from "./readFilter.js";
|
|
14
|
+
export * from "./words.js";
|
|
15
|
+
export * from "./evaluate.js";
|
|
16
|
+
// src/index.ts
|
|
17
|
+
/**
|
|
18
|
+
* `foldkit-entity` — domain structure as typed values.
|
|
19
|
+
*
|
|
20
|
+
* An Entity names its intrinsic Fields (one `Schema.Struct`), its Relations
|
|
21
|
+
* (navigation edges to other Entities, declared with `Entity.relate`), and its Derived members (readable
|
|
22
|
+
* values an interpreter supplies). It describes; it fetches, stores, and
|
|
23
|
+
* renders nothing. Remote, Drizzle, and form packages interpret it and attach
|
|
24
|
+
* what they need as `foldkit-metadata`.
|
|
25
|
+
*/
|
|
26
|
+
const Derivation = Data.taggedEnum();
|
|
27
|
+
function one(target, options) {
|
|
28
|
+
const onDelete = options?.onDelete;
|
|
29
|
+
const authorizedBy = options?.authorizedBy;
|
|
30
|
+
if (onDelete !== void 0 && onDelete !== "cascade") throw new Error(`[foldkit-entity] Relation.one(${target.name}): onDelete is 'cascade'; ${String(onDelete)} is not one`);
|
|
31
|
+
if (authorizedBy !== void 0 && authorizedBy !== "child" && authorizedBy !== "parent") throw new Error(`[foldkit-entity] Relation.one(${target.name}): authorizedBy is 'child' or 'parent'; ${String(authorizedBy)} is not one`);
|
|
32
|
+
if (authorizedBy !== void 0 && onDelete === void 0) throw new Error(`[foldkit-entity] Relation.one(${target.name}): authorizedBy says who lets a cascade take the row, so it needs onDelete`);
|
|
33
|
+
return {
|
|
34
|
+
target,
|
|
35
|
+
cardinality: "one",
|
|
36
|
+
optional: options?.optional ?? false,
|
|
37
|
+
onDelete: Option.fromUndefinedOr(onDelete),
|
|
38
|
+
authorizedBy: onDelete === void 0 ? Option.none() : Option.some(authorizedBy ?? "child")
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** What the owner sees: one or many of `target`. Used inside `Entity.relate`. */
|
|
42
|
+
const Relation = {
|
|
43
|
+
one,
|
|
44
|
+
/** For `Entity.input`: the input key holds this relation's target id, or ids for a `many`. */
|
|
45
|
+
input: (relation) => Object.freeze({
|
|
46
|
+
_tag: "RelationInput",
|
|
47
|
+
relation
|
|
48
|
+
}),
|
|
49
|
+
/**
|
|
50
|
+
* For `Entity.input`: the input key holds the relation's target itself, written
|
|
51
|
+
* through `input`, an `Entity.input` of the target. A `many` holds a list.
|
|
52
|
+
*/
|
|
53
|
+
nested: (relation, input) => Object.freeze({
|
|
54
|
+
_tag: "NestedInput",
|
|
55
|
+
relation,
|
|
56
|
+
input
|
|
57
|
+
}),
|
|
58
|
+
many: (target) => ({
|
|
59
|
+
target,
|
|
60
|
+
cardinality: "many",
|
|
61
|
+
optional: false,
|
|
62
|
+
onDelete: Option.none(),
|
|
63
|
+
authorizedBy: Option.none()
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
/** A count: never negative, never a fraction. */
|
|
67
|
+
const CountSchema = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0));
|
|
68
|
+
/** The id of the row an exists found, as every Remote id is held: text, or none. */
|
|
69
|
+
const WitnessSchema = Schema.OptionFromNullOr(Schema.String);
|
|
70
|
+
const Derived = {
|
|
71
|
+
/**
|
|
72
|
+
* A member only an interpreter can compute, of type `schema`: it supplies the
|
|
73
|
+
* value. `predict` says how a client foresees it for a row it writes (Guard's
|
|
74
|
+
* `may`); without it the row reads as missing the member until the answer.
|
|
75
|
+
* `forbids` says which write to the row its value refuses (Guard's `may`
|
|
76
|
+
* again), so a client holding it refuses that write where it is made.
|
|
77
|
+
*/
|
|
78
|
+
make: (schema, options = {}) => ({
|
|
79
|
+
schema,
|
|
80
|
+
derives: Derivation.Supplied({
|
|
81
|
+
predict: Option.fromUndefinedOr(options.predict),
|
|
82
|
+
forbids: Option.fromUndefinedOr(options.forbids)
|
|
83
|
+
})
|
|
84
|
+
}),
|
|
85
|
+
/**
|
|
86
|
+
* How many rows point at this one through `relation`, a `one` relation of
|
|
87
|
+
* the counted Entity: `likeCount: Derived.count(Like.relations.post)` on
|
|
88
|
+
* Post. Added after `Entity.relate`, which makes the relation. A server
|
|
89
|
+
* compiles it from the relation's storage, and a client predicts it from
|
|
90
|
+
* the rows a pending write inserts, deletes or points elsewhere.
|
|
91
|
+
*/
|
|
92
|
+
count: (relation) => ({
|
|
93
|
+
schema: CountSchema,
|
|
94
|
+
derives: Derivation.Count({ relation })
|
|
95
|
+
}),
|
|
96
|
+
/**
|
|
97
|
+
* Whether a row points at this one through `relation` and at whoever reads
|
|
98
|
+
* it through `viewer`, another `one` relation of the same pointing Entity:
|
|
99
|
+
* `liked: Derived.exists(Like.relations.post, { viewer: Like.relations.user })`
|
|
100
|
+
* on Post. Its value is that row's id as the store holds it, or none, so the
|
|
101
|
+
* reader can take it back (`Write.delete` by that id). It depends on who
|
|
102
|
+
* reads it: a server answers it for the principal it reads as, and a client
|
|
103
|
+
* predicts it for the viewer it is told of.
|
|
104
|
+
*/
|
|
105
|
+
exists: (relation, options) => ({
|
|
106
|
+
schema: WitnessSchema,
|
|
107
|
+
derives: Derivation.Exists({
|
|
108
|
+
relation,
|
|
109
|
+
viewer: options.viewer
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
};
|
|
113
|
+
export { Computation, Derivation, Derived, Entity, EntityTypeId, Relation, SelectionPageTypeId, SelectionTypeId, Write, contextValue, viewerIdOf };
|
|
114
|
+
|
|
115
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * `foldkit-entity` — domain structure as typed values.\n *\n * An Entity names its intrinsic Fields (one `Schema.Struct`), its Relations\n * (navigation edges to other Entities, declared with `Entity.relate`), and its Derived members (readable\n * values an interpreter supplies). It describes; it fetches, stores, and\n * renders nothing. Remote, Drizzle, and form packages interpret it and attach\n * what they need as `foldkit-metadata`.\n */\nimport { Data, Option, type Pipeable, Schema } from 'effect'\nimport type { Metadata } from 'foldkit-metadata'\nimport type { ExprContext, NowExpr, ViewerExpr, ViewerPathExpr } from './context.js'\nimport type { HeldRow, HeldRows } from './held.js'\nimport { EntityTypeId, SelectionPageTypeId, SelectionTypeId } from './ids.js'\nimport type { EntitySet } from './sets.js'\n// The members of `Entity`, a namespace rather than an object so a page keeps only\n// those it calls (#378). Exported under the interface's name; `export * as`\n// would make each `Entity` type here a reference the declaration emitter refuses.\nimport * as EntityMembers from './entity.js'\nimport type { AnyVariantUnion } from './variants.js'\n\nexport { EntityTypeId, SelectionPageTypeId, SelectionTypeId } from './ids.js'\n\n/**\n * What makes two descriptors the same Entity. A pipe step returns a new\n * descriptor with the same identity, so compare `identity`, never the objects.\n */\nexport interface EntityIdentity<Name extends string> {\n readonly name: Name\n /** Distinguishes two Entities that were defined with the same name. */\n readonly token: symbol\n}\n\n/** An intrinsic value of the Entity: one property of `entity.schema`. */\nexport interface EntityField<Name extends string, Key extends string, S extends Schema.Constraint> {\n readonly _tag: 'Field'\n readonly owner: EntityIdentity<Name>\n readonly key: Key\n readonly schema: S\n readonly metadata: Metadata\n}\n\nexport type Cardinality = 'one' | 'many'\n\n/**\n * What happens to a row when the row its `one` relation points at is deleted\n * (`Relation.one(Post, { onDelete: 'cascade' })`): `cascade`, it is deleted\n * with it, by the server in the write that deletes, and foreseen by a client.\n */\nexport type OnDelete = 'cascade'\n\n/**\n * Who may let a cascade take a row (#348). `child`, the default: the row's own\n * `Delete` rule, judged for whoever deletes the row it points at, so a delete\n * that would take a row they may not delete is refused whole. `parent`: whoever\n * may delete the row it points at may take it too (a post's author removing\n * the likes under it), declared on the relation so it is never a default.\n */\nexport type AuthorizedBy = 'child' | 'parent'\n\n/** What `Relation.one` / `Relation.many` return, before `Entity.relate` binds it to an owner. */\nexport interface RelationSpec<\n Target extends AnyEntity,\n C extends Cardinality,\n Optional extends boolean,\n> {\n readonly target: Target\n readonly cardinality: C\n readonly optional: Optional\n readonly onDelete: Option.Option<OnDelete>\n readonly authorizedBy: Option.Option<AuthorizedBy>\n}\n\n/**\n * A navigation edge as its owner sees it: one or many of `target`. How the edge\n * is stored (a foreign key, a join table, a nested document) is the\n * interpreter's concern.\n */\nexport interface EntityRelation<\n Name extends string,\n Key extends string,\n Target extends AnyEntity,\n C extends Cardinality,\n Optional extends boolean,\n> {\n readonly _tag: 'Relation'\n readonly owner: EntityIdentity<Name>\n readonly key: Key\n /** The target as `Entity.relate` returned it, so its own relations can be followed. */\n readonly target: () => Target\n readonly cardinality: C\n readonly optional: Optional\n /**\n * What happens to this row when the row a `one` relation points at is\n * deleted; none for a `many`, and for a `one` declared with nothing.\n */\n readonly onDelete: Option.Option<OnDelete>\n /** Who may let a cascade through it take this row: some exactly where `onDelete` is. */\n readonly authorizedBy: Option.Option<AuthorizedBy>\n readonly metadata: Metadata\n}\n\ntype OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>\n\n/**\n * What a derived member is. `Supplied` (`Derived.make`): an interpreter\n * supplies it, and nothing else can say what it is. `Count`\n * (`Derived.count`): how many rows point at this one through `relation`, a\n * `one` relation of the counted Entity. `Exists` (`Derived.exists`): the id of\n * a row pointing here through `relation` whose `viewer` relation points at\n * whoever reads it, or none. A client predicts both from the rows it writes,\n * and a server compiles both from the relations' storage.\n */\nexport type Derivation = Data.TaggedEnum<{\n /**\n * `predict`: how a client foresees it for a row it writes; none, it waits\n * for the server. `forbids`: which write to the row its value refuses, so a\n * client holding it refuses that write where it is made.\n */\n Supplied: { readonly predict: Option.Option<Predict>; readonly forbids: Option.Option<Forbids> }\n Count: { readonly relation: OneRelation }\n Exists: { readonly relation: OneRelation; readonly viewer: OneRelation }\n}>\nexport const Derivation = Data.taggedEnum<Derivation>()\n\n/** What a supplied member's prediction is given: the row a write leaves, as its holder has it. */\nexport interface Predicting {\n /** The Entity written, as the write declared it. */\n readonly entity: AnyEntity\n readonly row: HeldRow\n /** The holder's other rows, where a rule's path reads. */\n readonly rows: HeldRows\n /** The operation's context: who asks, and the write's time. */\n readonly context: ExprContext\n}\n\n/**\n * A supplied member foreseen: its value **encoded**, as the store holds it, or\n * none where the holder cannot know it, so nothing is shown before the answer.\n */\nexport type Predict = (predicting: Predicting) => Option.Option<unknown>\n\n/**\n * What a write does to a row it finds, as a supplied member's `forbids` is\n * asked: an update's members and the relations it points, or a delete.\n */\nexport interface WriteSurface {\n readonly kind: 'Update' | 'Delete'\n /** The members it sets, moves or computes, relations not among them. */\n readonly members: ReadonlyArray<string>\n /** Each relation it points: at a row (`Link`), or at none (`Unlink`). */\n readonly relations: ReadonlyArray<{\n readonly relation: string\n readonly interaction: 'Link' | 'Unlink'\n }>\n}\n\n/**\n * Whether a supplied member's value, **encoded** as the store holds it,\n * refuses `surface`, with the reader's words for why when there are any; none\n * where it does not say so for certain. Only a definite refusal: a client\n * refuses the write unseen, and its server would refuse it too.\n */\nexport type Forbids = (\n value: unknown,\n surface: WriteSurface,\n) => Option.Option<{ readonly reason: Option.Option<string> }>\n\n/** What `Derived.make`, `Derived.count` or `Derived.exists` returns, before an Entity owns it. */\nexport interface DerivedSpec<S extends Schema.Constraint, D extends Derivation = Derivation> {\n readonly schema: S\n readonly derives: D\n}\n\n/** A value consumers may read that is not part of `entity.schema`, and what it derives. */\nexport interface EntityDerived<\n Name extends string,\n Key extends string,\n S extends Schema.Constraint,\n D extends Derivation = Derivation,\n> {\n readonly _tag: 'Derived'\n readonly owner: EntityIdentity<Name>\n readonly key: Key\n readonly schema: S\n readonly derives: D\n readonly metadata: Metadata\n}\n\nexport type EntityMember =\n | EntityField<string, string, Schema.Constraint>\n | EntityRelation<string, string, AnyEntity, Cardinality, boolean>\n | EntityDerived<string, string, Schema.Constraint>\n\nexport type RelationSpecs = Readonly<Record<string, RelationSpec<AnyEntity, Cardinality, boolean>>>\nexport type DerivedSpecs = Readonly<Record<string, DerivedSpec<Schema.Constraint>>>\n\ntype FieldsOf<Name extends string, Fields extends Schema.Struct.Fields> = {\n readonly [K in keyof Fields & string]: EntityField<Name, K, Fields[K]>\n}\ntype RelationsOf<Name extends string, Relations extends RelationSpecs> = {\n readonly [K in keyof Relations & string]: Relations[K] extends RelationSpec<\n infer Target,\n infer C,\n infer Optional\n >\n ? EntityRelation<Name, K, Target, C, Optional>\n : never\n}\ntype DerivedOf<Name extends string, Derived extends DerivedSpecs> = {\n readonly [K in keyof Derived & string]: EntityDerived<\n Name,\n K,\n Derived[K]['schema'],\n Derived[K]['derives']\n >\n}\n\nexport interface Entity<\n Name extends string,\n Fields extends Schema.Struct.Fields,\n Relations extends RelationSpecs = {},\n Derived extends DerivedSpecs = {},\n>\n extends Pipeable.Pipeable {\n readonly [EntityTypeId]: EntityTypeId\n readonly identity: EntityIdentity<Name>\n readonly name: Name\n /** The intrinsic value only: no relations, no derived members. */\n readonly schema: Schema.Struct<Fields>\n readonly fields: FieldsOf<Name, Fields>\n readonly relations: RelationsOf<Name, Relations>\n readonly derived: DerivedOf<Name, Derived>\n /** Every readable member under one namespace; keys never collide. */\n readonly members: FieldsOf<Name, Fields> & RelationsOf<Name, Relations> & DerivedOf<Name, Derived>\n /** The members whose value is the write's context, never its input (`Entity.sets`), by key. */\n readonly sets: Readonly<Record<string, EntitySet>>\n readonly metadata: Metadata\n}\n\nexport type AnyEntity = Entity<string, any, any, any>\n\n/**\n * An Entity whose rows are one of several shapes (`Entity.defineVariants`).\n * `variants` is Effect's tagged union of them, read with its own `match`;\n * `fields` and `schema` are the stored row, which every case shares.\n */\nexport interface VariantEntity<\n Name extends string,\n Fields extends Schema.Struct.Fields,\n Relations extends RelationSpecs = {},\n Derived extends DerivedSpecs = {},\n Union extends AnyVariantUnion = AnyVariantUnion,\n> extends Entity<Name, Fields, Relations, Derived> {\n readonly variants: Union\n}\n\nexport type AnyVariantEntity = VariantEntity<string, any, any, any, any>\n\n/** The keys of an Entity's `one` relations. */\ntype OneRelationKeys<E extends AnyEntity> = {\n [K in keyof E['relations'] & string]: E['relations'][K]['cardinality'] extends 'one' ? K : never\n}[keyof E['relations'] & string]\n\n/**\n * What a field holding `T` may be set to: the time where it holds a number,\n * the viewer's id where text, and what a path from the viewer reads where it\n * holds that.\n */\ntype ContextValueFor<T> =\n | (number extends T ? NowExpr<number> : never)\n | (T extends string ? ViewerExpr<T> : never)\n | ViewerPathExpr<T | null>\n\n/** The name of the Entity the `one` relation `K` points at. */\ntype TargetName<E extends AnyEntity, K extends keyof E['relations']> = ReturnType<\n E['relations'][K]['target']\n>['name']\n\n/**\n * What `Entity.sets` takes, by member: a field the value of the write's\n * context it can hold, a `one` relation `Expr.viewer` of its target.\n */\nexport type EntitySets<E extends AnyEntity> = {\n readonly [K in Exclude<keyof E['fields'] & string, 'id'>]?: ContextValueFor<\n Schema.Schema.Type<E['fields'][K]['schema']>\n >\n} & {\n readonly [K in OneRelationKeys<E>]?: ViewerExpr<unknown, TargetName<E, K>>\n}\n\nexport type Entities = Readonly<Record<string, AnyEntity>>\nexport type RelationMap<Es extends Entities> = { readonly [K in keyof Es]?: RelationSpecs }\n\n/** The key in `Es` of the Entity a spec targets, matched by name. */\nexport type TargetKey<Es extends Entities, Target extends AnyEntity> = {\n [K in keyof Es]: [Es[K]['name'], Target['name']] extends [Target['name'], Es[K]['name']]\n ? K\n : never\n}[keyof Es]\n\nexport type RelatedSpecs<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = {\n readonly [R in keyof Rs[K]]: Rs[K][R] extends RelationSpec<infer Target, infer C, infer Optional>\n ? RelationSpec<Related<Es, Rs, TargetKey<Es, Target>>, C, Optional>\n : never\n}\n\n/**\n * One Entity of an `Entity.relate` result. A relation's target is again a\n * `Related`, so relations can be followed through a cycle; the recursion goes\n * through type arguments, which TypeScript resolves only when asked.\n */\nexport type Related<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> =\n Es[K] extends VariantEntity<infer Name, infer Fields, infer R, infer D, infer U>\n ? VariantEntity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D, U>\n : Es[K] extends Entity<infer Name, infer Fields, infer R, infer D>\n ? Entity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D>\n : never\n\n/**\n * The schema that identifies one of an Entity: its `id` field when that is text\n * (a plain string, a brand, a pattern), and plain text otherwise. An id is text\n * because a ref travels and is stored as text; an Entity keyed by a number still\n * has refs, and their ids are the number as text.\n */\nexport type IdSchemaOf<E> = E extends {\n readonly fields: { readonly id: { readonly schema: infer S } }\n}\n ? S extends Schema.Constraint\n ? [S['Type']] extends [string]\n ? S\n : Schema.String\n : Schema.String\n : Schema.String\n\n/** What identifies one of an Entity: `IdOf<typeof Blog.Post>` is `PostId` when its `id` field is one. */\nexport type IdOf<E> = IdSchemaOf<E>['Type']\n\n/**\n * What a relation selected with `true` yields: which Entity, and which one. `Id`\n * is the target's own id type, so a ref to an Author cannot be used as a Post's.\n */\nexport interface EntityRef<Name extends string = string, Id extends string = string> {\n readonly entity: Name\n readonly id: Id\n}\n\n/**\n * A view of the Entity graph: which members to read, and the schema of the\n * value that results. It says nothing about where the value comes from.\n */\nexport interface Selection<\n Name extends string,\n Members,\n S extends Schema.Constraint,\n Id extends string = string,\n> {\n readonly [SelectionTypeId]: SelectionTypeId\n /** Type-only: the id type of the Entity selected, so a reader can ask for the right one. Never set. */\n readonly Id?: Id\n readonly entity: Entity<Name, any, any, any>\n /** What was selected, by member key: `true`, or a relation's nested Selection. */\n readonly members: Members\n readonly schema: S\n}\n\n/** The value a Selection reads: `Selected<typeof PostCard>` is one card's fields. */\nexport type Selected<S extends Selection<string, unknown, Schema.Constraint>> = S['schema']['Type']\n\n/**\n * Which part of a `many` relation to read: the first or last so many, from a\n * cursor or from the end. A cursor is whatever the interpreter handed out with an\n * earlier page; nothing here looks inside it.\n */\nexport interface PageWindow {\n readonly first?: number | undefined\n readonly last?: number | undefined\n readonly after?: string | undefined\n readonly before?: string | undefined\n}\n\n/** A Selection of a `many` relation's target, read a page at a time. */\nexport interface SelectionPage<Name extends string, S extends Schema.Constraint> {\n readonly [SelectionPageTypeId]: SelectionPageTypeId\n readonly selection: Selection<Name, any, S>\n readonly window: PageWindow\n}\n\n/** An input key that carries a relation's target: the id of a `one`, the ids of a `many`. */\nexport interface RelationInput<R extends EntityRelation<any, any, any, any, any>> {\n readonly _tag: 'RelationInput'\n readonly relation: R\n}\n\n/**\n * An input key that carries the relation's target itself, written through an\n * input of its own: a post created with a new author, an order with its lines.\n * A `one` holds that input's value, a `many` a list of them.\n */\nexport interface NestedInput<\n R extends EntityRelation<any, any, any, any, any>,\n I extends EntityInput<any, any, any>,\n> {\n readonly _tag: 'NestedInput'\n readonly relation: R\n readonly input: I\n}\n\n/** An input key that is about the operation, not the Entity: a reason, a flag, a confirmation. */\nexport interface Unmapped {\n readonly _tag: 'Unmapped'\n}\n\n/** What one key of an operation's input means in terms of the Entity. */\nexport type InputMember =\n | EntityField<string, string, Schema.Constraint>\n | RelationInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>>\n | NestedInput<\n EntityRelation<string, string, AnyEntity, Cardinality, boolean>,\n EntityInput<AnyEntity, any, any>\n >\n | Unmapped\n\n/**\n * An operation's input read against an Entity: which member each input key\n * writes. The operation decides what may be submitted; the Entity only says\n * what each submitted key means.\n */\nexport interface EntityInput<E extends AnyEntity, Fields extends Schema.Struct.Fields, Members> {\n readonly entity: E\n readonly schema: Schema.Struct<Fields>\n readonly members: Members\n}\n\nexport type { InputMembers, WrittenAs, WrittenKey, WrittenSpec } from './entity.js'\nexport { EntityMembers as Entity }\n\n/**\n * How a `one` relation is declared: whether it may point nowhere, and what a\n * delete of its target does. `authorizedBy` is said only beside `onDelete`.\n */\nexport type OneOptions<Optional extends boolean> = {\n readonly optional?: Optional\n} & (\n | { readonly onDelete?: undefined; readonly authorizedBy?: undefined }\n | {\n /** `cascade`: a row pointing here is deleted with the row it points at. */\n readonly onDelete: OnDelete\n /** Whose `Delete` rule lets the cascade take this row: its own (`child`), or the parent's. */\n readonly authorizedBy?: AuthorizedBy\n }\n)\n\nfunction one<Target extends AnyEntity>(target: Target): RelationSpec<Target, 'one', false>\nfunction one<Target extends AnyEntity, const Optional extends boolean>(\n target: Target,\n options: OneOptions<Optional> & { readonly optional: Optional },\n): RelationSpec<Target, 'one', Optional>\nfunction one<Target extends AnyEntity>(\n target: Target,\n options: OneOptions<false>,\n): RelationSpec<Target, 'one', false>\nfunction one(\n target: AnyEntity,\n options?: OneOptions<boolean>,\n): RelationSpec<AnyEntity, 'one', boolean> {\n const onDelete = options?.onDelete\n const authorizedBy = options?.authorizedBy\n if (onDelete !== undefined && onDelete !== 'cascade')\n throw new Error(\n `[foldkit-entity] Relation.one(${target.name}): onDelete is 'cascade'; ${String(onDelete)} is not one`,\n )\n if (authorizedBy !== undefined && authorizedBy !== 'child' && authorizedBy !== 'parent')\n throw new Error(\n `[foldkit-entity] Relation.one(${target.name}): authorizedBy is 'child' or 'parent'; ${String(authorizedBy)} is not one`,\n )\n if (authorizedBy !== undefined && onDelete === undefined)\n throw new Error(\n `[foldkit-entity] Relation.one(${target.name}): authorizedBy says who lets a cascade take the row, so it needs onDelete`,\n )\n return {\n target,\n cardinality: 'one',\n optional: options?.optional ?? false,\n onDelete: Option.fromUndefinedOr(onDelete),\n authorizedBy: onDelete === undefined ? Option.none() : Option.some(authorizedBy ?? 'child'),\n }\n}\n\n/** What the owner sees: one or many of `target`. Used inside `Entity.relate`. */\nexport const Relation = {\n one,\n\n /** For `Entity.input`: the input key holds this relation's target id, or ids for a `many`. */\n input: <R extends EntityRelation<any, any, any, any, any>>(relation: R): RelationInput<R> =>\n Object.freeze({ _tag: 'RelationInput', relation }),\n\n /**\n * For `Entity.input`: the input key holds the relation's target itself, written\n * through `input`, an `Entity.input` of the target. A `many` holds a list.\n */\n nested: <R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>>(\n relation: R,\n input: I,\n ): NestedInput<R, I> => Object.freeze({ _tag: 'NestedInput', relation, input }),\n\n many: <Target extends AnyEntity>(target: Target): RelationSpec<Target, 'many', false> => ({\n target,\n cardinality: 'many',\n optional: false,\n onDelete: Option.none(),\n authorizedBy: Option.none(),\n }),\n}\n\n/** A count: never negative, never a fraction. */\nconst CountSchema = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0))\n\n/** The id of the row an exists found, as every Remote id is held: text, or none. */\nconst WitnessSchema = Schema.OptionFromNullOr(Schema.String)\n\ntype OwnerName<R> = R extends EntityRelation<infer Name, any, any, any, any> ? Name : never\n\nexport const Derived = {\n /**\n * A member only an interpreter can compute, of type `schema`: it supplies the\n * value. `predict` says how a client foresees it for a row it writes (Guard's\n * `may`); without it the row reads as missing the member until the answer.\n * `forbids` says which write to the row its value refuses (Guard's `may`\n * again), so a client holding it refuses that write where it is made.\n */\n make: <S extends Schema.Constraint>(\n schema: S,\n options: {\n readonly predict?: Predict | undefined\n readonly forbids?: Forbids | undefined\n } = {},\n ): DerivedSpec<S, Extract<Derivation, { readonly _tag: 'Supplied' }>> => ({\n schema,\n derives: Derivation.Supplied({\n predict: Option.fromUndefinedOr(options.predict),\n forbids: Option.fromUndefinedOr(options.forbids),\n }),\n }),\n\n /**\n * How many rows point at this one through `relation`, a `one` relation of\n * the counted Entity: `likeCount: Derived.count(Like.relations.post)` on\n * Post. Added after `Entity.relate`, which makes the relation. A server\n * compiles it from the relation's storage, and a client predicts it from\n * the rows a pending write inserts, deletes or points elsewhere.\n */\n count: <R extends EntityRelation<string, string, AnyEntity, 'one', boolean>>(\n relation: R,\n ): DerivedSpec<typeof CountSchema, { readonly _tag: 'Count'; readonly relation: R }> => ({\n schema: CountSchema,\n derives: Derivation.Count({ relation }) as { readonly _tag: 'Count'; readonly relation: R },\n }),\n\n /**\n * Whether a row points at this one through `relation` and at whoever reads\n * it through `viewer`, another `one` relation of the same pointing Entity:\n * `liked: Derived.exists(Like.relations.post, { viewer: Like.relations.user })`\n * on Post. Its value is that row's id as the store holds it, or none, so the\n * reader can take it back (`Write.delete` by that id). It depends on who\n * reads it: a server answers it for the principal it reads as, and a client\n * predicts it for the viewer it is told of.\n */\n exists: <\n R extends OneRelation,\n V extends EntityRelation<OwnerName<R>, string, AnyEntity, 'one', boolean>,\n >(\n relation: R,\n options: { readonly viewer: V },\n ): DerivedSpec<\n typeof WitnessSchema,\n { readonly _tag: 'Exists'; readonly relation: R; readonly viewer: V }\n > => ({\n schema: WitnessSchema,\n derives: Derivation.Exists({ relation, viewer: options.viewer }) as {\n readonly _tag: 'Exists'\n readonly relation: R\n readonly viewer: V\n },\n }),\n}\n\nexport * from './expr.js'\nexport {\n contextValue,\n viewerIdOf,\n type ExprContext,\n type NowExpr,\n type ViewerExpr,\n type ViewerPathExpr,\n} from './context.js'\nexport * from './collation.js'\nexport * from './operators.js'\nexport * from './law.js'\nexport * from './text.js'\nexport * from './readFilter.js'\nexport * from './words.js'\nexport * from './shape.js'\nexport type {\n AnyVariantUnion,\n CheckCases,\n EntityVariants,\n StoredFields,\n VariantCases,\n VariantUnion,\n} from './variants.js'\n// The reference semantics of the IR above. The conformance suite that proves\n// an interpreter agrees with it is `foldkit-entity/conformance`, kept out of\n// this entry because it is fixture data every form and admin screen would\n// otherwise carry.\nexport * from './evaluate.js'\nexport * from './held.js'\nexport type {\n AfterRow,\n BeforeRow,\n Change,\n DeclaredInvariant,\n Invariant,\n Judgement,\n} from './invariant.js'\nexport type { ContextValue, EntitySet, SetOn } from './sets.js'\nexport {\n Write,\n type AnyWrite,\n type BoundWrite,\n type ConditionedWrite,\n Computation,\n type WriteAssignment,\n type WriteComputations,\n type WriteCondition,\n type WriteIncrement,\n type WriteKind,\n type WriteLink,\n type WriteSet,\n type ContextSet,\n type ContextRead,\n type ContextValues,\n} from './write.js'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA2HA,MAAa,aAAa,KAAK,WAAuB;AAmVtD,SAAS,IACP,QACA,SACyC;CACzC,MAAM,WAAW,SAAS;CAC1B,MAAM,eAAe,SAAS;CAC9B,IAAI,aAAa,KAAA,KAAa,aAAa,WACzC,MAAM,IAAI,MACR,iCAAiC,OAAO,KAAK,4BAA4B,OAAO,QAAQ,EAAE,YAC5F;CACF,IAAI,iBAAiB,KAAA,KAAa,iBAAiB,WAAW,iBAAiB,UAC7E,MAAM,IAAI,MACR,iCAAiC,OAAO,KAAK,0CAA0C,OAAO,YAAY,EAAE,YAC9G;CACF,IAAI,iBAAiB,KAAA,KAAa,aAAa,KAAA,GAC7C,MAAM,IAAI,MACR,iCAAiC,OAAO,KAAK,2EAC/C;CACF,OAAO;EACL;EACA,aAAa;EACb,UAAU,SAAS,YAAY;EAC/B,UAAU,OAAO,gBAAgB,QAAQ;EACzC,cAAc,aAAa,KAAA,IAAY,OAAO,KAAK,IAAI,OAAO,KAAK,gBAAgB,OAAO;CAC5F;AACF;;AAGA,MAAa,WAAW;CACtB;;CAGA,QAA2D,aACzD,OAAO,OAAO;EAAE,MAAM;EAAiB;CAAS,CAAC;;;;;CAMnD,SACE,UACA,UACsB,OAAO,OAAO;EAAE,MAAM;EAAe;EAAU;CAAM,CAAC;CAE9E,OAAiC,YAAyD;EACxF;EACA,aAAa;EACb,UAAU;EACV,UAAU,OAAO,KAAK;EACtB,cAAc,OAAO,KAAK;CAC5B;AACF;;AAGA,MAA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