@latticecms/core 0.0.0 → 0.2.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.
Files changed (113) hide show
  1. package/README.md +1297 -2
  2. package/dist/access.d.ts +209 -0
  3. package/dist/access.d.ts.map +1 -0
  4. package/dist/access.js +241 -0
  5. package/dist/access.js.map +1 -0
  6. package/dist/build.d.ts +555 -0
  7. package/dist/build.d.ts.map +1 -0
  8. package/dist/build.js +655 -0
  9. package/dist/build.js.map +1 -0
  10. package/dist/collection.d.ts +480 -0
  11. package/dist/collection.d.ts.map +1 -0
  12. package/dist/collection.js +351 -0
  13. package/dist/collection.js.map +1 -0
  14. package/dist/crud.d.ts +46 -0
  15. package/dist/crud.d.ts.map +1 -0
  16. package/dist/crud.js +527 -0
  17. package/dist/crud.js.map +1 -0
  18. package/dist/describe.d.ts +89 -0
  19. package/dist/describe.d.ts.map +1 -0
  20. package/dist/describe.js +119 -0
  21. package/dist/describe.js.map +1 -0
  22. package/dist/errors.d.ts +183 -0
  23. package/dist/errors.d.ts.map +1 -0
  24. package/dist/errors.js +150 -0
  25. package/dist/errors.js.map +1 -0
  26. package/dist/family.d.ts +80 -0
  27. package/dist/family.d.ts.map +1 -0
  28. package/dist/family.js +227 -0
  29. package/dist/family.js.map +1 -0
  30. package/dist/index.d.ts +14 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +10 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/marker.d.ts +15 -0
  35. package/dist/marker.d.ts.map +1 -0
  36. package/dist/marker.js +28 -0
  37. package/dist/marker.js.map +1 -0
  38. package/dist/mask.d.ts +69 -0
  39. package/dist/mask.d.ts.map +1 -0
  40. package/dist/mask.js +277 -0
  41. package/dist/mask.js.map +1 -0
  42. package/dist/populate.d.ts +39 -0
  43. package/dist/populate.d.ts.map +1 -0
  44. package/dist/populate.js +72 -0
  45. package/dist/populate.js.map +1 -0
  46. package/dist/relations.d.ts +144 -0
  47. package/dist/relations.d.ts.map +1 -0
  48. package/dist/relations.js +144 -0
  49. package/dist/relations.js.map +1 -0
  50. package/dist/repo.d.ts +99 -0
  51. package/dist/repo.d.ts.map +1 -0
  52. package/dist/repo.js +92 -0
  53. package/dist/repo.js.map +1 -0
  54. package/dist/sort.d.ts +86 -0
  55. package/dist/sort.d.ts.map +1 -0
  56. package/dist/sort.js +187 -0
  57. package/dist/sort.js.map +1 -0
  58. package/dist/store/conformance.d.ts +15 -0
  59. package/dist/store/conformance.d.ts.map +1 -0
  60. package/dist/store/conformance.js +811 -0
  61. package/dist/store/conformance.js.map +1 -0
  62. package/dist/store/index.d.ts +9 -0
  63. package/dist/store/index.d.ts.map +1 -0
  64. package/dist/store/index.js +8 -0
  65. package/dist/store/index.js.map +1 -0
  66. package/dist/store.d.ts +119 -0
  67. package/dist/store.d.ts.map +1 -0
  68. package/dist/store.js +176 -0
  69. package/dist/store.js.map +1 -0
  70. package/dist/tenancy.d.ts +66 -0
  71. package/dist/tenancy.d.ts.map +1 -0
  72. package/dist/tenancy.js +69 -0
  73. package/dist/tenancy.js.map +1 -0
  74. package/dist/tracing.d.ts +12 -0
  75. package/dist/tracing.d.ts.map +1 -0
  76. package/dist/tracing.js +27 -0
  77. package/dist/tracing.js.map +1 -0
  78. package/dist/where.d.ts +95 -0
  79. package/dist/where.d.ts.map +1 -0
  80. package/dist/where.js +308 -0
  81. package/dist/where.js.map +1 -0
  82. package/package.json +80 -2
  83. package/skills/lattice/SKILL.md +106 -0
  84. package/skills/lattice/references/access.md +90 -0
  85. package/skills/lattice/references/cli.md +87 -0
  86. package/skills/lattice/references/collection.md +94 -0
  87. package/skills/lattice/references/event-handler.md +88 -0
  88. package/skills/lattice/references/masking.md +72 -0
  89. package/skills/lattice/references/method.md +88 -0
  90. package/skills/lattice/references/override.md +160 -0
  91. package/skills/lattice/references/relations.md +91 -0
  92. package/skills/lattice/references/tenancy.md +82 -0
  93. package/skills/lattice/references/testing.md +75 -0
  94. package/src/access.ts +449 -0
  95. package/src/build.ts +1888 -0
  96. package/src/collection.ts +1025 -0
  97. package/src/crud.ts +797 -0
  98. package/src/describe.ts +232 -0
  99. package/src/errors.ts +174 -0
  100. package/src/family.ts +334 -0
  101. package/src/index.ts +44 -0
  102. package/src/marker.ts +54 -0
  103. package/src/mask.ts +336 -0
  104. package/src/populate.ts +103 -0
  105. package/src/relations.ts +370 -0
  106. package/src/repo.ts +218 -0
  107. package/src/sort.ts +271 -0
  108. package/src/store/conformance.ts +1179 -0
  109. package/src/store/index.ts +22 -0
  110. package/src/store.ts +315 -0
  111. package/src/tenancy.ts +124 -0
  112. package/src/tracing.ts +29 -0
  113. package/src/where.ts +463 -0
