@geonosis/document 0.4.1 → 0.6.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/CHANGELOG.md +40 -0
- package/README.md +4 -0
- package/dist/chunk-WWP7MNI7.js +180 -0
- package/dist/index.cjs +3 -1149
- package/dist/index.d.cts +2 -512
- package/dist/index.d.ts +2 -512
- package/dist/index.js +15 -1307
- package/dist/ports-D0NtPwAO.d.cts +233 -0
- package/dist/ports-D0NtPwAO.d.ts +233 -0
- package/dist/store.cjs +1352 -0
- package/dist/store.d.cts +285 -0
- package/dist/store.d.ts +285 -0
- package/dist/store.js +1200 -0
- package/package.json +26 -3
package/dist/index.cjs
CHANGED
|
@@ -21,28 +21,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
BODY_VERSION_KEY: () => BODY_VERSION_KEY,
|
|
24
|
-
DEFAULT_DOCUMENT_TABLE: () => DEFAULT_DOCUMENT_TABLE,
|
|
25
|
-
DEFAULT_REVISIONS_TABLE: () => DEFAULT_REVISIONS_TABLE,
|
|
26
|
-
DEFAULT_REVISION_KIND_COLUMN: () => DEFAULT_REVISION_KIND_COLUMN,
|
|
27
|
-
DOCUMENT_MIGRATION_PLACEHOLDERS: () => import_db.ENTITY_PLACEHOLDERS,
|
|
28
24
|
Document: () => Document,
|
|
29
|
-
HEAD_FIELDS: () => HEAD_FIELDS,
|
|
30
|
-
appendOnlyTables: () => appendOnlyTables,
|
|
31
25
|
canTransition: () => canTransition,
|
|
32
26
|
canonicalOf: () => canonicalOf,
|
|
33
27
|
documentBodyVersioning: () => documentBodyVersioning,
|
|
34
28
|
documentEntities: () => documentEntities,
|
|
35
29
|
documentIdentifiers: () => documentIdentifiers,
|
|
36
|
-
documentMigrations: () => documentMigrations,
|
|
37
|
-
documentRoleStatements: () => documentRoleStatements,
|
|
38
|
-
documentStore: () => documentStore,
|
|
39
|
-
documentSurface: () => documentSurface,
|
|
40
30
|
fieldPatterns: () => fieldPatterns,
|
|
41
|
-
hashOf: () => hashOf,
|
|
42
31
|
isSystemActor: () => isSystemActor,
|
|
43
32
|
isTerminalStatus: () => isTerminalStatus,
|
|
44
33
|
migrateBody: () => migrateBody,
|
|
45
|
-
physicalColumnsOf: () => physicalColumnsOf,
|
|
46
34
|
stampBody: () => stampBody,
|
|
47
35
|
systemActor: () => systemActor,
|
|
48
36
|
validatorOf: () => validatorOf
|
|
@@ -102,7 +90,7 @@ var FIELD_CHECKS = {
|
|
|
102
90
|
text: { message: "a string", test: (value) => typeof value === "string" }
|
|
103
91
|
};
|
|
104
92
|
var validatorOf = (fields, options) => {
|
|
105
|
-
const
|
|
93
|
+
const nullable = options?.nullable ?? false;
|
|
106
94
|
return {
|
|
107
95
|
"~standard": {
|
|
108
96
|
validate: (input) => {
|
|
@@ -115,7 +103,7 @@ var validatorOf = (fields, options) => {
|
|
|
115
103
|
for (const [key, kind] of Object.entries(fields)) {
|
|
116
104
|
const held = record[key];
|
|
117
105
|
value[key] = held;
|
|
118
|
-
if (
|
|
106
|
+
if (nullable && held === null) continue;
|
|
119
107
|
if (!FIELD_CHECKS[kind].test(held)) {
|
|
120
108
|
issues.push({ message: FIELD_CHECKS[kind].message, path: [{ key }] });
|
|
121
109
|
}
|
|
@@ -200,8 +188,7 @@ var SYSTEM_PREFIX = "system:";
|
|
|
200
188
|
var systemActor = (name) => `${SYSTEM_PREFIX}${name}`;
|
|
201
189
|
var isSystemActor = (actor) => actor.startsWith(SYSTEM_PREFIX);
|
|
202
190
|
|
|
203
|
-
// src/core/
|
|
204
|
-
var import_node_crypto = require("crypto");
|
|
191
|
+
// src/core/canonical.ts
|
|
205
192
|
var canonicalOf = (input) => {
|
|
206
193
|
if (input === null || typeof input !== "object") return input;
|
|
207
194
|
if (Array.isArray(input)) return input.map(canonicalOf);
|
|
@@ -209,7 +196,6 @@ var canonicalOf = (input) => {
|
|
|
209
196
|
Object.entries(input).toSorted(([a], [b]) => a.localeCompare(b)).map(([key, held]) => [key, canonicalOf(held)])
|
|
210
197
|
);
|
|
211
198
|
};
|
|
212
|
-
var hashOf = (payload) => (0, import_node_crypto.createHash)("sha256").update(JSON.stringify(canonicalOf(payload))).digest("hex");
|
|
213
199
|
|
|
214
200
|
// src/core/naming.ts
|
|
215
201
|
var camelOf = (name) => name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
@@ -228,1151 +214,19 @@ var documentIdentifiers = (name) => {
|
|
|
228
214
|
// src/core/status.ts
|
|
229
215
|
var canTransition = (machine, from, to) => machine.transitions[from].includes(to);
|
|
230
216
|
var isTerminalStatus = (machine, status) => machine.transitions[status].length === 0;
|
|
231
|
-
|
|
232
|
-
// src/surface/table.ts
|
|
233
|
-
var VENDOR = "geonosis-document-surface";
|
|
234
|
-
var isString = (value) => typeof value === "string";
|
|
235
|
-
var isNumber = (value) => typeof value === "number";
|
|
236
|
-
var scalar = (test, message) => ({
|
|
237
|
-
"~standard": {
|
|
238
|
-
validate: (input) => test(input) ? { value: input } : { issues: [{ message }] },
|
|
239
|
-
vendor: VENDOR,
|
|
240
|
-
version: 1
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
var stringSchema = scalar(isString, "expected a string");
|
|
244
|
-
var numberSchema = scalar(isNumber, "expected a number");
|
|
245
|
-
var booleanSchema = scalar(
|
|
246
|
-
(value) => typeof value === "boolean",
|
|
247
|
-
"expected a boolean"
|
|
248
|
-
);
|
|
249
|
-
var literalOf = (literal) => scalar(
|
|
250
|
-
(value) => value === literal,
|
|
251
|
-
`expected exactly ${JSON.stringify(literal)}`
|
|
252
|
-
);
|
|
253
|
-
var unknownSchema = {
|
|
254
|
-
"~standard": { validate: (input) => ({ value: input }), vendor: VENDOR, version: 1 }
|
|
255
|
-
};
|
|
256
|
-
var syncResult = (schema, input) => {
|
|
257
|
-
const result = schema["~standard"].validate(input);
|
|
258
|
-
if (result instanceof Promise) {
|
|
259
|
-
throw new Error(
|
|
260
|
-
"a document surface schema validates synchronously; an async Standard Schema cannot back a row"
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
return result;
|
|
264
|
-
};
|
|
265
|
-
var optional = (inner) => ({
|
|
266
|
-
"~standard": {
|
|
267
|
-
validate: (input) => input === void 0 ? { value: void 0 } : syncResult(inner, input),
|
|
268
|
-
vendor: VENDOR,
|
|
269
|
-
version: 1
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
var nullable = (inner) => ({
|
|
273
|
-
"~standard": {
|
|
274
|
-
validate: (input) => input === null ? { value: null } : syncResult(inner, input),
|
|
275
|
-
vendor: VENDOR,
|
|
276
|
-
version: 1
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
var arrayOf = (item) => ({
|
|
280
|
-
"~standard": {
|
|
281
|
-
validate: (input) => {
|
|
282
|
-
if (!Array.isArray(input)) return { issues: [{ message: "expected an array" }] };
|
|
283
|
-
const value = [];
|
|
284
|
-
const issues = [];
|
|
285
|
-
input.forEach((one, at) => {
|
|
286
|
-
const result = syncResult(item, one);
|
|
287
|
-
if (result.issues !== void 0) {
|
|
288
|
-
issues.push(
|
|
289
|
-
...result.issues.map((issue) => ({ ...issue, path: [at, ...issue.path ?? []] }))
|
|
290
|
-
);
|
|
291
|
-
} else {
|
|
292
|
-
value.push(result.value);
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
return issues.length > 0 ? { issues } : { value };
|
|
296
|
-
},
|
|
297
|
-
vendor: VENDOR,
|
|
298
|
-
version: 1
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
var merge = (schemas) => ({
|
|
302
|
-
"~standard": {
|
|
303
|
-
validate: (input) => {
|
|
304
|
-
let value = {};
|
|
305
|
-
const issues = [];
|
|
306
|
-
for (const schema of schemas) {
|
|
307
|
-
const result = syncResult(schema, input);
|
|
308
|
-
if (result.issues !== void 0) issues.push(...result.issues);
|
|
309
|
-
else value = { ...value, ...result.value };
|
|
310
|
-
}
|
|
311
|
-
return issues.length > 0 ? { issues } : { value };
|
|
312
|
-
},
|
|
313
|
-
vendor: VENDOR,
|
|
314
|
-
version: 1
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
var doorFor = (store, name) => {
|
|
318
|
-
const door = store[name];
|
|
319
|
-
if (door === void 0) {
|
|
320
|
-
throw new Error(
|
|
321
|
-
`"${name}" names no store this table's registry can find \u2014 the table and the store must share one registry`
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
return door;
|
|
325
|
-
};
|
|
326
|
-
var HEAD_COLUMNS = Object.keys(Document.headFields);
|
|
327
|
-
var SORT_COLUMNS = [...HEAD_COLUMNS, "number", "createdAt", "updatedAt"];
|
|
328
|
-
var rowsFor = (definition, store, schema) => {
|
|
329
|
-
const { name } = definition;
|
|
330
|
-
const door = doorFor(store, name);
|
|
331
|
-
const basePath = `/${definition.basePath}`;
|
|
332
|
-
const instanceSchema = merge([
|
|
333
|
-
Document.head,
|
|
334
|
-
Document.record,
|
|
335
|
-
schema.object({ body: definition.view, entity: schema.enum([name]), number: stringSchema })
|
|
336
|
-
]);
|
|
337
|
-
const commitOutput = schema.union([
|
|
338
|
-
schema.object({
|
|
339
|
-
committed: literalOf(false),
|
|
340
|
-
lineage: numberSchema,
|
|
341
|
-
ref: stringSchema,
|
|
342
|
-
version: numberSchema
|
|
343
|
-
}),
|
|
344
|
-
schema.object({
|
|
345
|
-
archived: optional(booleanSchema),
|
|
346
|
-
committed: literalOf(true),
|
|
347
|
-
hash: stringSchema,
|
|
348
|
-
lineage: numberSchema,
|
|
349
|
-
previous: nullable(schema.object({ hash: stringSchema, version: numberSchema })),
|
|
350
|
-
ref: stringSchema,
|
|
351
|
-
version: numberSchema
|
|
352
|
-
})
|
|
353
|
-
]);
|
|
354
|
-
const retireLikeOutput = schema.object({
|
|
355
|
-
hash: stringSchema,
|
|
356
|
-
lineage: numberSchema,
|
|
357
|
-
ref: stringSchema,
|
|
358
|
-
version: numberSchema
|
|
359
|
-
});
|
|
360
|
-
const verifyOutput = schema.union([
|
|
361
|
-
schema.object({ ok: literalOf(true), versions: numberSchema }),
|
|
362
|
-
schema.object({
|
|
363
|
-
brokenAt: numberSchema,
|
|
364
|
-
expected: stringSchema,
|
|
365
|
-
found: stringSchema,
|
|
366
|
-
ok: literalOf(false)
|
|
367
|
-
})
|
|
368
|
-
]);
|
|
369
|
-
const historyOutput = arrayOf(
|
|
370
|
-
schema.object({
|
|
371
|
-
actor: stringSchema,
|
|
372
|
-
at: numberSchema,
|
|
373
|
-
body: unknownSchema,
|
|
374
|
-
hash: stringSchema,
|
|
375
|
-
kind: schema.enum(["committed", "retired", "reinstated"]),
|
|
376
|
-
reason: nullable(stringSchema),
|
|
377
|
-
version: numberSchema
|
|
378
|
-
})
|
|
379
|
-
);
|
|
380
|
-
const listInput = schema.object({
|
|
381
|
-
cursor: optional(nullable(stringSchema)),
|
|
382
|
-
filter: optional(
|
|
383
|
-
schema.object(
|
|
384
|
-
Object.fromEntries(HEAD_COLUMNS.map((column) => [column, optional(stringSchema)]))
|
|
385
|
-
)
|
|
386
|
-
),
|
|
387
|
-
limit: optional(numberSchema),
|
|
388
|
-
order: optional(
|
|
389
|
-
schema.object({
|
|
390
|
-
column: schema.enum(SORT_COLUMNS),
|
|
391
|
-
direction: optional(schema.enum(["asc", "desc"]))
|
|
392
|
-
})
|
|
393
|
-
)
|
|
394
|
-
});
|
|
395
|
-
const listOutput = schema.object({
|
|
396
|
-
items: arrayOf(merge([instanceSchema, schema.object({ card: definition.card.schema })])),
|
|
397
|
-
nextCursor: nullable(stringSchema)
|
|
398
|
-
});
|
|
399
|
-
const ackOutput = schema.object({ ref: stringSchema });
|
|
400
|
-
return [
|
|
401
|
-
{
|
|
402
|
-
call: (handle, params) => door.get(handle, params),
|
|
403
|
-
input: schema.object({ ref: stringSchema }),
|
|
404
|
-
name: `${name}.get`,
|
|
405
|
-
output: nullable(instanceSchema),
|
|
406
|
-
path: `${basePath}/:ref`,
|
|
407
|
-
scope: "org",
|
|
408
|
-
verb: "query"
|
|
409
|
-
},
|
|
410
|
-
{
|
|
411
|
-
call: async (handle, params) => {
|
|
412
|
-
const page = await door.list(handle, params);
|
|
413
|
-
return {
|
|
414
|
-
items: page.items.map((item) => ({
|
|
415
|
-
...item,
|
|
416
|
-
card: definition.card.of(item, { counterpartyName: "" })
|
|
417
|
-
})),
|
|
418
|
-
nextCursor: page.nextCursor
|
|
419
|
-
};
|
|
420
|
-
},
|
|
421
|
-
input: listInput,
|
|
422
|
-
name: `${name}.list`,
|
|
423
|
-
output: listOutput,
|
|
424
|
-
path: basePath,
|
|
425
|
-
scope: "org",
|
|
426
|
-
verb: "query"
|
|
427
|
-
},
|
|
428
|
-
{
|
|
429
|
-
call: (handle, params) => door.history(handle, params),
|
|
430
|
-
input: schema.object({ lineage: optional(numberSchema), ref: stringSchema }),
|
|
431
|
-
name: `${name}.history`,
|
|
432
|
-
output: historyOutput,
|
|
433
|
-
path: `${basePath}/:ref/history`,
|
|
434
|
-
scope: "org",
|
|
435
|
-
verb: "query"
|
|
436
|
-
},
|
|
437
|
-
{
|
|
438
|
-
call: (handle, params) => door.verify(handle, params),
|
|
439
|
-
input: schema.object({ lineage: optional(numberSchema), ref: stringSchema }),
|
|
440
|
-
name: `${name}.verify`,
|
|
441
|
-
output: verifyOutput,
|
|
442
|
-
path: `${basePath}/:ref/verify`,
|
|
443
|
-
scope: "org",
|
|
444
|
-
verb: "query"
|
|
445
|
-
},
|
|
446
|
-
{
|
|
447
|
-
call: (handle, params) => door.commit(handle, params),
|
|
448
|
-
input: schema.object({
|
|
449
|
-
actor: stringSchema,
|
|
450
|
-
body: definition.body,
|
|
451
|
-
reason: optional(stringSchema),
|
|
452
|
-
ref: stringSchema
|
|
453
|
-
}),
|
|
454
|
-
name: `${name}.commit`,
|
|
455
|
-
output: commitOutput,
|
|
456
|
-
path: basePath,
|
|
457
|
-
scope: "org",
|
|
458
|
-
verb: "mutation"
|
|
459
|
-
},
|
|
460
|
-
{
|
|
461
|
-
call: (handle, params) => door.retire(handle, params),
|
|
462
|
-
input: schema.object({
|
|
463
|
-
actor: stringSchema,
|
|
464
|
-
reason: optional(stringSchema),
|
|
465
|
-
ref: stringSchema
|
|
466
|
-
}),
|
|
467
|
-
name: `${name}.retire`,
|
|
468
|
-
output: retireLikeOutput,
|
|
469
|
-
path: `${basePath}/:ref/retire`,
|
|
470
|
-
scope: "org",
|
|
471
|
-
verb: "mutation"
|
|
472
|
-
},
|
|
473
|
-
{
|
|
474
|
-
call: (handle, params) => door.reinstate(handle, params),
|
|
475
|
-
input: schema.object({
|
|
476
|
-
actor: stringSchema,
|
|
477
|
-
reason: optional(stringSchema),
|
|
478
|
-
ref: stringSchema
|
|
479
|
-
}),
|
|
480
|
-
name: `${name}.reinstate`,
|
|
481
|
-
output: retireLikeOutput,
|
|
482
|
-
path: `${basePath}/:ref/reinstate`,
|
|
483
|
-
scope: "org",
|
|
484
|
-
verb: "mutation"
|
|
485
|
-
},
|
|
486
|
-
{
|
|
487
|
-
call: async (handle, params) => {
|
|
488
|
-
const asked = params;
|
|
489
|
-
const current = await door.get(handle, params);
|
|
490
|
-
if (current === null) {
|
|
491
|
-
throw new Error(`"${asked.ref}" (${name}) has no document to set a status on`);
|
|
492
|
-
}
|
|
493
|
-
const from = definition.statusOf(current.body);
|
|
494
|
-
if (!canTransition(definition.statuses, from, asked.status)) {
|
|
495
|
-
throw new Error(
|
|
496
|
-
`"${name}" cannot move "${asked.ref}" from "${from}" to "${asked.status}": the status machine declares no such transition`
|
|
497
|
-
);
|
|
498
|
-
}
|
|
499
|
-
const body = definition.withStatus(
|
|
500
|
-
current.body,
|
|
501
|
-
asked.status,
|
|
502
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
503
|
-
);
|
|
504
|
-
return door.commit(handle, { ...asked, body });
|
|
505
|
-
},
|
|
506
|
-
input: schema.object({
|
|
507
|
-
actor: stringSchema,
|
|
508
|
-
reason: optional(stringSchema),
|
|
509
|
-
ref: stringSchema,
|
|
510
|
-
status: schema.enum(definition.statuses.statuses)
|
|
511
|
-
}),
|
|
512
|
-
name: `${name}.setStatus`,
|
|
513
|
-
output: commitOutput,
|
|
514
|
-
path: `${basePath}/:ref/status`,
|
|
515
|
-
scope: "org",
|
|
516
|
-
verb: "mutation"
|
|
517
|
-
},
|
|
518
|
-
{
|
|
519
|
-
call: { pending: "T3", workflow: `${name}.create` },
|
|
520
|
-
input: schema.object({ draft: definition.draft }),
|
|
521
|
-
name: `${name}.create`,
|
|
522
|
-
output: commitOutput,
|
|
523
|
-
path: basePath,
|
|
524
|
-
scope: "org",
|
|
525
|
-
verb: "mutation"
|
|
526
|
-
},
|
|
527
|
-
{
|
|
528
|
-
call: { pending: "T3", workflow: `${name}.send` },
|
|
529
|
-
input: schema.object({ ref: stringSchema }),
|
|
530
|
-
name: `${name}.send`,
|
|
531
|
-
output: ackOutput,
|
|
532
|
-
path: `${basePath}/:ref/send`,
|
|
533
|
-
scope: "org",
|
|
534
|
-
verb: "mutation"
|
|
535
|
-
},
|
|
536
|
-
{
|
|
537
|
-
call: { pending: "T3", workflow: `${name}.duplicate` },
|
|
538
|
-
input: schema.object({ ref: stringSchema }),
|
|
539
|
-
name: `${name}.duplicate`,
|
|
540
|
-
output: commitOutput,
|
|
541
|
-
path: `${basePath}/:ref/duplicate`,
|
|
542
|
-
scope: "org",
|
|
543
|
-
verb: "mutation"
|
|
544
|
-
},
|
|
545
|
-
{
|
|
546
|
-
call: { pending: "T3", workflow: `${name}.runStarted` },
|
|
547
|
-
input: schema.object({ ref: stringSchema }),
|
|
548
|
-
name: `${name}.runStarted`,
|
|
549
|
-
output: ackOutput,
|
|
550
|
-
path: `${basePath}/:ref/run-started`,
|
|
551
|
-
scope: "org",
|
|
552
|
-
verb: "mutation"
|
|
553
|
-
}
|
|
554
|
-
];
|
|
555
|
-
};
|
|
556
|
-
var documentSurface = (options) => {
|
|
557
|
-
const { registry, schema, store } = options;
|
|
558
|
-
const headsColumns = [...HEAD_COLUMNS, "entity", "number"];
|
|
559
|
-
const headsInput = schema.object({
|
|
560
|
-
columns: arrayOf(schema.enum(headsColumns)),
|
|
561
|
-
entity: optional(schema.union([stringSchema, arrayOf(stringSchema)])),
|
|
562
|
-
order: arrayOf(
|
|
563
|
-
schema.object({
|
|
564
|
-
column: schema.enum([...SORT_COLUMNS, "entity"]),
|
|
565
|
-
direction: optional(schema.enum(["asc", "desc"]))
|
|
566
|
-
})
|
|
567
|
-
),
|
|
568
|
-
where: optional(unknownSchema)
|
|
569
|
-
});
|
|
570
|
-
const aggregateInput = schema.object({
|
|
571
|
-
by: optional(unknownSchema),
|
|
572
|
-
entities: optional(arrayOf(stringSchema)),
|
|
573
|
-
entity: optional(stringSchema),
|
|
574
|
-
measures: arrayOf(schema.enum(["count", "sum:total"])),
|
|
575
|
-
where: optional(unknownSchema)
|
|
576
|
-
});
|
|
577
|
-
const aggregateOutput = arrayOf(
|
|
578
|
-
schema.object({
|
|
579
|
-
by: optional(stringSchema),
|
|
580
|
-
count: optional(numberSchema),
|
|
581
|
-
sum: optional(stringSchema)
|
|
582
|
-
})
|
|
583
|
-
);
|
|
584
|
-
const sharedOutput = schema.union(
|
|
585
|
-
registry.fold(
|
|
586
|
-
(entity) => schema.object({ entity: schema.enum([entity.name]), view: entity.view })
|
|
587
|
-
)
|
|
588
|
-
);
|
|
589
|
-
return [
|
|
590
|
-
...registry.fold((entity) => rowsFor(entity, store, schema)).flat(),
|
|
591
|
-
{
|
|
592
|
-
call: (handle, params) => store.heads(handle, params),
|
|
593
|
-
input: headsInput,
|
|
594
|
-
name: "heads",
|
|
595
|
-
output: arrayOf(unknownSchema),
|
|
596
|
-
path: "/documents/heads",
|
|
597
|
-
scope: "org",
|
|
598
|
-
verb: "query"
|
|
599
|
-
},
|
|
600
|
-
{
|
|
601
|
-
call: (handle, params) => store.aggregate(handle, params),
|
|
602
|
-
input: aggregateInput,
|
|
603
|
-
name: "aggregate",
|
|
604
|
-
output: aggregateOutput,
|
|
605
|
-
path: "/documents/aggregate",
|
|
606
|
-
scope: "org",
|
|
607
|
-
verb: "query"
|
|
608
|
-
},
|
|
609
|
-
{
|
|
610
|
-
call: { pending: "T3", workflow: "document.shared" },
|
|
611
|
-
input: schema.object({ token: stringSchema }),
|
|
612
|
-
name: "shared",
|
|
613
|
-
output: sharedOutput,
|
|
614
|
-
path: "/documents/shared",
|
|
615
|
-
scope: "token",
|
|
616
|
-
verb: "query"
|
|
617
|
-
}
|
|
618
|
-
];
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
// src/store/ddl.ts
|
|
622
|
-
var import_db = require("@geonosis/db");
|
|
623
|
-
var DEFAULT_DOCUMENT_TABLE = "documents";
|
|
624
|
-
var DEFAULT_ENTITY_COLUMN = "entity";
|
|
625
|
-
var DEFAULT_VERSION_COLUMN = "version";
|
|
626
|
-
var DEFAULT_TENANT_COLUMN = "tenant_id";
|
|
627
|
-
var DEFAULT_CREATED_AT_COLUMN = "created_at";
|
|
628
|
-
var DEFAULT_UPDATED_AT_COLUMN = "updated_at";
|
|
629
|
-
var DEFAULT_REVISIONS_TABLE = "document_revisions";
|
|
630
|
-
var DEFAULT_REVISION_KIND_COLUMN = "revision_kind";
|
|
631
|
-
var REVISION_KINDS = ["committed", "retired", "reinstated"];
|
|
632
|
-
var HEAD_FIELDS = Object.keys(headFields);
|
|
633
|
-
var snakeOf = (field) => field.replaceAll(/[A-Z]/g, (upper) => `_${upper.toLowerCase()}`);
|
|
634
|
-
var nameOf = (value, fallback) => value === void 0 ? fallback : typeof value === "string" ? value : value.name;
|
|
635
|
-
var physicalColumnsOf = (columns) => {
|
|
636
|
-
const heads = Object.fromEntries(
|
|
637
|
-
HEAD_FIELDS.map((field) => [
|
|
638
|
-
field,
|
|
639
|
-
nameOf(
|
|
640
|
-
columns?.heads?.[field] ?? (field === "total" ? columns?.total : void 0),
|
|
641
|
-
snakeOf(field)
|
|
642
|
-
)
|
|
643
|
-
])
|
|
644
|
-
);
|
|
645
|
-
return {
|
|
646
|
-
...heads,
|
|
647
|
-
createdAt: nameOf(columns?.createdAt, DEFAULT_CREATED_AT_COLUMN),
|
|
648
|
-
entity: columns?.entity ?? DEFAULT_ENTITY_COLUMN,
|
|
649
|
-
tenant: nameOf(columns?.tenant, DEFAULT_TENANT_COLUMN),
|
|
650
|
-
updatedAt: nameOf(columns?.updatedAt, DEFAULT_UPDATED_AT_COLUMN)
|
|
651
|
-
};
|
|
652
|
-
};
|
|
653
|
-
var appendOnlyTables = (options = {}) => {
|
|
654
|
-
if (options.revisions === false) return [];
|
|
655
|
-
return [options.revisions?.table ?? options.table ?? DEFAULT_REVISIONS_TABLE];
|
|
656
|
-
};
|
|
657
|
-
var quotedName = (name) => `"${name}"`;
|
|
658
|
-
var literalName = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
659
|
-
var documentRoleStatements = (appRole, options = {}) => {
|
|
660
|
-
if (typeof appRole !== "string" || appRole === "") {
|
|
661
|
-
throw new import_db.DbRefusal(
|
|
662
|
-
"documentRoleStatements needs an `appRole` \u2014 the name a consumer\u2019s application connects as belongs to whoever runs the database, and one baked in here would be another repo\u2019s"
|
|
663
|
-
);
|
|
664
|
-
}
|
|
665
|
-
const role = quotedName(appRole);
|
|
666
|
-
const on = (table) => options.schema === void 0 ? quotedName(table) : `${quotedName(options.schema)}.${quotedName(table)}`;
|
|
667
|
-
return appendOnlyTables(options).flatMap((table) => [
|
|
668
|
-
`revoke update, delete on ${on(table)} from ${role}`,
|
|
669
|
-
`grant select, insert on ${on(table)} to ${role}`
|
|
670
|
-
]);
|
|
671
|
-
};
|
|
672
|
-
var DDL_TYPE_OF = { decimal: "numeric" };
|
|
673
|
-
var headIndexedColumns = (physical) => HEAD_FIELDS.map((field) => {
|
|
674
|
-
const type = DDL_TYPE_OF[headFields[field]];
|
|
675
|
-
return {
|
|
676
|
-
column: physical[field],
|
|
677
|
-
nullable: field !== "status",
|
|
678
|
-
...type === void 0 ? {} : { type }
|
|
679
|
-
};
|
|
680
|
-
});
|
|
681
|
-
var revisionKindType = (column) => `text check (${quotedName(column)} in (${REVISION_KINDS.map(literalName).join(", ")}))`;
|
|
682
|
-
var walls = (options) => ({
|
|
683
|
-
...options.freeze === void 0 ? {} : { freeze: options.freeze },
|
|
684
|
-
...options.names === void 0 ? {} : { names: options.names },
|
|
685
|
-
...options.schema === void 0 ? {} : { schema: options.schema }
|
|
686
|
-
});
|
|
687
|
-
var revisionsMigrations = (options, physical) => {
|
|
688
|
-
if (options.revisions === false) return [];
|
|
689
|
-
if (typeof options.appRole !== "string" || options.appRole === "") {
|
|
690
|
-
throw new import_db.DbRefusal(
|
|
691
|
-
"documentMigrations needs an `appRole` to render the log\u2019s wall \u2014 the role a consumer\u2019s application connects as belongs to whoever runs the database, and one baked in here would be another repo\u2019s. Pass it, or opt out of the log entirely with `revisions: false`"
|
|
692
|
-
);
|
|
693
|
-
}
|
|
694
|
-
const revisionKindColumn = options.columns?.revisionKind ?? DEFAULT_REVISION_KIND_COLUMN;
|
|
695
|
-
const table = options.revisions?.table ?? DEFAULT_REVISIONS_TABLE;
|
|
696
|
-
const [tableMigration, policies] = (0, import_db.logMigrations)({
|
|
697
|
-
appRole: options.appRole,
|
|
698
|
-
columns: { tenant: physical.tenant },
|
|
699
|
-
extra: [{ column: revisionKindColumn, type: revisionKindType(revisionKindColumn) }],
|
|
700
|
-
key: [physical.entity, "ref", { column: "lineage", type: "integer" }],
|
|
701
|
-
...walls(options),
|
|
702
|
-
settings: options.settings,
|
|
703
|
-
table
|
|
704
|
-
});
|
|
705
|
-
if (tableMigration === void 0 || policies === void 0) {
|
|
706
|
-
throw new Error("logMigrations answered with fewer than the two migrations a log renders");
|
|
707
|
-
}
|
|
708
|
-
return [
|
|
709
|
-
{ ...tableMigration, name: `0003_${table}.sql` },
|
|
710
|
-
{ ...policies, name: `0004_${table}_policies.sql` }
|
|
711
|
-
];
|
|
712
|
-
};
|
|
713
|
-
var documentMigrations = (options) => {
|
|
714
|
-
const table = options.table ?? DEFAULT_DOCUMENT_TABLE;
|
|
715
|
-
const physical = physicalColumnsOf(options.columns);
|
|
716
|
-
const {
|
|
717
|
-
entity: _entity,
|
|
718
|
-
heads: _heads,
|
|
719
|
-
revisionKind: _revisionKind,
|
|
720
|
-
total: _total,
|
|
721
|
-
version,
|
|
722
|
-
...restColumns
|
|
723
|
-
} = options.columns ?? {};
|
|
724
|
-
const [tableMigration, ...rest] = (0, import_db.projectionMigrations)({
|
|
725
|
-
carried: [{ column: "lineage", type: "integer not null default 1" }],
|
|
726
|
-
columns: { ...restColumns, sequence: version ?? DEFAULT_VERSION_COLUMN },
|
|
727
|
-
constants: { [physical.entity]: "" },
|
|
728
|
-
indexed: headIndexedColumns(physical),
|
|
729
|
-
key: "number",
|
|
730
|
-
order: [{ column: "number" }],
|
|
731
|
-
...walls(options),
|
|
732
|
-
settings: options.settings,
|
|
733
|
-
sortable: [
|
|
734
|
-
...HEAD_FIELDS.map((field) => physical[field]),
|
|
735
|
-
physical.createdAt,
|
|
736
|
-
physical.updatedAt
|
|
737
|
-
],
|
|
738
|
-
table
|
|
739
|
-
});
|
|
740
|
-
if (tableMigration === void 0) {
|
|
741
|
-
throw new Error(
|
|
742
|
-
"projectionMigrations answered with no table migration to append the listing index to"
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
const listingIndex = `create index if not exists ${quotedName(`${table}_${physical.entity}_${physical.issueDate}_idx`)} on ${quotedName(table)} (${quotedName(physical.entity)}, ${quotedName(physical.issueDate)})`;
|
|
746
|
-
return [
|
|
747
|
-
{ ...tableMigration, statements: [...tableMigration.statements, listingIndex] },
|
|
748
|
-
...rest,
|
|
749
|
-
...revisionsMigrations(options, physical)
|
|
750
|
-
];
|
|
751
|
-
};
|
|
752
|
-
|
|
753
|
-
// src/store/store.ts
|
|
754
|
-
var import_db2 = require("@geonosis/db");
|
|
755
|
-
var DEFAULT_VERSION_COLUMN2 = "version";
|
|
756
|
-
var UNDO_ACTOR = systemActor("document.undo");
|
|
757
|
-
var nameOf2 = (value, fallback) => value === void 0 ? fallback : typeof value === "string" ? value : value.name;
|
|
758
|
-
var readBody = (entity, entityName, ref, stored) => {
|
|
759
|
-
const migrated = migrateBody(entity, stored);
|
|
760
|
-
const result = entity.body["~standard"].validate(migrated);
|
|
761
|
-
if (result instanceof Promise) {
|
|
762
|
-
throw new Error(
|
|
763
|
-
`${entityName} ${ref}: this entity's body schema validates asynchronously, and a stored body can only be read synchronously here`
|
|
764
|
-
);
|
|
765
|
-
}
|
|
766
|
-
if (result.issues !== void 0) {
|
|
767
|
-
throw new Error(
|
|
768
|
-
`${entityName} ${ref}'s stored body no longer satisfies its own schema after migration: ${result.issues.map((issue) => issue.message).join("; ")}`
|
|
769
|
-
);
|
|
770
|
-
}
|
|
771
|
-
return result.value;
|
|
772
|
-
};
|
|
773
|
-
var headFrom = (found) => found === void 0 ? void 0 : {
|
|
774
|
-
body: found.body,
|
|
775
|
-
hash: found.hash,
|
|
776
|
-
lineage: Number(found.carried.lineage ?? 1),
|
|
777
|
-
retiredAt: found.retiredAt,
|
|
778
|
-
version: found.sequence
|
|
779
|
-
};
|
|
780
|
-
var documentStore = (options) => {
|
|
781
|
-
const table = options.table ?? DEFAULT_DOCUMENT_TABLE;
|
|
782
|
-
const revisionKindColumn = options.columns?.revisionKind ?? DEFAULT_REVISION_KIND_COLUMN;
|
|
783
|
-
const physical = physicalColumnsOf(options.columns);
|
|
784
|
-
const {
|
|
785
|
-
entity: _entity,
|
|
786
|
-
heads: _heads,
|
|
787
|
-
revisionKind: _revisionKind,
|
|
788
|
-
total: _total,
|
|
789
|
-
version,
|
|
790
|
-
...entityColumns
|
|
791
|
-
} = options.columns ?? {};
|
|
792
|
-
const versionColumn = nameOf2(version, DEFAULT_VERSION_COLUMN2);
|
|
793
|
-
const entityColumn = physical.entity;
|
|
794
|
-
const { archive, registry, schema, seam } = options;
|
|
795
|
-
const tenantKey = seam.tenantKey;
|
|
796
|
-
const placed = schema === void 0 ? {} : { schema };
|
|
797
|
-
const revisionsTable = options.revisions === false ? void 0 : options.revisions?.table ?? DEFAULT_REVISIONS_TABLE;
|
|
798
|
-
const tenanted = (tenantId) => ({ [tenantKey]: tenantId });
|
|
799
|
-
const columnOf = (column) => column === "number" ? "number" : physical[column];
|
|
800
|
-
const physicalHeads = HEAD_FIELDS.map(columnOf);
|
|
801
|
-
const narrowed = (where) => Object.fromEntries(
|
|
802
|
-
Object.entries(where ?? {}).map(([column, held]) => [
|
|
803
|
-
columnOf(column),
|
|
804
|
-
held
|
|
805
|
-
])
|
|
806
|
-
);
|
|
807
|
-
const revisions = revisionsTable === void 0 ? void 0 : (0, import_db2.defineLog)({
|
|
808
|
-
columns: { tenant: physical.tenant },
|
|
809
|
-
extra: [{ column: revisionKindColumn, param: "kind" }],
|
|
810
|
-
hash: (row) => hashOf({
|
|
811
|
-
actor: row.actor,
|
|
812
|
-
at: row.at,
|
|
813
|
-
body: row.body,
|
|
814
|
-
entity: row.entity,
|
|
815
|
-
kind: row.kind,
|
|
816
|
-
lineage: row.lineage,
|
|
817
|
-
prevHash: row.prevHash,
|
|
818
|
-
reason: row.reason,
|
|
819
|
-
ref: row.ref,
|
|
820
|
-
tenant: row.tenant,
|
|
821
|
-
version: row.sequence
|
|
822
|
-
}),
|
|
823
|
-
key: [
|
|
824
|
-
{ column: entityColumn, param: "entity" },
|
|
825
|
-
{ column: "ref", param: "ref" },
|
|
826
|
-
{ column: "lineage", param: "lineage" }
|
|
827
|
-
],
|
|
828
|
-
...placed,
|
|
829
|
-
seam,
|
|
830
|
-
table: revisionsTable
|
|
831
|
-
});
|
|
832
|
-
const needsLog = () => {
|
|
833
|
-
if (revisions === void 0) {
|
|
834
|
-
throw new import_db2.DbRefusal(
|
|
835
|
-
"this store has no log \u2014 `documentStore({ revisions: false })` opted out of the append-only record entirely, and the write model has no other door"
|
|
836
|
-
);
|
|
837
|
-
}
|
|
838
|
-
return revisions;
|
|
839
|
-
};
|
|
840
|
-
const readers = (0, import_db2.defineEntity)({
|
|
841
|
-
columns: { ...entityColumns, version: versionColumn },
|
|
842
|
-
filterable: [...physicalHeads, entityColumn, physical.createdAt, physical.updatedAt],
|
|
843
|
-
item: (row) => row,
|
|
844
|
-
key: { column: "number", param: "ref" },
|
|
845
|
-
order: [{ column: "number" }],
|
|
846
|
-
...placed,
|
|
847
|
-
seam,
|
|
848
|
-
sortable: [...physicalHeads, entityColumn, "number", physical.createdAt, physical.updatedAt],
|
|
849
|
-
table
|
|
850
|
-
});
|
|
851
|
-
const entityStoreFor = (name) => {
|
|
852
|
-
const definition = registry.named(name);
|
|
853
|
-
const item = (row) => {
|
|
854
|
-
const body = readBody(definition, name, row.key, row.body);
|
|
855
|
-
const head = definition.headOf(body);
|
|
856
|
-
return registry.instance(name, {
|
|
857
|
-
...head,
|
|
858
|
-
body,
|
|
859
|
-
createdAt: row.createdAt,
|
|
860
|
-
number: row.key,
|
|
861
|
-
updatedAt: row.updatedAt,
|
|
862
|
-
version: row.version ?? 1
|
|
863
|
-
});
|
|
864
|
-
};
|
|
865
|
-
const instanceFromRevision = (ref, row) => {
|
|
866
|
-
const body = readBody(definition, name, ref, row.body);
|
|
867
|
-
const head = definition.headOf(body);
|
|
868
|
-
return registry.instance(name, {
|
|
869
|
-
...head,
|
|
870
|
-
body,
|
|
871
|
-
createdAt: row.at,
|
|
872
|
-
number: ref,
|
|
873
|
-
updatedAt: row.at,
|
|
874
|
-
version: row.sequence
|
|
875
|
-
});
|
|
876
|
-
};
|
|
877
|
-
const projection = (0, import_db2.projectionOf)({
|
|
878
|
-
carried: ["lineage"],
|
|
879
|
-
columns: { ...entityColumns, hash: "hash", sequence: versionColumn },
|
|
880
|
-
constants: { [entityColumn]: name },
|
|
881
|
-
filterable: physicalHeads,
|
|
882
|
-
item,
|
|
883
|
-
key: { column: "number", param: "ref" },
|
|
884
|
-
order: [{ column: "number" }],
|
|
885
|
-
...placed,
|
|
886
|
-
seam,
|
|
887
|
-
sortable: [...physicalHeads, "number", physical.createdAt, physical.updatedAt],
|
|
888
|
-
table
|
|
889
|
-
});
|
|
890
|
-
const list = (handle, params) => projection.list(handle, {
|
|
891
|
-
...params,
|
|
892
|
-
...params.filter === void 0 ? {} : { filter: narrowed(params.filter) },
|
|
893
|
-
...params.order === void 0 ? {} : {
|
|
894
|
-
order: {
|
|
895
|
-
column: columnOf(params.order.column),
|
|
896
|
-
...params.order.direction === void 0 ? {} : { direction: params.order.direction }
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
});
|
|
900
|
-
const locked = async (tx, params) => headFrom(await projection.lockLatest(tx, params));
|
|
901
|
-
const currentLineageOf = async (tx, params) => headFrom(await projection.head(tx, params))?.lineage ?? 1;
|
|
902
|
-
const stream = (params, lineage) => ({
|
|
903
|
-
...tenanted(params[tenantKey]),
|
|
904
|
-
entity: name,
|
|
905
|
-
lineage,
|
|
906
|
-
ref: params.ref
|
|
907
|
-
});
|
|
908
|
-
const headColumnsOf = (body) => Object.fromEntries(
|
|
909
|
-
Object.entries(definition.headOf(body)).map(([key, held]) => [
|
|
910
|
-
columnOf(key),
|
|
911
|
-
held
|
|
912
|
-
])
|
|
913
|
-
);
|
|
914
|
-
const storedHead = (ref, body) => headColumnsOf(readBody(definition, name, ref, body));
|
|
915
|
-
const append = async (tx, write) => {
|
|
916
|
-
const log = needsLog();
|
|
917
|
-
const params = {
|
|
918
|
-
...tenanted(write.tenantId),
|
|
919
|
-
actor: write.actor,
|
|
920
|
-
body: write.body,
|
|
921
|
-
entity: name,
|
|
922
|
-
kind: write.kind,
|
|
923
|
-
lineage: write.lineage,
|
|
924
|
-
ref: write.ref,
|
|
925
|
-
...write.reason === null ? {} : { reason: write.reason }
|
|
926
|
-
};
|
|
927
|
-
const out = write.compensationOf === null ? await log.append(tx, params) : await log.appendCompensation(tx, { ...params, compensationOf: write.compensationOf });
|
|
928
|
-
return { at: out.at, hash: out.hash, prevHash: out.prevHash, version: out.sequence };
|
|
929
|
-
};
|
|
930
|
-
const project = async (tx, write) => projection.write(tx, {
|
|
931
|
-
...tenanted(write.tenantId),
|
|
932
|
-
at: write.at,
|
|
933
|
-
body: write.body,
|
|
934
|
-
carried: { lineage: write.lineage },
|
|
935
|
-
derived: write.head,
|
|
936
|
-
hash: write.hash,
|
|
937
|
-
ref: write.ref,
|
|
938
|
-
retiredAt: write.retiredAt,
|
|
939
|
-
sequence: write.version
|
|
940
|
-
});
|
|
941
|
-
const commitTx = seam.scoped(
|
|
942
|
-
async (tx, params) => {
|
|
943
|
-
const tenantId = params[tenantKey];
|
|
944
|
-
const prepared = definition.prepare(params.body);
|
|
945
|
-
const stamped = stampBody(definition, prepared);
|
|
946
|
-
const existing = await locked(tx, params);
|
|
947
|
-
if (existing !== void 0 && existing.retiredAt !== null) {
|
|
948
|
-
throw new import_db2.DbRefusal(
|
|
949
|
-
`"${params.ref}" (${name}) is retired: commit refuses a retired document \u2014 succeed opens a new lineage for it`
|
|
950
|
-
);
|
|
951
|
-
}
|
|
952
|
-
const changed = existing === void 0 || JSON.stringify(canonicalOf(existing.body)) !== JSON.stringify(canonicalOf(stamped));
|
|
953
|
-
if (existing !== void 0 && !changed) {
|
|
954
|
-
return {
|
|
955
|
-
committed: false,
|
|
956
|
-
lineage: existing.lineage,
|
|
957
|
-
ref: params.ref,
|
|
958
|
-
version: existing.version
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
const lineage = existing?.lineage ?? 1;
|
|
962
|
-
const reason = params.reason ?? null;
|
|
963
|
-
const appended = await append(tx, {
|
|
964
|
-
actor: params.actor,
|
|
965
|
-
body: stamped,
|
|
966
|
-
compensationOf: null,
|
|
967
|
-
kind: "committed",
|
|
968
|
-
lineage,
|
|
969
|
-
reason,
|
|
970
|
-
ref: params.ref,
|
|
971
|
-
tenantId
|
|
972
|
-
});
|
|
973
|
-
await project(tx, {
|
|
974
|
-
at: appended.at,
|
|
975
|
-
body: stamped,
|
|
976
|
-
hash: appended.hash,
|
|
977
|
-
head: headColumnsOf(prepared),
|
|
978
|
-
lineage,
|
|
979
|
-
ref: params.ref,
|
|
980
|
-
retiredAt: null,
|
|
981
|
-
tenantId,
|
|
982
|
-
version: appended.version
|
|
983
|
-
});
|
|
984
|
-
return {
|
|
985
|
-
archivable: {
|
|
986
|
-
actor: params.actor,
|
|
987
|
-
at: appended.at,
|
|
988
|
-
body: stamped,
|
|
989
|
-
prevHash: appended.prevHash,
|
|
990
|
-
reason
|
|
991
|
-
},
|
|
992
|
-
committed: true,
|
|
993
|
-
hash: appended.hash,
|
|
994
|
-
lineage,
|
|
995
|
-
previous: existing === void 0 ? null : { hash: existing.hash, version: existing.version },
|
|
996
|
-
ref: params.ref,
|
|
997
|
-
version: appended.version
|
|
998
|
-
};
|
|
999
|
-
}
|
|
1000
|
-
);
|
|
1001
|
-
const commit = async (handle, params) => {
|
|
1002
|
-
const { archivable, ...answer } = await commitTx(handle, params);
|
|
1003
|
-
if (archive === void 0 || !answer.committed || archivable === void 0) return answer;
|
|
1004
|
-
const key = `${params[tenantKey]}/${name}/${answer.lineage}/${answer.version}.json`;
|
|
1005
|
-
const payload = {
|
|
1006
|
-
actor: archivable.actor,
|
|
1007
|
-
at: archivable.at,
|
|
1008
|
-
body: archivable.body,
|
|
1009
|
-
entity: name,
|
|
1010
|
-
hash: answer.hash,
|
|
1011
|
-
kind: "committed",
|
|
1012
|
-
lineage: answer.lineage,
|
|
1013
|
-
prevHash: archivable.prevHash,
|
|
1014
|
-
ref: params.ref,
|
|
1015
|
-
reason: archivable.reason,
|
|
1016
|
-
tenant: params[tenantKey],
|
|
1017
|
-
version: answer.version
|
|
1018
|
-
};
|
|
1019
|
-
try {
|
|
1020
|
-
await archive.put(key, new TextEncoder().encode(JSON.stringify(payload)), {
|
|
1021
|
-
hash: answer.hash,
|
|
1022
|
-
immutable: true
|
|
1023
|
-
});
|
|
1024
|
-
return { ...answer, archived: true };
|
|
1025
|
-
} catch {
|
|
1026
|
-
return { ...answer, archived: false };
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
const retire = seam.scoped(
|
|
1030
|
-
async (tx, params) => {
|
|
1031
|
-
const tenantId = params[tenantKey];
|
|
1032
|
-
const existing = await locked(tx, params);
|
|
1033
|
-
if (existing === void 0) {
|
|
1034
|
-
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) has no document to retire`);
|
|
1035
|
-
}
|
|
1036
|
-
if (existing.retiredAt !== null) {
|
|
1037
|
-
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) is already retired`);
|
|
1038
|
-
}
|
|
1039
|
-
const appended = await append(tx, {
|
|
1040
|
-
actor: params.actor,
|
|
1041
|
-
body: existing.body,
|
|
1042
|
-
compensationOf: null,
|
|
1043
|
-
kind: "retired",
|
|
1044
|
-
lineage: existing.lineage,
|
|
1045
|
-
reason: params.reason ?? null,
|
|
1046
|
-
ref: params.ref,
|
|
1047
|
-
tenantId
|
|
1048
|
-
});
|
|
1049
|
-
await project(tx, {
|
|
1050
|
-
at: appended.at,
|
|
1051
|
-
body: existing.body,
|
|
1052
|
-
hash: appended.hash,
|
|
1053
|
-
head: storedHead(params.ref, existing.body),
|
|
1054
|
-
lineage: existing.lineage,
|
|
1055
|
-
ref: params.ref,
|
|
1056
|
-
retiredAt: appended.at,
|
|
1057
|
-
tenantId,
|
|
1058
|
-
version: appended.version
|
|
1059
|
-
});
|
|
1060
|
-
return {
|
|
1061
|
-
hash: appended.hash,
|
|
1062
|
-
lineage: existing.lineage,
|
|
1063
|
-
ref: params.ref,
|
|
1064
|
-
version: appended.version
|
|
1065
|
-
};
|
|
1066
|
-
}
|
|
1067
|
-
);
|
|
1068
|
-
const succeed = seam.scoped(
|
|
1069
|
-
async (tx, params) => {
|
|
1070
|
-
const tenantId = params[tenantKey];
|
|
1071
|
-
const existing = await locked(tx, params);
|
|
1072
|
-
if (existing === void 0 || existing.retiredAt === null) {
|
|
1073
|
-
throw new import_db2.DbRefusal(
|
|
1074
|
-
`"${params.ref}" (${name}) is not retired: succeed opens a new lineage only for a retired document \u2014 commit is the door for a live one`
|
|
1075
|
-
);
|
|
1076
|
-
}
|
|
1077
|
-
const prepared = definition.prepare(params.body);
|
|
1078
|
-
const stamped = stampBody(definition, prepared);
|
|
1079
|
-
const lineage = existing.lineage + 1;
|
|
1080
|
-
const appended = await append(tx, {
|
|
1081
|
-
actor: params.actor,
|
|
1082
|
-
body: stamped,
|
|
1083
|
-
compensationOf: null,
|
|
1084
|
-
kind: "committed",
|
|
1085
|
-
lineage,
|
|
1086
|
-
reason: params.reason ?? null,
|
|
1087
|
-
ref: params.ref,
|
|
1088
|
-
tenantId
|
|
1089
|
-
});
|
|
1090
|
-
await project(tx, {
|
|
1091
|
-
at: appended.at,
|
|
1092
|
-
body: stamped,
|
|
1093
|
-
hash: appended.hash,
|
|
1094
|
-
head: headColumnsOf(prepared),
|
|
1095
|
-
lineage,
|
|
1096
|
-
ref: params.ref,
|
|
1097
|
-
retiredAt: null,
|
|
1098
|
-
tenantId,
|
|
1099
|
-
version: appended.version
|
|
1100
|
-
});
|
|
1101
|
-
return {
|
|
1102
|
-
committed: true,
|
|
1103
|
-
hash: appended.hash,
|
|
1104
|
-
lineage,
|
|
1105
|
-
previous: null,
|
|
1106
|
-
ref: params.ref,
|
|
1107
|
-
version: appended.version
|
|
1108
|
-
};
|
|
1109
|
-
}
|
|
1110
|
-
);
|
|
1111
|
-
const reinstate = seam.scoped(
|
|
1112
|
-
async (tx, params) => {
|
|
1113
|
-
const tenantId = params[tenantKey];
|
|
1114
|
-
const existing = await locked(tx, params);
|
|
1115
|
-
if (existing === void 0 || existing.retiredAt === null) {
|
|
1116
|
-
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) is not retired: nothing to reinstate`);
|
|
1117
|
-
}
|
|
1118
|
-
const appended = await append(tx, {
|
|
1119
|
-
actor: params.actor,
|
|
1120
|
-
body: existing.body,
|
|
1121
|
-
compensationOf: null,
|
|
1122
|
-
kind: "reinstated",
|
|
1123
|
-
lineage: existing.lineage,
|
|
1124
|
-
reason: params.reason ?? null,
|
|
1125
|
-
ref: params.ref,
|
|
1126
|
-
tenantId
|
|
1127
|
-
});
|
|
1128
|
-
await project(tx, {
|
|
1129
|
-
at: appended.at,
|
|
1130
|
-
body: existing.body,
|
|
1131
|
-
hash: appended.hash,
|
|
1132
|
-
head: storedHead(params.ref, existing.body),
|
|
1133
|
-
lineage: existing.lineage,
|
|
1134
|
-
ref: params.ref,
|
|
1135
|
-
retiredAt: null,
|
|
1136
|
-
tenantId,
|
|
1137
|
-
version: appended.version
|
|
1138
|
-
});
|
|
1139
|
-
return {
|
|
1140
|
-
hash: appended.hash,
|
|
1141
|
-
lineage: existing.lineage,
|
|
1142
|
-
ref: params.ref,
|
|
1143
|
-
version: appended.version
|
|
1144
|
-
};
|
|
1145
|
-
}
|
|
1146
|
-
);
|
|
1147
|
-
const at = seam.scoped(
|
|
1148
|
-
async (tx, params) => {
|
|
1149
|
-
const lineage = await currentLineageOf(tx, params);
|
|
1150
|
-
const row = await needsLog().at(tx, {
|
|
1151
|
-
...stream(params, lineage),
|
|
1152
|
-
sequence: params.version
|
|
1153
|
-
});
|
|
1154
|
-
return row === null ? null : instanceFromRevision(params.ref, row);
|
|
1155
|
-
}
|
|
1156
|
-
);
|
|
1157
|
-
const asOf = seam.scoped(
|
|
1158
|
-
async (tx, params) => {
|
|
1159
|
-
const lineage = await currentLineageOf(tx, params);
|
|
1160
|
-
const row = await needsLog().asOf(tx, { ...stream(params, lineage), at: params.at });
|
|
1161
|
-
return row === null ? null : instanceFromRevision(params.ref, row);
|
|
1162
|
-
}
|
|
1163
|
-
);
|
|
1164
|
-
const history = seam.scoped(
|
|
1165
|
-
async (tx, params) => {
|
|
1166
|
-
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
1167
|
-
const rows = await needsLog().history(tx, stream(params, lineage));
|
|
1168
|
-
return rows.map((row) => ({
|
|
1169
|
-
actor: row.actor,
|
|
1170
|
-
at: row.at,
|
|
1171
|
-
body: readBody(definition, name, `${params.ref} version ${row.sequence}`, row.body),
|
|
1172
|
-
hash: row.hash,
|
|
1173
|
-
kind: row.kind,
|
|
1174
|
-
reason: row.reason,
|
|
1175
|
-
version: row.sequence
|
|
1176
|
-
}));
|
|
1177
|
-
}
|
|
1178
|
-
);
|
|
1179
|
-
const verify = seam.scoped(
|
|
1180
|
-
async (tx, params) => {
|
|
1181
|
-
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
1182
|
-
return needsLog().verify(tx, stream(params, lineage));
|
|
1183
|
-
}
|
|
1184
|
-
);
|
|
1185
|
-
const compensate = async (scoped, tenantId, output, kind, body, retired) => {
|
|
1186
|
-
const appended = await append(scoped, {
|
|
1187
|
-
actor: UNDO_ACTOR,
|
|
1188
|
-
body,
|
|
1189
|
-
compensationOf: output.version,
|
|
1190
|
-
kind,
|
|
1191
|
-
lineage: output.lineage,
|
|
1192
|
-
reason: `undo of v${output.version}`,
|
|
1193
|
-
ref: output.ref,
|
|
1194
|
-
tenantId
|
|
1195
|
-
});
|
|
1196
|
-
await project(scoped, {
|
|
1197
|
-
at: appended.at,
|
|
1198
|
-
body,
|
|
1199
|
-
hash: appended.hash,
|
|
1200
|
-
head: storedHead(output.ref, body),
|
|
1201
|
-
lineage: output.lineage,
|
|
1202
|
-
ref: output.ref,
|
|
1203
|
-
retiredAt: retired ? appended.at : null,
|
|
1204
|
-
tenantId,
|
|
1205
|
-
version: appended.version
|
|
1206
|
-
});
|
|
1207
|
-
};
|
|
1208
|
-
const lockedForUndo = async (scoped, tenantId, output) => {
|
|
1209
|
-
const existing = await locked(scoped, { ...tenanted(tenantId), ref: output.ref });
|
|
1210
|
-
if (existing === void 0) {
|
|
1211
|
-
throw new import_db2.DbRefusal(
|
|
1212
|
-
`"${output.ref}" (${name}) has nothing to undo \u2014 the row v${output.version} wrote is gone`
|
|
1213
|
-
);
|
|
1214
|
-
}
|
|
1215
|
-
return existing;
|
|
1216
|
-
};
|
|
1217
|
-
const undos = {
|
|
1218
|
-
commit: (tx, { output, tenantId }) => seam.inTenant(tx, tenantId, async (scoped) => {
|
|
1219
|
-
if (!output.committed) return;
|
|
1220
|
-
const existing = await lockedForUndo(scoped, tenantId, output);
|
|
1221
|
-
if (output.previous === null) {
|
|
1222
|
-
await compensate(scoped, tenantId, output, "retired", existing.body, true);
|
|
1223
|
-
return;
|
|
1224
|
-
}
|
|
1225
|
-
const prior = await needsLog().at(scoped, {
|
|
1226
|
-
...stream({ ...tenanted(tenantId), ref: output.ref }, output.lineage),
|
|
1227
|
-
sequence: output.previous.version
|
|
1228
|
-
});
|
|
1229
|
-
if (prior === null) {
|
|
1230
|
-
throw new import_db2.DbRefusal(
|
|
1231
|
-
`"${output.ref}" (${name}) version ${output.previous.version} is gone \u2014 cannot undo v${output.version} without it`
|
|
1232
|
-
);
|
|
1233
|
-
}
|
|
1234
|
-
await compensate(scoped, tenantId, output, "committed", prior.body, false);
|
|
1235
|
-
}),
|
|
1236
|
-
retire: (tx, { output, tenantId }) => seam.inTenant(tx, tenantId, async (scoped) => {
|
|
1237
|
-
const existing = await lockedForUndo(scoped, tenantId, output);
|
|
1238
|
-
await compensate(scoped, tenantId, output, "reinstated", existing.body, false);
|
|
1239
|
-
})
|
|
1240
|
-
};
|
|
1241
|
-
const restore = async (tx, row, createdAt) => {
|
|
1242
|
-
const ref = String(row.ref);
|
|
1243
|
-
await project(tx, {
|
|
1244
|
-
at: createdAt,
|
|
1245
|
-
body: row.body,
|
|
1246
|
-
hash: row.hash,
|
|
1247
|
-
head: storedHead(ref, row.body),
|
|
1248
|
-
lineage: Number(row.lineage),
|
|
1249
|
-
ref,
|
|
1250
|
-
retiredAt: row.kind === "retired" ? row.at : null,
|
|
1251
|
-
tenantId: row.tenant,
|
|
1252
|
-
version: row.sequence
|
|
1253
|
-
});
|
|
1254
|
-
};
|
|
1255
|
-
return {
|
|
1256
|
-
door: {
|
|
1257
|
-
asOf,
|
|
1258
|
-
at,
|
|
1259
|
-
commit,
|
|
1260
|
-
get: projection.get,
|
|
1261
|
-
history,
|
|
1262
|
-
list,
|
|
1263
|
-
listAll: projection.listAll,
|
|
1264
|
-
reinstate,
|
|
1265
|
-
retire,
|
|
1266
|
-
succeed,
|
|
1267
|
-
undos,
|
|
1268
|
-
verify
|
|
1269
|
-
},
|
|
1270
|
-
restore
|
|
1271
|
-
};
|
|
1272
|
-
};
|
|
1273
|
-
const built = registry.names.map((name) => [name, entityStoreFor(name)]);
|
|
1274
|
-
const rawEntities = Object.fromEntries(built.map(([name, one]) => [name, one.door]));
|
|
1275
|
-
const restorers = Object.fromEntries(
|
|
1276
|
-
built.map(([name, one]) => [name, one.restore])
|
|
1277
|
-
);
|
|
1278
|
-
const heads = seam.scoped(
|
|
1279
|
-
async (tx, params) => {
|
|
1280
|
-
const { columns, entity, order, where } = params;
|
|
1281
|
-
const rows = await readers.project(tx, {
|
|
1282
|
-
...tenanted(params[tenantKey]),
|
|
1283
|
-
columns: columns.filter((column) => column !== "number").map(columnOf),
|
|
1284
|
-
order: order.map((one) => ({
|
|
1285
|
-
column: columnOf(one.column),
|
|
1286
|
-
...one.direction === void 0 ? {} : { direction: one.direction }
|
|
1287
|
-
})),
|
|
1288
|
-
where: {
|
|
1289
|
-
...entity === void 0 ? {} : { [entityColumn]: entity },
|
|
1290
|
-
...narrowed(where)
|
|
1291
|
-
}
|
|
1292
|
-
});
|
|
1293
|
-
return rows.map(
|
|
1294
|
-
(row) => Object.fromEntries(
|
|
1295
|
-
columns.map((column) => [column, column === "number" ? row.key : row[columnOf(column)]])
|
|
1296
|
-
)
|
|
1297
|
-
);
|
|
1298
|
-
}
|
|
1299
|
-
);
|
|
1300
|
-
const aggregate = seam.scoped(
|
|
1301
|
-
(tx, params) => {
|
|
1302
|
-
const { by, entities, entity, measures, where } = params;
|
|
1303
|
-
return readers.aggregate(tx, {
|
|
1304
|
-
...tenanted(params[tenantKey]),
|
|
1305
|
-
...by === void 0 ? {} : {
|
|
1306
|
-
by: typeof by === "string" ? columnOf(by) : { bucket: by.bucket, of: columnOf(by.of) }
|
|
1307
|
-
},
|
|
1308
|
-
measures: measures.map(
|
|
1309
|
-
(one) => one === "sum:total" ? `sum:${physical.total}` : one
|
|
1310
|
-
),
|
|
1311
|
-
where: {
|
|
1312
|
-
...entity === void 0 ? {} : { [entityColumn]: entity },
|
|
1313
|
-
...entities === void 0 ? {} : { [entityColumn]: entities },
|
|
1314
|
-
...narrowed(where)
|
|
1315
|
-
}
|
|
1316
|
-
});
|
|
1317
|
-
}
|
|
1318
|
-
);
|
|
1319
|
-
const rebuild = seam.scoped(
|
|
1320
|
-
async (tx, params) => {
|
|
1321
|
-
const log = needsLog();
|
|
1322
|
-
const tenant = tenanted(params[tenantKey]);
|
|
1323
|
-
const latest = /* @__PURE__ */ new Map();
|
|
1324
|
-
for (const row of await log.latestPerStream(tx, tenant)) {
|
|
1325
|
-
if (params.entity !== void 0 && row.entity !== params.entity) continue;
|
|
1326
|
-
const address = `${String(row.entity)} ${String(row.ref)}`;
|
|
1327
|
-
const held = latest.get(address);
|
|
1328
|
-
if (held === void 0 || Number(row.lineage) > Number(held.lineage)) {
|
|
1329
|
-
latest.set(address, row);
|
|
1330
|
-
}
|
|
1331
|
-
}
|
|
1332
|
-
let rows = 0;
|
|
1333
|
-
for (const row of latest.values()) {
|
|
1334
|
-
const restore = restorers[String(row.entity)];
|
|
1335
|
-
if (restore === void 0) continue;
|
|
1336
|
-
const first = await log.at(tx, {
|
|
1337
|
-
...tenant,
|
|
1338
|
-
entity: row.entity,
|
|
1339
|
-
lineage: row.lineage,
|
|
1340
|
-
ref: row.ref,
|
|
1341
|
-
sequence: 1
|
|
1342
|
-
});
|
|
1343
|
-
await restore(tx, row, first?.at ?? row.at);
|
|
1344
|
-
rows += 1;
|
|
1345
|
-
}
|
|
1346
|
-
return { rows };
|
|
1347
|
-
}
|
|
1348
|
-
);
|
|
1349
|
-
return { ...rawEntities, aggregate, heads, rebuild };
|
|
1350
|
-
};
|
|
1351
217
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1352
218
|
0 && (module.exports = {
|
|
1353
219
|
BODY_VERSION_KEY,
|
|
1354
|
-
DEFAULT_DOCUMENT_TABLE,
|
|
1355
|
-
DEFAULT_REVISIONS_TABLE,
|
|
1356
|
-
DEFAULT_REVISION_KIND_COLUMN,
|
|
1357
|
-
DOCUMENT_MIGRATION_PLACEHOLDERS,
|
|
1358
220
|
Document,
|
|
1359
|
-
HEAD_FIELDS,
|
|
1360
|
-
appendOnlyTables,
|
|
1361
221
|
canTransition,
|
|
1362
222
|
canonicalOf,
|
|
1363
223
|
documentBodyVersioning,
|
|
1364
224
|
documentEntities,
|
|
1365
225
|
documentIdentifiers,
|
|
1366
|
-
documentMigrations,
|
|
1367
|
-
documentRoleStatements,
|
|
1368
|
-
documentStore,
|
|
1369
|
-
documentSurface,
|
|
1370
226
|
fieldPatterns,
|
|
1371
|
-
hashOf,
|
|
1372
227
|
isSystemActor,
|
|
1373
228
|
isTerminalStatus,
|
|
1374
229
|
migrateBody,
|
|
1375
|
-
physicalColumnsOf,
|
|
1376
230
|
stampBody,
|
|
1377
231
|
systemActor,
|
|
1378
232
|
validatorOf
|