@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
package/dist/crud.js ADDED
@@ -0,0 +1,527 @@
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import { CallAccess } from './access.js';
3
+ import { maxLinks } from './collection.js';
4
+ import { LatticeError, parse } from './errors.js';
5
+ import { refuseWithheld } from './mask.js';
6
+ import { linkFields } from './populate.js';
7
+ import { findQuery, stamped, touched } from './repo.js';
8
+ import { tenantClause, withoutTenant } from './tenancy.js';
9
+ import { both } from './where.js';
10
+ /**
11
+ * The filter that limits what a call may see and change (0005): the call's
12
+ * access scope. Asked here, so a body called from another operation's
13
+ * override is still checked (0036); after the family's guard, it reads the
14
+ * kept answer. Create has no row to filter: its body asks with its data.
15
+ */
16
+ async function scopeOf(ctx, collection, action) {
17
+ if (action === 'create')
18
+ return undefined;
19
+ return (await ctx.access.scope(collection, action)).where;
20
+ }
21
+ /** `tenantId` for an event payload, on a tenanted collection only. */
22
+ function tenantPayload(call) {
23
+ return call.tenantId === undefined ? {} : { tenantId: call.tenantId };
24
+ }
25
+ function notFound(call, id) {
26
+ return LatticeError.NOT_FOUND({ collection: call.collection.slug, id });
27
+ }
28
+ /** A stored doc as the collection's document type: it has its timestamps. */
29
+ function documentOf(call, doc) {
30
+ const { createdAt, updatedAt } = doc;
31
+ if (typeof createdAt !== 'string' || typeof updatedAt !== 'string') {
32
+ throw new Error(`Lattice: "${call.collection.slug}.${doc.id}" has no createdAt or updatedAt`);
33
+ }
34
+ return { ...doc, createdAt, updatedAt };
35
+ }
36
+ /** A document's own tenant, for a tenanted collection. */
37
+ function tenantOfDoc(collection, doc) {
38
+ const value = collection.tenant === undefined ? undefined : doc[collection.tenant];
39
+ return typeof value === 'string' ? value : undefined;
40
+ }
41
+ function keyOf(node) {
42
+ return JSON.stringify([node.collection.slug, node.doc.id]);
43
+ }
44
+ /** Every document of `collection` matching `where`, page by page. */
45
+ async function* every(store, collection, where) {
46
+ let after;
47
+ do {
48
+ const page = await store.find(findQuery(collection, { where, sort: ['id'], limit: 100, after }));
49
+ yield* page.docs;
50
+ after = page.next ?? undefined;
51
+ } while (after !== undefined);
52
+ }
53
+ /**
54
+ * The onDelete plan of deleting `root`, through every ref to it, and to
55
+ * each cascaded document in turn. Restrict throws REFERENCED here, before
56
+ * anything is written. These are system steps: they skip access functions,
57
+ * but each carries the tenant of the document it starts from, so a row of
58
+ * another tenant that stores this id is neither counted nor touched. A
59
+ * global document's children are found across tenants.
60
+ */
61
+ async function planDelete(store, referrers, root) {
62
+ const plan = { deletes: [], nullifies: [] };
63
+ const visited = new Set([keyOf(root)]);
64
+ const queue = [root];
65
+ for (let node = queue.shift(); node !== undefined; node = queue.shift()) {
66
+ const { id } = node.doc;
67
+ const tenantId = tenantOfDoc(node.collection, node.doc);
68
+ for (const { collection, ref, relation } of referrers.get(node.collection.slug) ?? []) {
69
+ const where = both({ [ref.field]: ref.array ? { has: id } : { eq: id } }, tenantClause(collection, tenantId)) ?? {};
70
+ if (ref.onDelete === 'restrict') {
71
+ if ((await store.count(collection.slug, where)) > 0) {
72
+ throw LatticeError.REFERENCED({
73
+ collection: node.collection.slug,
74
+ id,
75
+ by: {
76
+ collection: collection.slug,
77
+ field: ref.field,
78
+ ...(relation === undefined ? {} : { relation }),
79
+ },
80
+ });
81
+ }
82
+ continue;
83
+ }
84
+ for await (const doc of every(store, collection, where)) {
85
+ const child = { collection, doc };
86
+ if (ref.onDelete === 'nullify') {
87
+ plan.nullifies.push({ ...child, ref, id });
88
+ }
89
+ else if (!visited.has(keyOf(child))) {
90
+ // A cycle meets a document already planned: stop there.
91
+ visited.add(keyOf(child));
92
+ plan.deletes.push(child);
93
+ queue.push(child);
94
+ }
95
+ }
96
+ }
97
+ }
98
+ return plan;
99
+ }
100
+ /**
101
+ * Applies a plan in `store`, returning the events it owes. Clears refs
102
+ * first, then deletes the cascaded documents, the deepest first, so a
103
+ * store without transactions ends with the parent, deleted last by the
104
+ * caller.
105
+ */
106
+ async function applyPlan(store, call, plan) {
107
+ const events = [];
108
+ const deleted = new Set(plan.deletes.map(keyOf));
109
+ for (const { collection, doc, ref, id } of plan.nullifies) {
110
+ if (deleted.has(keyOf({ collection, doc })))
111
+ continue;
112
+ // Read again: an earlier step may have changed this document.
113
+ const current = await store.get(collection.slug, doc.id);
114
+ if (current === null)
115
+ continue;
116
+ const value = current[ref.field];
117
+ const cleared = ref.array
118
+ ? Array.isArray(value)
119
+ ? value.filter((element) => element !== id)
120
+ : value
121
+ : ref.cleared;
122
+ const updated = await store.update(collection.slug, doc.id, touched({ [ref.field]: cleared }));
123
+ if (updated === null)
124
+ continue;
125
+ const tenantId = tenantOfDoc(collection, updated);
126
+ events.push(new collection.events.updated({
127
+ doc: documentOf({ collection }, updated),
128
+ previous: documentOf({ collection }, current),
129
+ actorId: call.actorId,
130
+ ...(tenantId === undefined ? {} : { tenantId }),
131
+ }));
132
+ }
133
+ for (const { collection, doc } of [...plan.deletes].reverse()) {
134
+ const removed = await store.remove(collection.slug, doc.id);
135
+ if (removed === null)
136
+ continue;
137
+ const tenantId = tenantOfDoc(collection, removed);
138
+ events.push(new collection.events.deleted({
139
+ doc: documentOf({ collection }, removed),
140
+ actorId: call.actorId,
141
+ ...(tenantId === undefined ? {} : { tenantId }),
142
+ }));
143
+ }
144
+ return events;
145
+ }
146
+ async function read(call, doc) {
147
+ const document = documentOf(call, doc);
148
+ const { afterRead } = call.collection;
149
+ return afterRead === undefined ? document : afterRead({ doc: document });
150
+ }
151
+ /** The ids a ref field holds: one, a list, or none. */
152
+ function idsIn(value) {
153
+ return (Array.isArray(value) ? value : [value]).filter((id) => typeof id === 'string');
154
+ }
155
+ /**
156
+ * The documents of `slug` among `ids` that the caller may read, by id: one
157
+ * getMany under the caller's read scope there, which carries the tenant,
158
+ * then each through its afterRead. A `false` read access withholds them
159
+ * all rather than failing the read that populates them.
160
+ */
161
+ async function readable(call, slug, ids) {
162
+ const collection = call.collections.get(slug);
163
+ if (collection === undefined) {
164
+ throw new Error(`Lattice: no collection "${slug}" to populate`);
165
+ }
166
+ const scope = await call.access.scope(collection, 'read').catch((error) => {
167
+ if (LatticeError.is(error) && error.code === 'FORBIDDEN')
168
+ return null;
169
+ throw error;
170
+ });
171
+ if (scope === null)
172
+ return new Map();
173
+ const stored = await call.store.getMany(slug, ids, scope.where);
174
+ return new Map(await Promise.all(stored.map(async (doc) => [
175
+ doc.id,
176
+ await read({ collection }, doc),
177
+ ])));
178
+ }
179
+ /**
180
+ * `docs` of `collection` with the ref fields in `keys` populated (0022).
181
+ * Ids are collected per target collection across all of `docs`, and each
182
+ * target is read once. A target the caller may not read, or one that no
183
+ * longer exists, is `{ id }`. A list of ids populates element-wise.
184
+ */
185
+ async function populated(call, collection, keys, docs) {
186
+ const refs = collection.refs.filter((ref) => keys.includes(ref.field));
187
+ if (refs.length === 0)
188
+ return [...docs];
189
+ const wanted = new Map();
190
+ for (const doc of docs) {
191
+ for (const ref of refs) {
192
+ const ids = wanted.get(ref.slug) ?? new Set();
193
+ for (const id of idsIn(doc[ref.field]))
194
+ ids.add(id);
195
+ wanted.set(ref.slug, ids);
196
+ }
197
+ }
198
+ const found = new Map(await Promise.all([...wanted].map(async ([slug, ids]) => [slug, await readable(call, slug, [...ids])])));
199
+ return docs.map((doc) => {
200
+ const next = { ...doc };
201
+ for (const ref of refs) {
202
+ const targets = found.get(ref.slug);
203
+ const swap = (id) => typeof id === 'string' ? (targets?.get(id) ?? { id }) : id;
204
+ const value = doc[ref.field];
205
+ next[ref.field] = Array.isArray(value) ? value.map(swap) : swap(value);
206
+ }
207
+ return next;
208
+ });
209
+ }
210
+ async function change(call, args) {
211
+ const { beforeChange } = call.collection;
212
+ return beforeChange === undefined ? args.data : beforeChange(args);
213
+ }
214
+ /**
215
+ * Each operation's body. Each parses its own input: the family guard has
216
+ * rejected bad input, but hexok hands execute the raw value, so parsing here
217
+ * strips unknown keys and applies the create schema's defaults.
218
+ */
219
+ /*
220
+ * Each write publishes its event after the store write succeeds, so a failed
221
+ * write publishes nothing. A publish that fails reaches the caller although
222
+ * the write stays: the delivery gap architecture.md accepts until the outbox.
223
+ */
224
+ /**
225
+ * `document` with the `many()` relations in `keys` populated, as `get`
226
+ * does: each relation's links in one find on its join collection, inside
227
+ * the parent's tenant, then their targets in one getMany. `set` keeps a
228
+ * parent to at most 1000 links, so one find holds them all. The links run
229
+ * under the parent's read, as `list` does, which the get has passed.
230
+ */
231
+ async function withLinks(call, collection, keys, parent, document) {
232
+ const next = { ...document };
233
+ for (const [name, relation] of Object.entries(collection.relations)) {
234
+ if (!keys.includes(name))
235
+ continue;
236
+ const join = relation.through;
237
+ const where = both({ [relation.parentField]: { eq: parent.id } }, tenantClause(join, tenantOfDoc(collection, parent)));
238
+ const { docs } = await call.store.find(findQuery(join, { where, limit: maxLinks }));
239
+ const targets = await readable(call, relation.target, docs.flatMap((link) => idsIn(link[relation.targetField])));
240
+ const fields = linkFields(relation);
241
+ next[name] = docs.map((link) => {
242
+ const id = link[relation.targetField];
243
+ return {
244
+ ...Object.fromEntries(fields
245
+ .filter((field) => link[field] !== undefined)
246
+ .map((field) => [field, link[field]])),
247
+ doc: typeof id === 'string' ? (targets.get(id) ?? { id }) : id,
248
+ };
249
+ });
250
+ }
251
+ return next;
252
+ }
253
+ const operations = {
254
+ async find(call, raw) {
255
+ const { where, sort, limit, after } = parse(call.ops.find.input, raw);
256
+ await refuseWithheld(call.access, call.collection, 'find', raw);
257
+ const { populate = [] } = parse(call.populate, raw);
258
+ const page = await call.store.find(findQuery(call.collection, {
259
+ where: both(where, call.scope),
260
+ sort,
261
+ limit,
262
+ after,
263
+ }));
264
+ const docs = await Promise.all(page.docs.map((doc) => read(call, doc)));
265
+ return {
266
+ docs: await populated(call, call.collection, populate, docs),
267
+ next: page.next,
268
+ };
269
+ },
270
+ async count(call, raw) {
271
+ const { where } = parse(call.ops.count.input, raw);
272
+ await refuseWithheld(call.access, call.collection, 'count', raw);
273
+ return {
274
+ count: await call.store.count(call.collection.slug, both(where, call.scope)),
275
+ };
276
+ },
277
+ async get(call, raw) {
278
+ const { id } = parse(call.ops.get.input, raw);
279
+ const { populate = [] } = parse(call.populate, raw);
280
+ const doc = await call.store.get(call.collection.slug, id, call.scope);
281
+ if (doc === null)
282
+ throw notFound(call, id);
283
+ const [document] = await populated(call, call.collection, populate, [
284
+ await read(call, doc),
285
+ ]);
286
+ return document === undefined
287
+ ? document
288
+ : withLinks(call, call.collection, populate, doc, document);
289
+ },
290
+ async create(call, raw) {
291
+ const input = parse(call.ops.create.input, raw);
292
+ // Create has no scope to filter by, so its access is asked here, with
293
+ // the data it will write.
294
+ await call.access.scope(call.collection, 'create', input);
295
+ const data = await change(call, { data: input, operation: 'create' });
296
+ // Stamped after beforeChange, so a hook cannot pick the tenant.
297
+ const { tenant } = call.collection;
298
+ const doc = stamped(tenant === undefined
299
+ ? data
300
+ : { ...withoutTenant(call.collection, data), [tenant]: call.tenantId });
301
+ await call.store.insert(call.collection.slug, doc);
302
+ await call.events.publish(new call.collection.events.created({
303
+ doc,
304
+ actorId: call.actorId,
305
+ ...tenantPayload(call),
306
+ }));
307
+ return read(call, doc);
308
+ },
309
+ async update(call, raw) {
310
+ // The id schema types `id`; across all collections the patch schema is an
311
+ // index signature, which would type it unknown.
312
+ const { id } = parse(call.ops.get.input, raw);
313
+ const { id: _, ...patch } = parse(call.ops.update.input, raw);
314
+ await refuseWithheld(call.access, call.collection, 'update', patch);
315
+ const stored = await call.store.get(call.collection.slug, id, call.scope);
316
+ if (stored === null)
317
+ throw notFound(call, id);
318
+ const previous = documentOf(call, stored);
319
+ // The tenant field is Lattice's, like the system fields: the schema does
320
+ // not have it, so leaving it in would read as a stored key to remove.
321
+ const { id: _id, createdAt, updatedAt, ...stamps } = previous;
322
+ const data = withoutTenant(call.collection, stamps);
323
+ // The merged document must pass the whole schema, as a create would, so
324
+ // an object-level refinement holds on update too. Parsing also drops
325
+ // stored keys the schema no longer has.
326
+ // A null on a field it clears removes the key (0035); a nullable field
327
+ // keeps its null as a value.
328
+ const { cleared } = call.ops.update;
329
+ const merged = parse(call.ops.create.input, Object.fromEntries(Object.entries({ ...data, ...patch }).filter(([key, value]) => value !== null || !cleared.has(key))));
330
+ // beforeChange sees the whole document after the change, as on create.
331
+ // A hook that returns the tenant field changes nothing: update never
332
+ // writes it.
333
+ const next = withoutTenant(call.collection, await change(call, { data: merged, operation: 'update', previous }));
334
+ // Only the fields that differ from the stored ones are written. A stored
335
+ // key that is gone from `next` (dropped by the schema or by the hook) is
336
+ // written as undefined, which the store removes.
337
+ const changes = Object.fromEntries(Object.keys({ ...data, ...next })
338
+ .filter((key) => !isDeepStrictEqual(data[key], next[key]))
339
+ .map((key) => [key, next[key]]));
340
+ const updated = await call.store.update(call.collection.slug, id, touched(changes), call.scope);
341
+ if (updated === null)
342
+ throw notFound(call, id);
343
+ await call.events.publish(new call.collection.events.updated({
344
+ doc: documentOf(call, updated),
345
+ previous,
346
+ actorId: call.actorId,
347
+ ...tenantPayload(call),
348
+ }));
349
+ return read(call, updated);
350
+ },
351
+ async delete(call, raw) {
352
+ const { id } = parse(call.ops.delete.input, raw);
353
+ const { slug } = call.collection;
354
+ // With refs to this collection, onDelete runs in one store transaction,
355
+ // and its events publish only after the commit (0028).
356
+ const { removed, owed } = (call.referrers.get(slug) ?? []).length === 0
357
+ ? { removed: await call.store.remove(slug, id, call.scope), owed: [] }
358
+ : await call.store.transaction(async (store) => {
359
+ const doc = await store.get(slug, id, call.scope);
360
+ if (doc === null)
361
+ throw notFound(call, id);
362
+ const plan = await planDelete(store, call.referrers, {
363
+ collection: call.collection,
364
+ doc,
365
+ });
366
+ const owed = await applyPlan(store, call, plan);
367
+ return { removed: await store.remove(slug, id, call.scope), owed };
368
+ });
369
+ if (removed === null)
370
+ throw notFound(call, id);
371
+ for (const event of owed)
372
+ await call.events.publish(event);
373
+ await call.events.publish(new call.collection.events.deleted({
374
+ doc: documentOf(call, removed),
375
+ actorId: call.actorId,
376
+ ...tenantPayload(call),
377
+ }));
378
+ return read(call, removed);
379
+ },
380
+ };
381
+ /** The body of one generated operation. */
382
+ export function operationBody(family, store, events, collection, ops, action, populate, referrers, collections) {
383
+ const run = operations[action];
384
+ return async (ctx, raw) => run({
385
+ store,
386
+ events,
387
+ collection,
388
+ ops,
389
+ scope: await scopeOf(ctx, collection, action),
390
+ populate,
391
+ actorId: family.actorId(ctx.actor),
392
+ tenantId: CallAccess.stampOf(ctx.access, collection),
393
+ referrers,
394
+ access: ctx.access,
395
+ collections,
396
+ }, raw);
397
+ }
398
+ /**
399
+ * A generated use case, an operation's or a relation operation's, in the
400
+ * app's family: its guards, logging and span around `body`.
401
+ */
402
+ export function generatedUseCase(family, token, spec, body) {
403
+ class Generated extends family(token, spec) {
404
+ async execute(ctx, raw) {
405
+ return body(ctx, raw);
406
+ }
407
+ }
408
+ return new Generated();
409
+ }
410
+ /**
411
+ * The body of a relation operation of `collection`: `<slug>.<name>.list` or
412
+ * `.set`. `list` runs the parent's read and `set` its update (0028): each
413
+ * first gets the parent under that scope, so a parent outside it is
414
+ * NOT_FOUND.
415
+ */
416
+ export function relationBody(family, store, events, collection, name, relation, op, collections) {
417
+ const join = relation.through;
418
+ const { parentField, targetField } = relation;
419
+ // The fields of a link, as `set` compares them.
420
+ const linkFields = Object.keys(join.schema.shape).filter((field) => field !== parentField);
421
+ /** The parent under the scope of `action`, or NOT_FOUND. */
422
+ async function parentOf(ctx, id) {
423
+ const scope = await ctx.access.scope(collection, `${name}.${op}`);
424
+ const parent = await store.get(collection.slug, id, scope.where);
425
+ if (parent === null) {
426
+ throw LatticeError.NOT_FOUND({ collection: collection.slug, id });
427
+ }
428
+ return parent;
429
+ }
430
+ /** The links of `id`, inside the parent's tenant. */
431
+ function linksOf(id, tenantId) {
432
+ return (both({ [parentField]: { eq: id } }, tenantClause(join, tenantId)) ?? {});
433
+ }
434
+ async function list(ctx, raw) {
435
+ const { id, where, sort, limit, after, populate } = parse(relation.list.input, raw);
436
+ const parent = await parentOf(ctx, id);
437
+ const page = await store.find(findQuery(join, {
438
+ where: both(where, linksOf(id, tenantOfDoc(collection, parent))),
439
+ sort,
440
+ limit,
441
+ after,
442
+ }));
443
+ return {
444
+ docs: await populated({ store, access: ctx.access, collections }, join, populate ?? [], page.docs.map((doc) => documentOf({ collection: join }, doc))),
445
+ next: page.next,
446
+ };
447
+ }
448
+ async function set(ctx, raw) {
449
+ const { id, links } = parse(relation.set.input, raw);
450
+ const parent = await parentOf(ctx, id);
451
+ const tenantId = tenantOfDoc(collection, parent);
452
+ const actorId = family.actorId(ctx.actor);
453
+ const tenant = tenantId === undefined ? {} : { tenantId };
454
+ // The set replaces the links by diffing: an unchanged link keeps its id
455
+ // and createdAt, and each change owes its own event, after the commit.
456
+ const { docs, owed } = await store.transaction(async (tx) => {
457
+ const existing = [];
458
+ for await (const doc of every(tx, join, linksOf(id, tenantId))) {
459
+ existing.push(doc);
460
+ }
461
+ const byTarget = new Map(existing.map((doc) => [doc[targetField], doc]));
462
+ const wanted = new Set(links.map((link) => link[targetField]));
463
+ const owed = [];
464
+ for (const doc of existing) {
465
+ if (wanted.has(doc[targetField]))
466
+ continue;
467
+ const removed = await tx.remove(join.slug, doc.id);
468
+ if (removed === null)
469
+ continue;
470
+ owed.push(new join.events.deleted({
471
+ doc: documentOf({ collection: join }, removed),
472
+ actorId,
473
+ ...tenant,
474
+ }));
475
+ }
476
+ const docs = [];
477
+ for (const link of links) {
478
+ const previous = byTarget.get(link[targetField]);
479
+ if (previous === undefined) {
480
+ const doc = stamped({
481
+ ...link,
482
+ [parentField]: id,
483
+ ...(join.tenant === undefined || tenantId === undefined
484
+ ? {}
485
+ : { [join.tenant]: tenantId }),
486
+ });
487
+ await tx.insert(join.slug, doc);
488
+ owed.push(new join.events.created({
489
+ doc: documentOf({ collection: join }, doc),
490
+ actorId,
491
+ ...tenant,
492
+ }));
493
+ docs.push(doc);
494
+ continue;
495
+ }
496
+ // A field the new link leaves out is written as undefined, which
497
+ // the store removes.
498
+ const changes = Object.fromEntries(linkFields
499
+ .filter((field) => !isDeepStrictEqual(previous[field], link[field]))
500
+ .map((field) => [field, link[field]]));
501
+ if (Object.keys(changes).length === 0) {
502
+ docs.push(previous);
503
+ continue;
504
+ }
505
+ const updated = await tx.update(join.slug, previous.id, touched(changes));
506
+ if (updated === null)
507
+ throw notFoundIn(join, previous.id);
508
+ owed.push(new join.events.updated({
509
+ doc: documentOf({ collection: join }, updated),
510
+ previous: documentOf({ collection: join }, previous),
511
+ actorId,
512
+ ...tenant,
513
+ }));
514
+ docs.push(updated);
515
+ }
516
+ return { docs, owed };
517
+ });
518
+ for (const event of owed)
519
+ await events.publish(event);
520
+ return { docs: docs.map((doc) => documentOf({ collection: join }, doc)) };
521
+ }
522
+ return op === 'list' ? list : set;
523
+ }
524
+ function notFoundIn(collection, id) {
525
+ return LatticeError.NOT_FOUND({ collection: collection.slug, id });
526
+ }
527
+ //# sourceMappingURL=crud.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crud.js","sourceRoot":"","sources":["../src/crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAWzC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,aAAa,CAA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,EAAE,EAAU;IACvD,OAAO,YAAY,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;AACrE,CAAC"}
@@ -0,0 +1,89 @@
1
+ import { type DocsMeta } from '@ts-pf/docs';
2
+ import { type McpEffect } from '@ts-pf/mcp/meta';
3
+ /**
4
+ * Every text and fact Lattice writes into a contract, in one place: each
5
+ * procedure's summary and `lattice` meta, a collection's label, and why a
6
+ * query on a maskable field may be refused. They reach the catalog, so
7
+ * OpenAPI, codegen's JSDoc, MCP tools and the admin read them with no code
8
+ * in the app.
9
+ */
10
+ /** A generated collection operation, or a relation's. */
11
+ type GeneratedOp = 'find' | 'count' | 'get' | 'create' | 'update' | 'delete' | 'list' | 'set';
12
+ /**
13
+ * How an operation affects data, as `@ts-pf/mcp` reads it for tool
14
+ * annotations: reads are read-only, and an operation that removes data
15
+ * (delete, or a relation's `set`, which drops links it is not given) is
16
+ * destructive.
17
+ */
18
+ export type EffectOf<Op extends GeneratedOp> = Op extends 'find' | 'count' | 'get' | 'list' ? 'read' : Op extends 'delete' | 'set' ? 'destructive' : 'write';
19
+ /** How a collection reads in a sentence: `{ singular: 'company', plural: 'companies' }`. */
20
+ export type Label = {
21
+ readonly singular: string;
22
+ readonly plural: string;
23
+ };
24
+ /**
25
+ * What every Lattice procedure says about itself in its meta, under the
26
+ * `lattice` key: its collection and that collection's label, and which
27
+ * operation it is. A relation's `list` and `set` name the relation, and a
28
+ * method names itself. An override carries the meta of the operation it
29
+ * replaces, because clients see the same procedure.
30
+ */
31
+ export type LatticeMeta = {
32
+ readonly collection: string;
33
+ readonly label: Label;
34
+ readonly operation: 'find' | 'count' | 'get' | 'create' | 'update' | 'delete';
35
+ } | {
36
+ readonly collection: string;
37
+ readonly label: Label;
38
+ readonly operation: 'list' | 'set';
39
+ readonly relation: string;
40
+ /** The slug of the collection the relation's links point to. */
41
+ readonly target: string;
42
+ } | {
43
+ readonly collection: string;
44
+ readonly label: Label;
45
+ readonly operation: 'method';
46
+ readonly method: string;
47
+ };
48
+ /** A procedure's meta: its summary, its MCP effect, and Lattice's facts. */
49
+ export type ProcedureMeta<Fx extends McpEffect> = {
50
+ readonly docs: DocsMeta;
51
+ readonly mcp: {
52
+ readonly effect: Fx;
53
+ };
54
+ readonly lattice: LatticeMeta;
55
+ };
56
+ /** A collection's label from its slug: `deal_stages` is "deal stage" and "deal stages". */
57
+ export declare function labelOf(slug: string): Label;
58
+ /** What a summary names: the collection, the operation, and what it populates. */
59
+ export type Described = {
60
+ readonly slug: string;
61
+ readonly label: Label;
62
+ readonly op: Exclude<GeneratedOp, 'list' | 'set'>;
63
+ readonly populate: readonly string[];
64
+ } | {
65
+ readonly slug: string;
66
+ readonly label: Label;
67
+ readonly op: 'list' | 'set';
68
+ readonly relation: string;
69
+ readonly target: string;
70
+ readonly populate: readonly string[];
71
+ };
72
+ /** The meta of a generated procedure. */
73
+ export declare function generatedMeta<Op extends GeneratedOp>(described: Described & {
74
+ readonly op: Op;
75
+ }): ProcedureMeta<EffectOf<Op>>;
76
+ /** The meta of a method's procedure, from its `summary` and `effect` statics. */
77
+ export declare function methodMeta<Fx extends McpEffect>(method: {
78
+ readonly slug: string;
79
+ readonly label: Label;
80
+ readonly name: string;
81
+ readonly summary: string;
82
+ readonly effect: Fx;
83
+ }): ProcedureMeta<Fx>;
84
+ /** A `where` entry's description for a maskable field. */
85
+ export declare function refusedWhere(field: string): string;
86
+ /** A `sort` description for a collection with maskable fields. */
87
+ export declare function refusedSort(fields: readonly string[]): string;
88
+ export {};
89
+ //# sourceMappingURL=describe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"describe.d.ts","sourceRoot":"","sources":["../src/describe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAQ,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,KAAK,SAAS,EAAO,MAAM,iBAAiB,CAAC;AAEtD;;;;;;GAMG;AAEH,y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