@@ -0,0 +1,95 @@
1
+ import { z } from 'zod';
2
+ export type Kind = 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'other';
3
+ /** The field kinds each operator applies to. `Where` and the validator both read it. */
4
+ declare const operators: {
5
+ readonly eq: readonly ["string", "number", "boolean", "enum"];
6
+ readonly ne: readonly ["string", "number", "boolean", "enum"];
7
+ readonly in: readonly ["string", "number", "boolean", "enum"];
8
+ readonly nin: readonly ["string", "number", "boolean", "enum"];
9
+ readonly gt: readonly ["string", "number"];
10
+ readonly gte: readonly ["string", "number"];
11
+ readonly lt: readonly ["string", "number"];
12
+ readonly lte: readonly ["string", "number"];
13
+ readonly contains: readonly ["string"];
14
+ readonly has: readonly ["array"];
15
+ readonly exists: readonly ["string", "number", "boolean", "enum", "array", "other"];
16
+ };
17
+ type Operator = keyof typeof operators;
18
+ /** A brand is type-only: a branded string is a string at runtime. */
19
+ type Unbrand<T> = T extends z.core.$brand<infer Brand> ? T extends infer U & z.core.$brand<Brand> ? U : T : T;
20
+ /**
21
+ * A field's kind, from its value type. `kindOf` below makes the same
22
+ * decision from the schema: all strings, with an open `string` among them,
23
+ * is 'string'; only string or only number literals is 'enum'; and so on.
24
+ * The table test in where.test.ts checks both sides on every field form.
25
+ */
26
+ export type KindOf<V> = [Unbrand<Exclude<V, null | undefined>>] extends [
27
+ infer B
28
+ ] ? [B] extends [never] ? 'other' : [B] extends [boolean] ? 'boolean' : [B] extends [string] ? string extends B ? 'string' : 'enum' : [B] extends [number] ? number extends B ? 'number' : 'enum' : [B] extends [readonly (infer E)[]] ? KindOf<E> extends 'string' | 'number' | 'enum' ? 'array' : 'other' : 'other' : never;
29
+ type Operand<Op extends Operator, B> = Op extends 'eq' | 'ne' ? B | null : Op extends 'in' | 'nin' ? readonly (B | null)[] : Op extends 'contains' ? string : Op extends 'exists' ? boolean : Op extends 'has' ? B extends readonly (infer E)[] ? Exclude<E, null | undefined> : never : B;
30
+ /** The operators one field allows, typed from its value. */
31
+ export type FieldWhere<V> = {
32
+ readonly [Op in Operator as KindOf<V> extends (typeof operators)[Op][number] ? Op : never]?: Operand<Op, Exclude<V, null | undefined>>;
33
+ };
34
+ /**
35
+ * A filter on a collection's documents. Several fields, or several operators
36
+ * on one field, all have to match. `and` and `or` take one or more filters.
37
+ */
38
+ export type Where<Doc> = {
39
+ readonly [K in keyof Doc]?: FieldWhere<Doc[K]>;
40
+ } & {
41
+ readonly and?: readonly Where<Doc>[];
42
+ readonly or?: readonly Where<Doc>[];
43
+ };
44
+ /**
45
+ * A wrapper that keeps its inner schema's values (optional, nullable,
46
+ * default, prefault, nonoptional, readonly, catch): its inner schema, and the
47
+ * same wrapper around another one. Undefined for any other schema.
48
+ */
49
+ export declare function wrapperOf(schema: z.core.$ZodType): {
50
+ readonly inner: z.core.$ZodType;
51
+ readonly around: (inner: z.core.$ZodType) => z.core.$ZodType;
52
+ } | undefined;
53
+ /** The schema a wrapper keeps the values of, or undefined for any other schema. */
54
+ export declare function innerOf(schema: z.core.$ZodType): z.core.$ZodType | undefined;
55
+ /** Whether a schema's output can be null or undefined. Mirrors `null extends V`. */
56
+ export declare function nullish(schema: z.core.$ZodType): boolean;
57
+ /** A field's kind, from its schema. Mirrors `KindOf` above. */
58
+ export declare function kindOf(schema: z.core.$ZodType): Kind;
59
+ /**
60
+ * The validator of a `Where` on documents of `schema`. The overload states
61
+ * its static type, as input and output alike: a `Where` has no defaults or
62
+ * transforms. The implementation builds the schema from the shape at
63
+ * runtime, which a loop cannot express as a mapped type.
64
+ */
65
+ export declare function whereSchema<S extends z.ZodObject>(schema: S): z.ZodType<Where<z.output<S>>, Where<z.output<S>>>;
66
+ /** Parses a `Where` on documents of `schema`, or throws `VALIDATION` naming each path. */
67
+ export declare function parseWhere<S extends z.ZodObject>(schema: S, value: unknown): Where<z.output<S>>;
68
+ /** The operators of one field in a parsed `Where`. */
69
+ export type FieldOps = {
70
+ readonly eq?: unknown;
71
+ readonly ne?: unknown;
72
+ readonly in?: readonly unknown[];
73
+ readonly nin?: readonly unknown[];
74
+ readonly gt?: unknown;
75
+ readonly gte?: unknown;
76
+ readonly lt?: unknown;
77
+ readonly lte?: unknown;
78
+ readonly contains?: string;
79
+ readonly has?: unknown;
80
+ readonly exists?: boolean;
81
+ };
82
+ /** Both filters: undefined when neither is set. */
83
+ export declare function both(a: AnyWhere | undefined, b: AnyWhere | undefined): AnyWhere | undefined;
84
+ /** Any parsed `Where`, as `matches` reads it. */
85
+ export type AnyWhere = {
86
+ readonly and?: readonly AnyWhere[];
87
+ readonly or?: readonly AnyWhere[];
88
+ readonly [field: string]: FieldOps | readonly AnyWhere[] | undefined;
89
+ };
90
+ /** Strings compare by code unit, so ISO timestamps order correctly. */
91
+ export declare function compare(value: unknown, target: unknown): number | undefined;
92
+ /** Whether `doc` matches a parsed `where`. The in-memory store's filter. */
93
+ export declare function matches(doc: Readonly<Record<string, unknown>>, where: AnyWhere): boolean;
94
+ export {};
95
+ //# sourceMappingURL=where.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"where.d.ts","sourceRoot":"","sources":["../src/where.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM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AAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,mDAAmD;AACnD,wBAAgB,IAAI,CAClB,CAAC,EAAE,QAAQ,GAAG,SAAS,EACvB,CAAC,EAAE,QAAQ,GAAG,SAAS,GACtB,QAAQ,GAAG,SAAS,CAItB;AAED,iDAAiD;AACjD,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;IACnC,QAAQ,CAAC,EAAE,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAC;IAClC,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,QAAQ,EAAE,GAAG,SAAS,CAAC;CACtE,CAAC;AAeF,uEAAuE;AACvE,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAQ3E;AA+CD,4EAA4E;AAC5E,wBAAgB,OAAO,CACrB,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EACtC,KAAK,EAAE,QAAQ,GACd,OAAO,CAeT"}
package/dist/where.js ADDED
@@ -0,0 +1,308 @@
1
+ import { z } from 'zod';
2
+ import { refusedWhere } from './describe.js';
3
+ import { invalid } from './errors.js';
4
+ import { markOf } from './marker.js';
5
+ const equality = ['string', 'number', 'boolean', 'enum'];
6
+ const ordered = ['string', 'number'];
7
+ /** The field kinds each operator applies to. `Where` and the validator both read it. */
8
+ const operators = {
9
+ eq: equality,
10
+ ne: equality,
11
+ in: equality,
12
+ nin: equality,
13
+ gt: ordered,
14
+ gte: ordered,
15
+ lt: ordered,
16
+ lte: ordered,
17
+ contains: ['string'],
18
+ // An array of strings, numbers or enum values: has one equal element.
19
+ has: ['array'],
20
+ exists: ['string', 'number', 'boolean', 'enum', 'array', 'other'],
21
+ };
22
+ /**
23
+ * A wrapper that keeps its inner schema's values (optional, nullable,
24
+ * default, prefault, nonoptional, readonly, catch): its inner schema, and the
25
+ * same wrapper around another one. Undefined for any other schema.
26
+ */
27
+ export function wrapperOf(schema) {
28
+ if (schema instanceof z.core.$ZodOptional ||
29
+ schema instanceof z.core.$ZodNullable ||
30
+ schema instanceof z.core.$ZodDefault ||
31
+ schema instanceof z.core.$ZodPrefault ||
32
+ schema instanceof z.core.$ZodNonOptional ||
33
+ schema instanceof z.core.$ZodReadonly ||
34
+ schema instanceof z.core.$ZodCatch) {
35
+ const { def } = schema._zod;
36
+ return {
37
+ inner: def.innerType,
38
+ around: (inner) => z.core.util.clone(schema, { ...def, innerType: inner }),
39
+ };
40
+ }
41
+ return undefined;
42
+ }
43
+ /** The schema a wrapper keeps the values of, or undefined for any other schema. */
44
+ export function innerOf(schema) {
45
+ return wrapperOf(schema)?.inner;
46
+ }
47
+ function unwrap(schema) {
48
+ const inner = innerOf(schema);
49
+ return inner === undefined ? schema : unwrap(inner);
50
+ }
51
+ function valueAtoms(value) {
52
+ switch (typeof value) {
53
+ case 'string':
54
+ return ['stringLiteral'];
55
+ case 'number':
56
+ return ['numberLiteral'];
57
+ case 'boolean':
58
+ return ['boolean'];
59
+ case 'undefined':
60
+ return ['undefined'];
61
+ default:
62
+ return value === null ? ['null'] : ['other'];
63
+ }
64
+ }
65
+ const notUndefined = (atom) => atom !== 'undefined';
66
+ /**
67
+ * The kinds of value a schema's output can hold. Each wrapper changes them
68
+ * the way it changes the output type: optional adds undefined, nullable adds
69
+ * null, default, prefault and nonoptional remove undefined.
70
+ */
71
+ function atoms(schema) {
72
+ if (schema instanceof z.core.$ZodOptional) {
73
+ return ['undefined', ...atoms(schema._zod.def.innerType)];
74
+ }
75
+ if (schema instanceof z.core.$ZodNullable) {
76
+ return ['null', ...atoms(schema._zod.def.innerType)];
77
+ }
78
+ if (schema instanceof z.core.$ZodDefault ||
79
+ schema instanceof z.core.$ZodPrefault ||
80
+ schema instanceof z.core.$ZodNonOptional) {
81
+ return atoms(schema._zod.def.innerType).filter(notUndefined);
82
+ }
83
+ const inner = innerOf(schema);
84
+ if (inner !== undefined)
85
+ return atoms(inner);
86
+ if (schema instanceof z.core.$ZodUnion) {
87
+ return schema._zod.def.options.flatMap(atoms);
88
+ }
89
+ switch (schema._zod.def.type) {
90
+ case 'string':
91
+ return ['string'];
92
+ case 'number':
93
+ return ['number'];
94
+ case 'boolean':
95
+ return ['boolean'];
96
+ case 'template_literal':
97
+ return ['stringLiteral'];
98
+ case 'enum':
99
+ case 'literal':
100
+ return [...(schema._zod.values ?? [])].flatMap(valueAtoms);
101
+ case 'null':
102
+ return ['null'];
103
+ case 'undefined':
104
+ case 'void':
105
+ return ['undefined'];
106
+ default:
107
+ return ['other'];
108
+ }
109
+ }
110
+ /** Whether a schema's output can be null or undefined. Mirrors `null extends V`. */
111
+ export function nullish(schema) {
112
+ return atoms(schema).some((atom) => atom === 'null' || atom === 'undefined');
113
+ }
114
+ /** The element schema of an array field, through its wrappers. */
115
+ function elementOf(schema) {
116
+ const inner = innerOf(schema);
117
+ if (inner !== undefined)
118
+ return elementOf(inner);
119
+ return schema instanceof z.core.$ZodArray
120
+ ? schema._zod.def.element
121
+ : undefined;
122
+ }
123
+ /** A field's kind, from its schema. Mirrors `KindOf` above. */
124
+ export function kindOf(schema) {
125
+ const element = elementOf(schema);
126
+ if (element !== undefined) {
127
+ const kind = kindOf(element);
128
+ return kind === 'string' || kind === 'number' || kind === 'enum'
129
+ ? 'array'
130
+ : 'other';
131
+ }
132
+ const found = atoms(schema).filter((atom) => atom !== 'null' && atom !== 'undefined');
133
+ const only = (...allowed) => found.every((atom) => allowed.includes(atom));
134
+ if (found.length === 0)
135
+ return 'other';
136
+ if (only('boolean'))
137
+ return 'boolean';
138
+ if (only('string', 'stringLiteral')) {
139
+ return found.includes('string') ? 'string' : 'enum';
140
+ }
141
+ if (only('number', 'numberLiteral')) {
142
+ return found.includes('number') ? 'number' : 'enum';
143
+ }
144
+ return 'other';
145
+ }
146
+ /** The schema of a single value an operator compares against. */
147
+ function valueSchema(kind, leaf) {
148
+ switch (kind) {
149
+ case 'string':
150
+ return z.string();
151
+ case 'number':
152
+ return z.number();
153
+ case 'boolean':
154
+ return z.boolean();
155
+ case 'enum':
156
+ return leaf;
157
+ default:
158
+ // 'other' fields only take `exists`, which reads no value.
159
+ return z.never();
160
+ }
161
+ }
162
+ function operandSchema(op, value) {
163
+ switch (op) {
164
+ case 'eq':
165
+ case 'ne':
166
+ return z.nullable(value);
167
+ case 'in':
168
+ case 'nin':
169
+ return z.array(z.nullable(value));
170
+ case 'contains':
171
+ return z.string();
172
+ case 'exists':
173
+ return z.boolean();
174
+ default:
175
+ return value;
176
+ }
177
+ }
178
+ function fieldSchema(field) {
179
+ const kind = kindOf(field);
180
+ // An enum's values are checked by its own schema, without wrappers such as
181
+ // catch, which would accept anything. An array's operand is one element.
182
+ const element = elementOf(field);
183
+ const value = element === undefined
184
+ ? valueSchema(kind, unwrap(field))
185
+ : valueSchema(kindOf(element), unwrap(element));
186
+ return z.strictObject(Object.fromEntries(Object.entries(operators)
187
+ .filter(([, kinds]) => kinds.some((k) => k === kind))
188
+ .map(([op]) => [op, z.optional(operandSchema(op, value))])));
189
+ }
190
+ export function whereSchema(schema) {
191
+ const fields = Object.fromEntries(Object.entries(schema.shape).map(([name, field]) => {
192
+ const entry = z.optional(fieldSchema(field));
193
+ // The family guard refuses it for a caller who may not read it; the
194
+ // description reaches JSON Schema, so OpenAPI and MCP say so.
195
+ return [
196
+ name,
197
+ markOf(field) === undefined
198
+ ? entry
199
+ : entry.describe(refusedWhere(name)),
200
+ ];
201
+ }));
202
+ const clauses = z.optional(z.array(z.lazy(() => where)).min(1));
203
+ const where = z.strictObject({
204
+ ...fields,
205
+ and: clauses,
206
+ or: clauses,
207
+ });
208
+ return where;
209
+ }
210
+ /** Parses a `Where` on documents of `schema`, or throws `VALIDATION` naming each path. */
211
+ export function parseWhere(schema, value) {
212
+ const result = whereSchema(schema).safeParse(value);
213
+ if (result.success)
214
+ return result.data;
215
+ throw invalid(result.error.issues);
216
+ }
217
+ /** Both filters: undefined when neither is set. */
218
+ export function both(a, b) {
219
+ if (a === undefined)
220
+ return b;
221
+ if (b === undefined)
222
+ return a;
223
+ return { and: [a, b] };
224
+ }
225
+ // Null and missing follow MongoDB's query semantics, so MongoStore can pass a
226
+ // Where through unchanged. `eq: null` matches null or missing; `ne` and `nin`
227
+ // match a missing field; a comparison with null or missing is false. The one
228
+ // difference is `exists`: it asks whether the field has a value, so null does
229
+ // not exist (Mongo `{ $ne: null }` and `{ $eq: null }`).
230
+ function present(value) {
231
+ return value !== undefined && value !== null;
232
+ }
233
+ function equals(value, target) {
234
+ return target === null ? !present(value) : value === target;
235
+ }
236
+ /** Strings compare by code unit, so ISO timestamps order correctly. */
237
+ export function compare(value, target) {
238
+ if (typeof value === 'number' && typeof target === 'number') {
239
+ return value - target;
240
+ }
241
+ if (typeof value === 'string' && typeof target === 'string') {
242
+ return value < target ? -1 : value > target ? 1 : 0;
243
+ }
244
+ return undefined;
245
+ }
246
+ function compares(value, target, test) {
247
+ if (target === undefined)
248
+ return true;
249
+ const order = compare(value, target);
250
+ return order !== undefined && test(order);
251
+ }
252
+ function matchesField(value, ops) {
253
+ if (ops.eq !== undefined && !equals(value, ops.eq))
254
+ return false;
255
+ if (ops.ne !== undefined && equals(value, ops.ne))
256
+ return false;
257
+ if (ops.in !== undefined && !ops.in.some((t) => equals(value, t))) {
258
+ return false;
259
+ }
260
+ if (ops.nin?.some((t) => equals(value, t)))
261
+ return false;
262
+ if (!compares(value, ops.gt, (order) => order > 0))
263
+ return false;
264
+ if (!compares(value, ops.gte, (order) => order >= 0))
265
+ return false;
266
+ if (!compares(value, ops.lt, (order) => order < 0))
267
+ return false;
268
+ if (!compares(value, ops.lte, (order) => order <= 0))
269
+ return false;
270
+ if (ops.contains !== undefined) {
271
+ // A literal, case-insensitive substring. Never a regex.
272
+ const needle = ops.contains.toLowerCase();
273
+ if (typeof value !== 'string' || !value.toLowerCase().includes(needle)) {
274
+ return false;
275
+ }
276
+ }
277
+ // An element equal to the operand. A missing or null array has none.
278
+ if (ops.has !== undefined &&
279
+ !(Array.isArray(value) && value.some((element) => equals(element, ops.has)))) {
280
+ return false;
281
+ }
282
+ if (ops.exists !== undefined && present(value) !== ops.exists)
283
+ return false;
284
+ return true;
285
+ }
286
+ function isClauses(condition) {
287
+ return Array.isArray(condition);
288
+ }
289
+ /** Whether `doc` matches a parsed `where`. The in-memory store's filter. */
290
+ export function matches(doc, where) {
291
+ for (const [field, condition] of Object.entries(where)) {
292
+ if (condition === undefined)
293
+ continue;
294
+ if (isClauses(condition)) {
295
+ // collection() rejects fields named `and` and `or`, so only they hold arrays.
296
+ const holds = field === 'and'
297
+ ? condition.every((clause) => matches(doc, clause))
298
+ : condition.some((clause) => matches(doc, clause));
299
+ if (!holds)
300
+ return false;
301
+ continue;
302
+ }
303
+ if (!matchesField(doc[field], condition))
304
+ return false;
305
+ }
306
+ return true;
307
+ }
308
+ //# sourceMappingURL=where.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"where.js","sourceRoot":"","sources":["../src/where.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,MAAM,QAAQ,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAU,CAAC;AAClE,MAAM,OAAO,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAC;AAE9C,wFAAwF;AACxF,MAAM,SAAS,GAAG;I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{
2
2
  "name": "@latticecms/core",
3
- "version": "0.0.0",
4
- "description": "Placeholder that reserves the name. Lattice CMS releases start at 0.1.0.",
3
+ "version": "0.2.0",
4
+ "description": "Collections that compile into hexok use cases and a ts-pf contract.",
5
+ "keywords": [
6
+ "lattice",
7
+ "cms",
8
+ "headless-cms",
9
+ "collections",
10
+ "hexok",
11
+ "ts-pf",
12
+ "zod",
13
+ "rpc",
14
+ "typescript"
15
+ ],
5
16
  "license": "MIT",
17
+ "type": "module",
18
+ "sideEffects": false,
6
19
  "repository": {
7
20
  "type": "git",
8
21
  "url": "git+https://github.com/crobinson42/lattice-cms.git",
9
22
  "directory": "packages/core"
10
23
  },
11
24
  "homepage": "https://github.com/crobinson42/lattice-cms/tree/main/packages/core#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/crobinson42/lattice-cms/issues"
27
+ },
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./store": {
35
+ "types": "./dist/store/index.d.ts",
36
+ "import": "./dist/store/index.js",
37
+ "default": "./dist/store/index.js"
38
+ },
39
+ "./store/conformance": {
40
+ "types": "./dist/store/conformance.d.ts",
41
+ "import": "./dist/store/conformance.js",
42
+ "default": "./dist/store/conformance.js"
43
+ },
44
+ "./package.json": "./package.json"
45
+ },
46
+ "files": [
47
+ "dist",
48
+ "skills",
49
+ "src",
50
+ "!src/**/*.test.ts",
51
+ "!src/fixtures"
52
+ ],
12
53
  "publishConfig": {
13
54
  "access": "public"
55
+ },
56
+ "engines": {
57
+ "node": ">=22.11"
58
+ },
59
+ "scripts": {
60
+ "build": "tsc -b",
61
+ "test": "vitest run",
62
+ "type-check": "tsc --noEmit -p tsconfig.check.json && tsc -p tsconfig.dist.json",
63
+ "lint": "biome check .",
64
+ "clean": "rm -rf dist *.tsbuildinfo",
65
+ "type-cost": "node scripts/type-cost.mjs"
66
+ },
67
+ "peerDependencies": {
68
+ "@hexok/logger": "^0.2.0",
69
+ "@opentelemetry/api": "^1.3.0",
70
+ "@ts-pf/contract": "^1.0.0",
71
+ "@ts-pf/docs": "^1.0.0",
72
+ "@ts-pf/mcp": "^1.0.0",
73
+ "@ts-pf/protocol": "^1.0.0",
74
+ "@ts-pf/server": "^1.0.0",
75
+ "hexok": "^2.2.8",
76
+ "zod": "^4.1.0"
77
+ },
78
+ "devDependencies": {
79
+ "@hexok/logger": "^0.2.0",
80
+ "@opentelemetry/api": "~1.9.1",
81
+ "@opentelemetry/context-async-hooks": "^2.12.1",
82
+ "@opentelemetry/sdk-trace-base": "^2.12.1",
83
+ "@ts-pf/client": "^1.0.1",
84
+ "@ts-pf/contract": "^1.0.1",
85
+ "@ts-pf/docs": "^1.0.1",
86
+ "@ts-pf/mcp": "^1.0.1",
87
+ "@ts-pf/protocol": "^1.0.1",
88
+ "@ts-pf/server": "^1.0.1",
89
+ "@ts-pf/server-http": "^1.0.1",
90
+ "hexok": "^2.2.8",
91
+ "zod": "^4.1.0"
14
92
  }
15
93
  }
@@ -0,0 +1,106 @@
1
+ ---
2
+ name: lattice
3
+ description: >
4
+ Build features in an app made with Lattice CMS (@latticecms/core). Use when
5
+ the task adds or changes a collection, its access, tenancy, relations,
6
+ masked fields, an override, a method, an event handler on a collection's
7
+ events, a data migration, or a test of any of them. Follow its steps, and
8
+ read the reference it names before writing that piece.
9
+ license: MIT
10
+ metadata:
11
+ short-description: Build features with Lattice collections
12
+ ---
13
+
14
+ # Lattice
15
+
16
+ Import from `@latticecms/core`. A collection is a slug and a Zod schema. `framework()` compiles each one into hexok use cases, hexok events and a ts-pf contract, with `find`, `count`, `get`, `create`, `update` and `delete`. Every generated piece is a real hexok `UseCase` and `Event`, and a real ts-pf procedure, so hand-written code takes its place with the same types.
17
+
18
+ The app owns authentication, HTTP, the store's lifecycle, migrations' timing and the worker. Lattice owns the collection's procedures, its access and tenant filters, masking, population and its events.
19
+
20
+ The running shape is `examples/crm`. Companies, contacts and deals belong to organizations. A deal refers to a company, links contacts through a relation, and hides its amount from members. A deal closes once through a method, and its delete is an override that archives it. An activity is logged through a method that stamps its author. Use the app's real story. The crm is only the shape.
21
+
22
+ ## Concepts
23
+
24
+ Read the reference before writing that concept.
25
+
26
+ | Concept | Reference | Place |
27
+ | --- | --- | --- |
28
+ | Collection | [references/collection.md](references/collection.md) | Domain. A slug, a schema, a tenant, a label and indexes. |
29
+ | Access | [references/access.md](references/access.md) | Composition. Who may do what, as a filter in the query. |
30
+ | Tenancy | [references/tenancy.md](references/tenancy.md) | Family and collection. Each organization's rows kept apart. |
31
+ | Relations | [references/relations.md](references/relations.md) | Domain. `ref()`, `many()`, `onDelete` and population. |
32
+ | Masking | [references/masking.md](references/masking.md) | Domain and composition. Fields a caller's rule withholds. |
33
+ | Override | [references/override.md](references/override.md) | Application. A hand-written use case in place of a generated one. |
34
+ | Method | [references/method.md](references/method.md) | Application. A procedure of the collection's own, such as `deals.close`. |
35
+ | Event handler | [references/event-handler.md](references/event-handler.md) | Application. Work that follows a collection's event or the app's own. |
36
+ | Testing | [references/testing.md](references/testing.md) | Tests. `build()`, `createLocalClient` and `MemoryStore`, and the store suite. |
37
+ | CLI | [references/cli.md](references/cli.md) | Tooling. `lattice collection`, `migration` and `migrate`. |
38
+
39
+ ## How to build a feature
40
+
41
+ Walk these steps before you edit files.
42
+
43
+ 1. **Thought.** State one user story: who acts, and what they can do. Example: a manager closes a deal as won.
44
+ 2. **Research.** Find what exists: the collection, its entry in the composition (`framework(...).collection(...)`), the family, the events catalog, the overrides and methods, the handlers and the tests.
45
+ 3. **Plan.** Write the steps below, each marked `exists`, `add` or `none`. Then implement them in that order. Read each step's reference first.
46
+
47
+ ### 1. The collection
48
+
49
+ A story that stores a new kind of thing is a collection. Run `lattice collection <slug>` in the app: it writes the file, a test, the registration and the events catalog entries. Then edit the schema. Read [references/collection.md](references/collection.md) and [references/cli.md](references/cli.md).
50
+
51
+ A new field on an existing collection is a schema change. When stored documents lack it, add a data migration with `lattice migration <name>`.
52
+
53
+ ### 2. Access
54
+
55
+ Every collection has an `access` map at composition. A scaffolded collection is open to any signed-in caller. Tighten it to the story. Read [references/access.md](references/access.md).
56
+
57
+ ### 3. Tenancy
58
+
59
+ A collection whose rows belong to an organization has `tenant: 'organizationId'`. One every organization shares, such as the organizations themselves, has none. Read [references/tenancy.md](references/tenancy.md).
60
+
61
+ ### 4. Relations
62
+
63
+ A field that holds another document's id is a `ref()`. A list of links with fields of their own, such as a contact's role on a deal, is a `many()` relation. Decide what deleting the parent does. Read [references/relations.md](references/relations.md).
64
+
65
+ ### 5. Masking
66
+
67
+ A field some callers may not read, such as a deal's amount, is `maskable`, with a read rule at composition. Read [references/masking.md](references/masking.md).
68
+
69
+ ### 6. Overrides and methods
70
+
71
+ When a generated operation is wrong for the story, override it: a delete that archives, or an update that refuses to close a deal and then runs the generated update. When the story is an action of its own, add a method: closing a deal. Both are hand-written use cases in the app's family. Read [references/override.md](references/override.md) and [references/method.md](references/method.md).
72
+
73
+ ### 7. Events and handlers
74
+
75
+ Generated writes publish the collection's events. Overrides and methods publish their own. Work that follows a write, in this product, is an event handler. Work for another product or a customer is a mapping at the edge. Read [references/event-handler.md](references/event-handler.md).
76
+
77
+ ### 8. Tests
78
+
79
+ Test the story through the contract, with `createLocalClient` over the built app. Read [references/testing.md](references/testing.md).
80
+
81
+ ## What Lattice does not do
82
+
83
+ Use hexok and ts-pf directly for these, with their skills (`hexok`, `ts-pf-*`).
84
+
85
+ - **Authentication.** The edge proves the caller with `@hexok/auth` and builds the actor. Use cases receive the actor and never import auth or HTTP.
86
+ - **Serving.** `@ts-pf/server-http`'s `FetchHandler` serves the built `app`. The context function is the app's.
87
+ - **Clients, MCP and OpenAPI.** `@ts-pf/client`, `@ts-pf/mcp`, `@ts-pf/openapi` and `@ts-pf/codegen` read the contract. Lattice gives each procedure its summary and effect.
88
+ - **Delivery.** Queues, workers, webhooks and integration events are hexok's packages. Lattice publishes collection events to the app's publisher, and nothing more.
89
+ - **Read events and pattern subscriptions.** Reads publish nothing. A handler names its event.
90
+ - **An outbox.** A publish after a write can be lost in a crash.
91
+ - **File uploads.** Not built yet.
92
+
93
+ ## Conventions
94
+
95
+ - One family per app. Generated and hand-written use cases extend it.
96
+ - Access and tenancy are filters in the query. Never load a row and then decide whether the caller may see it.
97
+ - Every `Repo` call takes a scope: `ctx.access.scope(C, action)` in a use case, `tenancy.scope(C, tenantId)` in a handler, `unscoped` in trusted code that acts for no tenant.
98
+ - No casts, no `any`, no `!`. A type error from Lattice names the rule; fix the code, not the type.
99
+ - No parallel primitive. A use case is a hexok `UseCase` from the family. An event is a hexok `Event` in the app's one catalog. A procedure comes from the framework.
100
+ - The tenant field is stamped, never written. It is not in create or update input.
101
+ - A collection event stays inside the product. Map it to an integration event or a webhook at the edge.
102
+ - Generated code reads the stored shape. A migration reads the raw database, because old documents have the old shape.
103
+
104
+ ## When Lattice changes
105
+
106
+ A change to a concept updates its reference in the same change. Each code block here quotes a file of `examples/crm` and names it, and the crm's tests fail when a quote no longer matches.