@geonosis/document 0.2.0 → 0.4.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 +64 -0
- package/README.md +78 -34
- package/dist/index.cjs +811 -751
- package/dist/index.d.cts +124 -95
- package/dist/index.d.ts +124 -95
- package/dist/index.js +812 -753
- package/migrations/0001_documents.sql +22 -14
- package/migrations/0003_document_revisions.sql +4 -5
- package/package.json +2 -3
package/dist/index.cjs
CHANGED
|
@@ -26,6 +26,7 @@ __export(index_exports, {
|
|
|
26
26
|
DEFAULT_REVISION_KIND_COLUMN: () => DEFAULT_REVISION_KIND_COLUMN,
|
|
27
27
|
DOCUMENT_MIGRATION_PLACEHOLDERS: () => import_db.ENTITY_PLACEHOLDERS,
|
|
28
28
|
Document: () => Document,
|
|
29
|
+
HEAD_FIELDS: () => HEAD_FIELDS,
|
|
29
30
|
appendOnlyTables: () => appendOnlyTables,
|
|
30
31
|
canTransition: () => canTransition,
|
|
31
32
|
canonicalOf: () => canonicalOf,
|
|
@@ -35,11 +36,13 @@ __export(index_exports, {
|
|
|
35
36
|
documentMigrations: () => documentMigrations,
|
|
36
37
|
documentRoleStatements: () => documentRoleStatements,
|
|
37
38
|
documentStore: () => documentStore,
|
|
39
|
+
documentSurface: () => documentSurface,
|
|
38
40
|
fieldPatterns: () => fieldPatterns,
|
|
39
41
|
hashOf: () => hashOf,
|
|
40
42
|
isSystemActor: () => isSystemActor,
|
|
41
43
|
isTerminalStatus: () => isTerminalStatus,
|
|
42
44
|
migrateBody: () => migrateBody,
|
|
45
|
+
physicalColumnsOf: () => physicalColumnsOf,
|
|
43
46
|
stampBody: () => stampBody,
|
|
44
47
|
systemActor: () => systemActor,
|
|
45
48
|
validatorOf: () => validatorOf
|
|
@@ -99,7 +102,7 @@ var FIELD_CHECKS = {
|
|
|
99
102
|
text: { message: "a string", test: (value) => typeof value === "string" }
|
|
100
103
|
};
|
|
101
104
|
var validatorOf = (fields, options) => {
|
|
102
|
-
const
|
|
105
|
+
const nullable2 = options?.nullable ?? false;
|
|
103
106
|
return {
|
|
104
107
|
"~standard": {
|
|
105
108
|
validate: (input) => {
|
|
@@ -112,7 +115,7 @@ var validatorOf = (fields, options) => {
|
|
|
112
115
|
for (const [key, kind] of Object.entries(fields)) {
|
|
113
116
|
const held = record[key];
|
|
114
117
|
value[key] = held;
|
|
115
|
-
if (
|
|
118
|
+
if (nullable2 && held === null) continue;
|
|
116
119
|
if (!FIELD_CHECKS[kind].test(held)) {
|
|
117
120
|
issues.push({ message: FIELD_CHECKS[kind].message, path: [{ key }] });
|
|
118
121
|
}
|
|
@@ -160,7 +163,8 @@ var Document = {
|
|
|
160
163
|
extend: (definition) => ({
|
|
161
164
|
...definition,
|
|
162
165
|
headOf: (body) => parseWith(headValidator, definition.headOf(body)),
|
|
163
|
-
is: (document) => document.entity === definition.name
|
|
166
|
+
is: (document) => document.entity === definition.name,
|
|
167
|
+
view: definition.view ?? definition.body
|
|
164
168
|
})
|
|
165
169
|
};
|
|
166
170
|
|
|
@@ -186,7 +190,8 @@ var documentEntities = (map) => {
|
|
|
186
190
|
fold,
|
|
187
191
|
instance: (name, parts) => ({ ...parts, entity: name, body: parts.body }),
|
|
188
192
|
named: (name) => map[name],
|
|
189
|
-
names
|
|
193
|
+
names,
|
|
194
|
+
views: distinctBy((entity) => entity.view)
|
|
190
195
|
};
|
|
191
196
|
};
|
|
192
197
|
|
|
@@ -224,20 +229,433 @@ var documentIdentifiers = (name) => {
|
|
|
224
229
|
var canTransition = (machine, from, to) => machine.transitions[from].includes(to);
|
|
225
230
|
var isTerminalStatus = (machine, status) => machine.transitions[status].length === 0;
|
|
226
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
|
+
|
|
227
621
|
// src/store/ddl.ts
|
|
228
622
|
var import_db = require("@geonosis/db");
|
|
229
623
|
var DEFAULT_DOCUMENT_TABLE = "documents";
|
|
230
624
|
var DEFAULT_ENTITY_COLUMN = "entity";
|
|
231
625
|
var DEFAULT_VERSION_COLUMN = "version";
|
|
232
|
-
var DEFAULT_TOTAL_COLUMN = "total";
|
|
233
626
|
var DEFAULT_TENANT_COLUMN = "tenant_id";
|
|
627
|
+
var DEFAULT_CREATED_AT_COLUMN = "created_at";
|
|
628
|
+
var DEFAULT_UPDATED_AT_COLUMN = "updated_at";
|
|
234
629
|
var DEFAULT_REVISIONS_TABLE = "document_revisions";
|
|
235
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()}`);
|
|
236
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
|
+
};
|
|
237
653
|
var appendOnlyTables = (options = {}) => {
|
|
238
654
|
if (options.revisions === false) return [];
|
|
239
655
|
return [options.revisions?.table ?? options.table ?? DEFAULT_REVISIONS_TABLE];
|
|
240
656
|
};
|
|
657
|
+
var quotedName = (name) => `"${name}"`;
|
|
658
|
+
var literalName = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
241
659
|
var documentRoleStatements = (appRole, options = {}) => {
|
|
242
660
|
if (typeof appRole !== "string" || appRole === "") {
|
|
243
661
|
throw new import_db.DbRefusal(
|
|
@@ -252,143 +670,91 @@ var documentRoleStatements = (appRole, options = {}) => {
|
|
|
252
670
|
]);
|
|
253
671
|
};
|
|
254
672
|
var DDL_TYPE_OF = { decimal: "numeric" };
|
|
255
|
-
var headIndexedColumns = (
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
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
|
+
};
|
|
259
680
|
});
|
|
260
|
-
var
|
|
261
|
-
var
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (revisions === false) return [];
|
|
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 [];
|
|
268
689
|
if (typeof options.appRole !== "string" || options.appRole === "") {
|
|
269
690
|
throw new import_db.DbRefusal(
|
|
270
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`"
|
|
271
692
|
);
|
|
272
693
|
}
|
|
273
|
-
const appRole = options.appRole;
|
|
274
694
|
const revisionKindColumn = options.columns?.revisionKind ?? DEFAULT_REVISION_KIND_COLUMN;
|
|
275
|
-
const table = revisions?.table ?? DEFAULT_REVISIONS_TABLE;
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
columns: { tenant:
|
|
279
|
-
|
|
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),
|
|
280
702
|
settings: options.settings,
|
|
281
703
|
table
|
|
282
704
|
});
|
|
283
|
-
if (policies === void 0) {
|
|
284
|
-
throw new Error(
|
|
285
|
-
"entityMigrations answered with no policies migration to borrow for the log\u2019s tenant wall"
|
|
286
|
-
);
|
|
705
|
+
if (tableMigration === void 0 || policies === void 0) {
|
|
706
|
+
throw new Error("logMigrations answered with fewer than the two migrations a log renders");
|
|
287
707
|
}
|
|
288
|
-
const functionName = immutableFunctionName(table);
|
|
289
|
-
const triggerName = noUpdateOrDeleteTriggerName(table);
|
|
290
|
-
const qualifiedFunction = options.schema === void 0 ? quotedName(functionName) : `${quotedName(options.schema)}.${quotedName(functionName)}`;
|
|
291
708
|
return [
|
|
292
|
-
{
|
|
293
|
-
|
|
294
|
-
statements: [
|
|
295
|
-
`create table if not exists ${on} (
|
|
296
|
-
${quotedName(tenantColumn)} text not null default ${settingDefault(options.settings.tenantSetting)},
|
|
297
|
-
${quotedName(entityColumn)} text not null,
|
|
298
|
-
${quotedName("ref")} text not null,
|
|
299
|
-
${quotedName("lineage")} integer not null,
|
|
300
|
-
${quotedName("version")} integer not null,
|
|
301
|
-
${quotedName(revisionKindColumn)} text not null,
|
|
302
|
-
${quotedName("body")} jsonb not null,
|
|
303
|
-
${quotedName("actor")} text not null,
|
|
304
|
-
${quotedName("reason")} text,
|
|
305
|
-
${quotedName("at")} bigint not null default (extract(epoch from clock_timestamp()) * 1000)::bigint,
|
|
306
|
-
${quotedName("prev_hash")} text,
|
|
307
|
-
${quotedName("hash")} text not null,
|
|
308
|
-
${quotedName("compensation_of")} integer,
|
|
309
|
-
constraint ${quotedName(`${table}_pkey`)} primary key (${quotedName(tenantColumn)}, ${quotedName(entityColumn)}, ${quotedName("ref")}, ${quotedName("lineage")}, ${quotedName("version")}),
|
|
310
|
-
constraint ${quotedName(`${table}_${revisionKindColumn}_check`)} check (${quotedName(revisionKindColumn)} in ('committed', 'retired', 'reinstated'))
|
|
311
|
-
)`
|
|
312
|
-
]
|
|
313
|
-
},
|
|
314
|
-
{
|
|
315
|
-
...policies,
|
|
316
|
-
name: `0004_${table}_policies.sql`,
|
|
317
|
-
statements: [
|
|
318
|
-
...policies.statements,
|
|
319
|
-
`create or replace function ${qualifiedFunction}() returns trigger language plpgsql as $$ begin raise exception 'this table is append-only: % is not permitted', tg_op; end; $$`,
|
|
320
|
-
`drop trigger if exists ${quotedName(triggerName)} on ${on}`,
|
|
321
|
-
`create trigger ${quotedName(triggerName)} before update or delete on ${on} for each row execute function ${qualifiedFunction}()`,
|
|
322
|
-
`do $$ begin if exists (select from pg_roles where rolname = ${literalName(appRole)}) then execute 'revoke update, delete on ${on} from ' || quote_ident(${literalName(appRole)}); end if; end $$`
|
|
323
|
-
]
|
|
324
|
-
}
|
|
709
|
+
{ ...tableMigration, name: `0003_${table}.sql` },
|
|
710
|
+
{ ...policies, name: `0004_${table}_policies.sql` }
|
|
325
711
|
];
|
|
326
712
|
};
|
|
327
713
|
var documentMigrations = (options) => {
|
|
328
714
|
const table = options.table ?? DEFAULT_DOCUMENT_TABLE;
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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),
|
|
337
729
|
key: "number",
|
|
338
730
|
order: [{ column: "number" }],
|
|
339
|
-
...options
|
|
731
|
+
...walls(options),
|
|
340
732
|
settings: options.settings,
|
|
341
|
-
sortable:
|
|
733
|
+
sortable: [
|
|
734
|
+
...HEAD_FIELDS.map((field) => physical[field]),
|
|
735
|
+
physical.createdAt,
|
|
736
|
+
physical.updatedAt
|
|
737
|
+
],
|
|
342
738
|
table
|
|
343
739
|
});
|
|
344
|
-
const [tableMigration, ...rest] = migrations;
|
|
345
740
|
if (tableMigration === void 0) {
|
|
346
741
|
throw new Error(
|
|
347
|
-
"
|
|
742
|
+
"projectionMigrations answered with no table migration to append the listing index to"
|
|
348
743
|
);
|
|
349
744
|
}
|
|
350
|
-
const listingIndex = `create index if not exists ${quotedName(`${table}_${
|
|
351
|
-
const on = options.schema === void 0 ? quotedName(table) : `${quotedName(options.schema)}.${quotedName(table)}`;
|
|
352
|
-
const lineageAndHash = [
|
|
353
|
-
`alter table ${on} add column if not exists ${quotedName("lineage")} integer not null default 1`,
|
|
354
|
-
`alter table ${on} add column if not exists ${quotedName("hash")} text`
|
|
355
|
-
];
|
|
745
|
+
const listingIndex = `create index if not exists ${quotedName(`${table}_${physical.entity}_${physical.issueDate}_idx`)} on ${quotedName(table)} (${quotedName(physical.entity)}, ${quotedName(physical.issueDate)})`;
|
|
356
746
|
return [
|
|
357
|
-
{
|
|
358
|
-
...tableMigration,
|
|
359
|
-
statements: [...tableMigration.statements, listingIndex, ...lineageAndHash]
|
|
360
|
-
},
|
|
747
|
+
{ ...tableMigration, statements: [...tableMigration.statements, listingIndex] },
|
|
361
748
|
...rest,
|
|
362
|
-
...revisionsMigrations(options,
|
|
749
|
+
...revisionsMigrations(options, physical)
|
|
363
750
|
];
|
|
364
751
|
};
|
|
365
752
|
|
|
366
753
|
// src/store/store.ts
|
|
367
|
-
var import_conformance = require("@geonosis/conformance");
|
|
368
754
|
var import_db2 = require("@geonosis/db");
|
|
369
|
-
var DEFAULT_ENTITY_COLUMN2 = "entity";
|
|
370
755
|
var DEFAULT_VERSION_COLUMN2 = "version";
|
|
371
|
-
var DEFAULT_TOTAL_COLUMN2 = "total";
|
|
372
|
-
var DEFAULT_COLUMNS = { createdAt: "created_at", retiredAt: "retired_at", tenant: "tenant_id" };
|
|
373
756
|
var UNDO_ACTOR = systemActor("document.undo");
|
|
374
757
|
var nameOf2 = (value, fallback) => value === void 0 ? fallback : typeof value === "string" ? value : value.name;
|
|
375
|
-
var quote = (name) => `"${name}"`;
|
|
376
|
-
var chain = (before, after) => ({
|
|
377
|
-
chunks: [
|
|
378
|
-
...before.chunks.slice(0, -1),
|
|
379
|
-
`${before.chunks.at(-1) ?? ""}${after.chunks[0] ?? ""}`,
|
|
380
|
-
...after.chunks.slice(1)
|
|
381
|
-
],
|
|
382
|
-
values: [...before.values, ...after.values]
|
|
383
|
-
});
|
|
384
|
-
var chainAll = (pieces) => pieces.reduce((built, piece) => chain(built, piece), (0, import_db2.fragment)([""]));
|
|
385
|
-
var text = (chunk) => (0, import_db2.fragment)([chunk]);
|
|
386
|
-
var bound = (held) => (0, import_db2.fragment)(["", ""], [held]);
|
|
387
|
-
var setList = (columns) => columns.flatMap((one, at) => [
|
|
388
|
-
text(`${at === 0 ? "" : ", "}${quote(one.column)} = `),
|
|
389
|
-
bound(one.held)
|
|
390
|
-
]);
|
|
391
|
-
var valuesList = (columns) => columns.flatMap((one, at) => [text(at === 0 ? "" : ", "), bound(one.held)]);
|
|
392
758
|
var readBody = (entity, entityName, ref, stored) => {
|
|
393
759
|
const migrated = migrateBody(entity, stored);
|
|
394
760
|
const result = entity.body["~standard"].validate(migrated);
|
|
@@ -404,289 +770,84 @@ var readBody = (entity, entityName, ref, stored) => {
|
|
|
404
770
|
}
|
|
405
771
|
return result.value;
|
|
406
772
|
};
|
|
407
|
-
var
|
|
408
|
-
|
|
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
|
+
};
|
|
409
780
|
var documentStore = (options) => {
|
|
410
781
|
const table = options.table ?? DEFAULT_DOCUMENT_TABLE;
|
|
411
|
-
const entityColumn = options.columns?.entity ?? DEFAULT_ENTITY_COLUMN2;
|
|
412
|
-
const versionColumn = nameOf2(options.columns?.version, DEFAULT_VERSION_COLUMN2);
|
|
413
|
-
const tenantColumn = nameOf2(options.columns?.tenant, DEFAULT_COLUMNS.tenant);
|
|
414
|
-
const createdAtColumn = nameOf2(options.columns?.createdAt, DEFAULT_COLUMNS.createdAt);
|
|
415
|
-
const retiredAtColumn = nameOf2(options.columns?.retiredAt, DEFAULT_COLUMNS.retiredAt);
|
|
416
|
-
const totalColumn = nameOf2(options.columns?.total, DEFAULT_TOTAL_COLUMN2);
|
|
417
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;
|
|
418
794
|
const { archive, registry, schema, seam } = options;
|
|
419
795
|
const tenantKey = seam.tenantKey;
|
|
796
|
+
const placed = schema === void 0 ? {} : { schema };
|
|
420
797
|
const revisionsTable = options.revisions === false ? void 0 : options.revisions?.table ?? DEFAULT_REVISIONS_TABLE;
|
|
421
|
-
const
|
|
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
|
+
});
|
|
422
832
|
const needsLog = () => {
|
|
423
|
-
if (
|
|
833
|
+
if (revisions === void 0) {
|
|
424
834
|
throw new import_db2.DbRefusal(
|
|
425
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"
|
|
426
836
|
);
|
|
427
837
|
}
|
|
428
|
-
return
|
|
429
|
-
};
|
|
430
|
-
const currentDbTime = async (tx) => {
|
|
431
|
-
const [row] = (0, import_conformance.rowsOf)(
|
|
432
|
-
await seam.send(
|
|
433
|
-
tx,
|
|
434
|
-
text('select (extract(epoch from clock_timestamp()) * 1000)::bigint as "now"')
|
|
435
|
-
)
|
|
436
|
-
);
|
|
437
|
-
return Number(row?.now ?? Date.now());
|
|
438
|
-
};
|
|
439
|
-
const lockedProjection = async (tx, tenantId, entityName, ref) => {
|
|
440
|
-
const [row] = (0, import_conformance.rowsOf)(
|
|
441
|
-
await seam.send(
|
|
442
|
-
tx,
|
|
443
|
-
chainAll([
|
|
444
|
-
text(
|
|
445
|
-
`select ${quote("body")} as "body", ${quote(versionColumn)} as "version", ${quote("lineage")} as "lineage", ${quote("hash")} as "hash", ${quote(retiredAtColumn)} as "retiredAt" from ${quote(table)} where ${quote(tenantColumn)} = `
|
|
446
|
-
),
|
|
447
|
-
bound(tenantId),
|
|
448
|
-
text(` and ${quote(entityColumn)} = `),
|
|
449
|
-
bound(entityName),
|
|
450
|
-
text(` and ${quote("number")} = `),
|
|
451
|
-
bound(ref),
|
|
452
|
-
text(" for update")
|
|
453
|
-
])
|
|
454
|
-
)
|
|
455
|
-
);
|
|
456
|
-
if (row === void 0) return void 0;
|
|
457
|
-
return {
|
|
458
|
-
body: row.body,
|
|
459
|
-
hash: row.hash ?? "",
|
|
460
|
-
lineage: row.lineage,
|
|
461
|
-
retiredAt: row.retiredAt === null ? null : Number(row.retiredAt),
|
|
462
|
-
version: row.version
|
|
463
|
-
};
|
|
464
|
-
};
|
|
465
|
-
const currentLineageOf = async (tx, tenantId, entityName, ref) => {
|
|
466
|
-
const [row] = (0, import_conformance.rowsOf)(
|
|
467
|
-
await seam.send(
|
|
468
|
-
tx,
|
|
469
|
-
chainAll([
|
|
470
|
-
text(
|
|
471
|
-
`select ${quote("lineage")} as "lineage" from ${quote(table)} where ${quote(tenantColumn)} = `
|
|
472
|
-
),
|
|
473
|
-
bound(tenantId),
|
|
474
|
-
text(` and ${quote(entityColumn)} = `),
|
|
475
|
-
bound(entityName),
|
|
476
|
-
text(` and ${quote("number")} = `),
|
|
477
|
-
bound(ref)
|
|
478
|
-
])
|
|
479
|
-
)
|
|
480
|
-
);
|
|
481
|
-
return row?.lineage ?? 1;
|
|
482
|
-
};
|
|
483
|
-
const insertRevisionRow = async (tx, write) => {
|
|
484
|
-
const log = needsLog();
|
|
485
|
-
const hash = hashOf({
|
|
486
|
-
actor: write.actor,
|
|
487
|
-
at: write.at,
|
|
488
|
-
body: write.body,
|
|
489
|
-
entity: write.entityName,
|
|
490
|
-
kind: write.kind,
|
|
491
|
-
lineage: write.lineage,
|
|
492
|
-
prevHash: write.prevHash,
|
|
493
|
-
reason: write.reason,
|
|
494
|
-
ref: write.ref,
|
|
495
|
-
tenant: write.tenantId,
|
|
496
|
-
version: write.version
|
|
497
|
-
});
|
|
498
|
-
await seam.send(
|
|
499
|
-
tx,
|
|
500
|
-
chainAll([
|
|
501
|
-
text(
|
|
502
|
-
`insert into ${quote(log)} (${quote(tenantColumn)}, ${quote(entityColumn)}, ${quote("ref")}, ${quote("lineage")}, ${quote("version")}, ${quote(revisionKindColumn)}, ${quote("body")}, ${quote("actor")}, ${quote("reason")}, ${quote("at")}, ${quote("prev_hash")}, ${quote("hash")}, ${quote("compensation_of")}) values (`
|
|
503
|
-
),
|
|
504
|
-
...valuesList([
|
|
505
|
-
{ column: tenantColumn, held: write.tenantId },
|
|
506
|
-
{ column: entityColumn, held: write.entityName },
|
|
507
|
-
{ column: "ref", held: write.ref },
|
|
508
|
-
{ column: "lineage", held: write.lineage },
|
|
509
|
-
{ column: "version", held: write.version },
|
|
510
|
-
{ column: revisionKindColumn, held: write.kind },
|
|
511
|
-
{ column: "body", held: write.body },
|
|
512
|
-
{ column: "actor", held: write.actor },
|
|
513
|
-
{ column: "reason", held: write.reason },
|
|
514
|
-
{ column: "at", held: write.at },
|
|
515
|
-
{ column: "prev_hash", held: write.prevHash },
|
|
516
|
-
{ column: "hash", held: hash },
|
|
517
|
-
{ column: "compensation_of", held: write.compensationOf }
|
|
518
|
-
]),
|
|
519
|
-
text(")")
|
|
520
|
-
])
|
|
521
|
-
);
|
|
522
|
-
return hash;
|
|
523
|
-
};
|
|
524
|
-
const writeProjection = async (tx, write) => {
|
|
525
|
-
const headColumns = Object.entries(write.head).map(([key, held]) => ({
|
|
526
|
-
column: columnOf(key),
|
|
527
|
-
held
|
|
528
|
-
}));
|
|
529
|
-
if (write.create) {
|
|
530
|
-
await seam.send(
|
|
531
|
-
tx,
|
|
532
|
-
chainAll([
|
|
533
|
-
text(
|
|
534
|
-
`insert into ${quote(table)} (${[
|
|
535
|
-
tenantColumn,
|
|
536
|
-
entityColumn,
|
|
537
|
-
"number",
|
|
538
|
-
createdAtColumn,
|
|
539
|
-
"updated_at",
|
|
540
|
-
retiredAtColumn,
|
|
541
|
-
versionColumn,
|
|
542
|
-
"lineage",
|
|
543
|
-
"hash",
|
|
544
|
-
"body",
|
|
545
|
-
...headColumns.map((one) => one.column)
|
|
546
|
-
].map(quote).join(", ")}) values (`
|
|
547
|
-
),
|
|
548
|
-
...valuesList([
|
|
549
|
-
{ column: tenantColumn, held: write.tenantId },
|
|
550
|
-
{ column: entityColumn, held: write.entityName },
|
|
551
|
-
{ column: "number", held: write.ref },
|
|
552
|
-
{ column: createdAtColumn, held: write.at },
|
|
553
|
-
{ column: "updated_at", held: write.at },
|
|
554
|
-
{ column: retiredAtColumn, held: write.retiredAt },
|
|
555
|
-
{ column: versionColumn, held: write.version },
|
|
556
|
-
{ column: "lineage", held: write.lineage },
|
|
557
|
-
{ column: "hash", held: write.hash },
|
|
558
|
-
{ column: "body", held: write.body },
|
|
559
|
-
...headColumns
|
|
560
|
-
]),
|
|
561
|
-
text(")")
|
|
562
|
-
])
|
|
563
|
-
);
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
await seam.send(
|
|
567
|
-
tx,
|
|
568
|
-
chainAll([
|
|
569
|
-
text(`update ${quote(table)} set `),
|
|
570
|
-
...setList([
|
|
571
|
-
{ column: "updated_at", held: write.at },
|
|
572
|
-
{ column: retiredAtColumn, held: write.retiredAt },
|
|
573
|
-
{ column: versionColumn, held: write.version },
|
|
574
|
-
{ column: "lineage", held: write.lineage },
|
|
575
|
-
{ column: "hash", held: write.hash },
|
|
576
|
-
{ column: "body", held: write.body },
|
|
577
|
-
...headColumns
|
|
578
|
-
]),
|
|
579
|
-
text(` where ${quote(tenantColumn)} = `),
|
|
580
|
-
bound(write.tenantId),
|
|
581
|
-
text(` and ${quote(entityColumn)} = `),
|
|
582
|
-
bound(write.entityName),
|
|
583
|
-
text(` and ${quote("number")} = `),
|
|
584
|
-
bound(write.ref)
|
|
585
|
-
])
|
|
586
|
-
);
|
|
587
|
-
};
|
|
588
|
-
const upsertProjection = async (tx, write) => {
|
|
589
|
-
const headColumns = Object.entries(write.head).map(([key, held]) => ({
|
|
590
|
-
column: columnOf(key),
|
|
591
|
-
held
|
|
592
|
-
}));
|
|
593
|
-
const inserted = [
|
|
594
|
-
{ column: tenantColumn, held: write.tenantId },
|
|
595
|
-
{ column: entityColumn, held: write.entityName },
|
|
596
|
-
{ column: "number", held: write.ref },
|
|
597
|
-
{ column: createdAtColumn, held: write.at },
|
|
598
|
-
{ column: "updated_at", held: write.at },
|
|
599
|
-
{ column: retiredAtColumn, held: write.retiredAt },
|
|
600
|
-
{ column: versionColumn, held: write.version },
|
|
601
|
-
{ column: "lineage", held: write.lineage },
|
|
602
|
-
{ column: "hash", held: write.hash },
|
|
603
|
-
{ column: "body", held: write.body },
|
|
604
|
-
...headColumns
|
|
605
|
-
];
|
|
606
|
-
const conflictColumns = /* @__PURE__ */ new Set([tenantColumn, entityColumn, "number"]);
|
|
607
|
-
const conflict = [...conflictColumns].map(quote).join(", ");
|
|
608
|
-
const updated = inserted.filter((one) => !conflictColumns.has(one.column));
|
|
609
|
-
await seam.send(
|
|
610
|
-
tx,
|
|
611
|
-
chainAll([
|
|
612
|
-
text(
|
|
613
|
-
`insert into ${quote(table)} (${inserted.map((one) => quote(one.column)).join(", ")}) values (`
|
|
614
|
-
),
|
|
615
|
-
...valuesList(inserted),
|
|
616
|
-
text(`) on conflict (${conflict}) do update set `),
|
|
617
|
-
...setList(updated)
|
|
618
|
-
])
|
|
619
|
-
);
|
|
620
|
-
};
|
|
621
|
-
const revisionRowAt = async (tx, tenantId, entityName, ref, lineage, version) => {
|
|
622
|
-
const log = needsLog();
|
|
623
|
-
const [row] = (0, import_conformance.rowsOf)(
|
|
624
|
-
await seam.send(
|
|
625
|
-
tx,
|
|
626
|
-
chainAll([
|
|
627
|
-
text(
|
|
628
|
-
`select ${quote("version")} as "version", ${quote(revisionKindColumn)} as "kind", ${quote("body")} as "body", ${quote("actor")} as "actor", ${quote("reason")} as "reason", ${quote("at")} as "at", ${quote("prev_hash")} as "prevHash", ${quote("hash")} as "hash" from ${quote(log)} where ${quote(tenantColumn)} = `
|
|
629
|
-
),
|
|
630
|
-
bound(tenantId),
|
|
631
|
-
text(` and ${quote(entityColumn)} = `),
|
|
632
|
-
bound(entityName),
|
|
633
|
-
text(` and ${quote("ref")} = `),
|
|
634
|
-
bound(ref),
|
|
635
|
-
text(` and ${quote("lineage")} = `),
|
|
636
|
-
bound(lineage),
|
|
637
|
-
text(` and ${quote("version")} = `),
|
|
638
|
-
bound(version)
|
|
639
|
-
])
|
|
640
|
-
)
|
|
641
|
-
);
|
|
642
|
-
return row === void 0 ? void 0 : { ...row, at: Number(row.at), kind: row.kind };
|
|
643
|
-
};
|
|
644
|
-
const revisionRowAsOf = async (tx, tenantId, entityName, ref, lineage, at) => {
|
|
645
|
-
const log = needsLog();
|
|
646
|
-
const [row] = (0, import_conformance.rowsOf)(
|
|
647
|
-
await seam.send(
|
|
648
|
-
tx,
|
|
649
|
-
chainAll([
|
|
650
|
-
text(
|
|
651
|
-
`select ${quote("version")} as "version", ${quote(revisionKindColumn)} as "kind", ${quote("body")} as "body", ${quote("actor")} as "actor", ${quote("reason")} as "reason", ${quote("at")} as "at", ${quote("prev_hash")} as "prevHash", ${quote("hash")} as "hash" from ${quote(log)} where ${quote(tenantColumn)} = `
|
|
652
|
-
),
|
|
653
|
-
bound(tenantId),
|
|
654
|
-
text(` and ${quote(entityColumn)} = `),
|
|
655
|
-
bound(entityName),
|
|
656
|
-
text(` and ${quote("ref")} = `),
|
|
657
|
-
bound(ref),
|
|
658
|
-
text(` and ${quote("lineage")} = `),
|
|
659
|
-
bound(lineage),
|
|
660
|
-
text(` and ${quote("at")} <= `),
|
|
661
|
-
bound(at),
|
|
662
|
-
text(` order by ${quote("version")} desc limit 1`)
|
|
663
|
-
])
|
|
664
|
-
)
|
|
665
|
-
);
|
|
666
|
-
return row === void 0 ? void 0 : { ...row, at: Number(row.at), kind: row.kind };
|
|
667
|
-
};
|
|
668
|
-
const revisionRowsFor = async (tx, tenantId, entityName, ref, lineage) => {
|
|
669
|
-
const log = needsLog();
|
|
670
|
-
const rows = (0, import_conformance.rowsOf)(
|
|
671
|
-
await seam.send(
|
|
672
|
-
tx,
|
|
673
|
-
chainAll([
|
|
674
|
-
text(
|
|
675
|
-
`select ${quote("version")} as "version", ${quote(revisionKindColumn)} as "kind", ${quote("body")} as "body", ${quote("actor")} as "actor", ${quote("reason")} as "reason", ${quote("at")} as "at", ${quote("prev_hash")} as "prevHash", ${quote("hash")} as "hash" from ${quote(log)} where ${quote(tenantColumn)} = `
|
|
676
|
-
),
|
|
677
|
-
bound(tenantId),
|
|
678
|
-
text(` and ${quote(entityColumn)} = `),
|
|
679
|
-
bound(entityName),
|
|
680
|
-
text(` and ${quote("ref")} = `),
|
|
681
|
-
bound(ref),
|
|
682
|
-
text(` and ${quote("lineage")} = `),
|
|
683
|
-
bound(lineage),
|
|
684
|
-
text(` order by ${quote("version")} asc`)
|
|
685
|
-
])
|
|
686
|
-
)
|
|
687
|
-
);
|
|
688
|
-
return rows.map((row) => ({ ...row, at: Number(row.at), kind: row.kind }));
|
|
838
|
+
return revisions;
|
|
689
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
|
+
});
|
|
690
851
|
const entityStoreFor = (name) => {
|
|
691
852
|
const definition = registry.named(name);
|
|
692
853
|
const item = (row) => {
|
|
@@ -710,36 +871,86 @@ var documentStore = (options) => {
|
|
|
710
871
|
createdAt: row.at,
|
|
711
872
|
number: ref,
|
|
712
873
|
updatedAt: row.at,
|
|
713
|
-
version: row.
|
|
874
|
+
version: row.sequence
|
|
714
875
|
});
|
|
715
876
|
};
|
|
716
|
-
const
|
|
717
|
-
|
|
877
|
+
const projection = (0, import_db2.projectionOf)({
|
|
878
|
+
carried: ["lineage"],
|
|
879
|
+
columns: { ...entityColumns, hash: "hash", sequence: versionColumn },
|
|
718
880
|
constants: { [entityColumn]: name },
|
|
719
|
-
filterable:
|
|
720
|
-
indexed: (body) => definition.headOf(body),
|
|
881
|
+
filterable: physicalHeads,
|
|
721
882
|
item,
|
|
722
883
|
key: { column: "number", param: "ref" },
|
|
723
884
|
order: [{ column: "number" }],
|
|
724
|
-
...
|
|
885
|
+
...placed,
|
|
725
886
|
seam,
|
|
726
|
-
sortable:
|
|
887
|
+
sortable: [...physicalHeads, "number", physical.createdAt, physical.updatedAt],
|
|
727
888
|
table
|
|
728
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
|
+
});
|
|
729
941
|
const commitTx = seam.scoped(
|
|
730
942
|
async (tx, params) => {
|
|
731
943
|
const tenantId = params[tenantKey];
|
|
732
944
|
const prepared = definition.prepare(params.body);
|
|
733
945
|
const stamped = stampBody(definition, prepared);
|
|
734
|
-
const
|
|
735
|
-
const existing = await lockedProjection(tx, tenantId, name, params.ref);
|
|
946
|
+
const existing = await locked(tx, params);
|
|
736
947
|
if (existing !== void 0 && existing.retiredAt !== null) {
|
|
737
948
|
throw new import_db2.DbRefusal(
|
|
738
949
|
`"${params.ref}" (${name}) is retired: commit refuses a retired document \u2014 succeed opens a new lineage for it`
|
|
739
950
|
);
|
|
740
951
|
}
|
|
741
|
-
const
|
|
742
|
-
if (existing !== void 0 && !
|
|
952
|
+
const changed = existing === void 0 || JSON.stringify(canonicalOf(existing.body)) !== JSON.stringify(canonicalOf(stamped));
|
|
953
|
+
if (existing !== void 0 && !changed) {
|
|
743
954
|
return {
|
|
744
955
|
committed: false,
|
|
745
956
|
lineage: existing.lineage,
|
|
@@ -748,50 +959,42 @@ var documentStore = (options) => {
|
|
|
748
959
|
};
|
|
749
960
|
}
|
|
750
961
|
const lineage = existing?.lineage ?? 1;
|
|
751
|
-
const
|
|
752
|
-
const
|
|
753
|
-
const head = definition.headOf(prepared);
|
|
754
|
-
const hash = await insertRevisionRow(tx, {
|
|
962
|
+
const reason = params.reason ?? null;
|
|
963
|
+
const appended = await append(tx, {
|
|
755
964
|
actor: params.actor,
|
|
756
|
-
at: now,
|
|
757
965
|
body: stamped,
|
|
758
966
|
compensationOf: null,
|
|
759
|
-
entityName: name,
|
|
760
967
|
kind: "committed",
|
|
761
968
|
lineage,
|
|
762
|
-
|
|
969
|
+
reason,
|
|
763
970
|
ref: params.ref,
|
|
764
|
-
|
|
765
|
-
tenantId,
|
|
766
|
-
version
|
|
971
|
+
tenantId
|
|
767
972
|
});
|
|
768
|
-
await
|
|
769
|
-
at:
|
|
973
|
+
await project(tx, {
|
|
974
|
+
at: appended.at,
|
|
770
975
|
body: stamped,
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
hash,
|
|
774
|
-
head,
|
|
976
|
+
hash: appended.hash,
|
|
977
|
+
head: headColumnsOf(prepared),
|
|
775
978
|
lineage,
|
|
776
979
|
ref: params.ref,
|
|
777
980
|
retiredAt: null,
|
|
778
981
|
tenantId,
|
|
779
|
-
version
|
|
982
|
+
version: appended.version
|
|
780
983
|
});
|
|
781
984
|
return {
|
|
782
985
|
archivable: {
|
|
783
986
|
actor: params.actor,
|
|
784
|
-
at:
|
|
987
|
+
at: appended.at,
|
|
785
988
|
body: stamped,
|
|
786
|
-
prevHash,
|
|
787
|
-
reason
|
|
989
|
+
prevHash: appended.prevHash,
|
|
990
|
+
reason
|
|
788
991
|
},
|
|
789
992
|
committed: true,
|
|
790
|
-
hash,
|
|
993
|
+
hash: appended.hash,
|
|
791
994
|
lineage,
|
|
792
995
|
previous: existing === void 0 ? null : { hash: existing.hash, version: existing.version },
|
|
793
996
|
ref: params.ref,
|
|
794
|
-
version
|
|
997
|
+
version: appended.version
|
|
795
998
|
};
|
|
796
999
|
}
|
|
797
1000
|
);
|
|
@@ -826,53 +1029,46 @@ var documentStore = (options) => {
|
|
|
826
1029
|
const retire = seam.scoped(
|
|
827
1030
|
async (tx, params) => {
|
|
828
1031
|
const tenantId = params[tenantKey];
|
|
829
|
-
const
|
|
830
|
-
const existing = await lockedProjection(tx, tenantId, name, params.ref);
|
|
1032
|
+
const existing = await locked(tx, params);
|
|
831
1033
|
if (existing === void 0) {
|
|
832
1034
|
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) has no document to retire`);
|
|
833
1035
|
}
|
|
834
1036
|
if (existing.retiredAt !== null) {
|
|
835
1037
|
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) is already retired`);
|
|
836
1038
|
}
|
|
837
|
-
const
|
|
838
|
-
const head = definition.headOf(
|
|
839
|
-
readBody(definition, name, params.ref, existing.body)
|
|
840
|
-
);
|
|
841
|
-
const hash = await insertRevisionRow(tx, {
|
|
1039
|
+
const appended = await append(tx, {
|
|
842
1040
|
actor: params.actor,
|
|
843
|
-
at: now,
|
|
844
1041
|
body: existing.body,
|
|
845
1042
|
compensationOf: null,
|
|
846
|
-
entityName: name,
|
|
847
1043
|
kind: "retired",
|
|
848
1044
|
lineage: existing.lineage,
|
|
849
|
-
prevHash: existing.hash,
|
|
850
|
-
ref: params.ref,
|
|
851
1045
|
reason: params.reason ?? null,
|
|
852
|
-
|
|
853
|
-
|
|
1046
|
+
ref: params.ref,
|
|
1047
|
+
tenantId
|
|
854
1048
|
});
|
|
855
|
-
await
|
|
856
|
-
at:
|
|
1049
|
+
await project(tx, {
|
|
1050
|
+
at: appended.at,
|
|
857
1051
|
body: existing.body,
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
hash,
|
|
861
|
-
head,
|
|
1052
|
+
hash: appended.hash,
|
|
1053
|
+
head: storedHead(params.ref, existing.body),
|
|
862
1054
|
lineage: existing.lineage,
|
|
863
1055
|
ref: params.ref,
|
|
864
|
-
retiredAt:
|
|
1056
|
+
retiredAt: appended.at,
|
|
865
1057
|
tenantId,
|
|
866
|
-
version
|
|
1058
|
+
version: appended.version
|
|
867
1059
|
});
|
|
868
|
-
return {
|
|
1060
|
+
return {
|
|
1061
|
+
hash: appended.hash,
|
|
1062
|
+
lineage: existing.lineage,
|
|
1063
|
+
ref: params.ref,
|
|
1064
|
+
version: appended.version
|
|
1065
|
+
};
|
|
869
1066
|
}
|
|
870
1067
|
);
|
|
871
1068
|
const succeed = seam.scoped(
|
|
872
1069
|
async (tx, params) => {
|
|
873
1070
|
const tenantId = params[tenantKey];
|
|
874
|
-
const
|
|
875
|
-
const existing = await lockedProjection(tx, tenantId, name, params.ref);
|
|
1071
|
+
const existing = await locked(tx, params);
|
|
876
1072
|
if (existing === void 0 || existing.retiredAt === null) {
|
|
877
1073
|
throw new import_db2.DbRefusal(
|
|
878
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`
|
|
@@ -880,413 +1076,274 @@ var documentStore = (options) => {
|
|
|
880
1076
|
}
|
|
881
1077
|
const prepared = definition.prepare(params.body);
|
|
882
1078
|
const stamped = stampBody(definition, prepared);
|
|
883
|
-
const head = definition.headOf(prepared);
|
|
884
1079
|
const lineage = existing.lineage + 1;
|
|
885
|
-
const
|
|
1080
|
+
const appended = await append(tx, {
|
|
886
1081
|
actor: params.actor,
|
|
887
|
-
at: now,
|
|
888
1082
|
body: stamped,
|
|
889
1083
|
compensationOf: null,
|
|
890
|
-
entityName: name,
|
|
891
1084
|
kind: "committed",
|
|
892
1085
|
lineage,
|
|
893
|
-
prevHash: null,
|
|
894
|
-
ref: params.ref,
|
|
895
1086
|
reason: params.reason ?? null,
|
|
896
|
-
|
|
897
|
-
|
|
1087
|
+
ref: params.ref,
|
|
1088
|
+
tenantId
|
|
898
1089
|
});
|
|
899
|
-
await
|
|
900
|
-
at:
|
|
1090
|
+
await project(tx, {
|
|
1091
|
+
at: appended.at,
|
|
901
1092
|
body: stamped,
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
hash,
|
|
905
|
-
head,
|
|
1093
|
+
hash: appended.hash,
|
|
1094
|
+
head: headColumnsOf(prepared),
|
|
906
1095
|
lineage,
|
|
907
1096
|
ref: params.ref,
|
|
908
1097
|
retiredAt: null,
|
|
909
1098
|
tenantId,
|
|
910
|
-
version:
|
|
1099
|
+
version: appended.version
|
|
911
1100
|
});
|
|
912
|
-
return {
|
|
1101
|
+
return {
|
|
1102
|
+
committed: true,
|
|
1103
|
+
hash: appended.hash,
|
|
1104
|
+
lineage,
|
|
1105
|
+
previous: null,
|
|
1106
|
+
ref: params.ref,
|
|
1107
|
+
version: appended.version
|
|
1108
|
+
};
|
|
913
1109
|
}
|
|
914
1110
|
);
|
|
915
1111
|
const reinstate = seam.scoped(
|
|
916
1112
|
async (tx, params) => {
|
|
917
1113
|
const tenantId = params[tenantKey];
|
|
918
|
-
const
|
|
919
|
-
const existing = await lockedProjection(tx, tenantId, name, params.ref);
|
|
1114
|
+
const existing = await locked(tx, params);
|
|
920
1115
|
if (existing === void 0 || existing.retiredAt === null) {
|
|
921
1116
|
throw new import_db2.DbRefusal(`"${params.ref}" (${name}) is not retired: nothing to reinstate`);
|
|
922
1117
|
}
|
|
923
|
-
const
|
|
924
|
-
const head = definition.headOf(
|
|
925
|
-
readBody(definition, name, params.ref, existing.body)
|
|
926
|
-
);
|
|
927
|
-
const hash = await insertRevisionRow(tx, {
|
|
1118
|
+
const appended = await append(tx, {
|
|
928
1119
|
actor: params.actor,
|
|
929
|
-
at: now,
|
|
930
1120
|
body: existing.body,
|
|
931
1121
|
compensationOf: null,
|
|
932
|
-
entityName: name,
|
|
933
1122
|
kind: "reinstated",
|
|
934
1123
|
lineage: existing.lineage,
|
|
935
|
-
prevHash: existing.hash,
|
|
936
|
-
ref: params.ref,
|
|
937
1124
|
reason: params.reason ?? null,
|
|
938
|
-
|
|
939
|
-
|
|
1125
|
+
ref: params.ref,
|
|
1126
|
+
tenantId
|
|
940
1127
|
});
|
|
941
|
-
await
|
|
942
|
-
at:
|
|
1128
|
+
await project(tx, {
|
|
1129
|
+
at: appended.at,
|
|
943
1130
|
body: existing.body,
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
hash,
|
|
947
|
-
head,
|
|
1131
|
+
hash: appended.hash,
|
|
1132
|
+
head: storedHead(params.ref, existing.body),
|
|
948
1133
|
lineage: existing.lineage,
|
|
949
1134
|
ref: params.ref,
|
|
950
1135
|
retiredAt: null,
|
|
951
1136
|
tenantId,
|
|
952
|
-
version
|
|
1137
|
+
version: appended.version
|
|
953
1138
|
});
|
|
954
|
-
return {
|
|
1139
|
+
return {
|
|
1140
|
+
hash: appended.hash,
|
|
1141
|
+
lineage: existing.lineage,
|
|
1142
|
+
ref: params.ref,
|
|
1143
|
+
version: appended.version
|
|
1144
|
+
};
|
|
955
1145
|
}
|
|
956
1146
|
);
|
|
957
1147
|
const at = seam.scoped(
|
|
958
1148
|
async (tx, params) => {
|
|
959
|
-
const
|
|
960
|
-
const
|
|
961
|
-
|
|
962
|
-
|
|
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);
|
|
963
1155
|
}
|
|
964
1156
|
);
|
|
965
1157
|
const asOf = seam.scoped(
|
|
966
1158
|
async (tx, params) => {
|
|
967
|
-
const
|
|
968
|
-
const
|
|
969
|
-
|
|
970
|
-
return row === void 0 ? null : instanceFromRevision(params.ref, row);
|
|
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);
|
|
971
1162
|
}
|
|
972
1163
|
);
|
|
973
1164
|
const history = seam.scoped(
|
|
974
1165
|
async (tx, params) => {
|
|
975
|
-
const
|
|
976
|
-
const
|
|
977
|
-
const rows = await revisionRowsFor(tx, tenantId, name, params.ref, lineage);
|
|
1166
|
+
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
1167
|
+
const rows = await needsLog().history(tx, stream(params, lineage));
|
|
978
1168
|
return rows.map((row) => ({
|
|
979
1169
|
actor: row.actor,
|
|
980
1170
|
at: row.at,
|
|
981
|
-
body: readBody(definition, name, `${params.ref} version ${row.
|
|
1171
|
+
body: readBody(definition, name, `${params.ref} version ${row.sequence}`, row.body),
|
|
982
1172
|
hash: row.hash,
|
|
983
1173
|
kind: row.kind,
|
|
984
1174
|
reason: row.reason,
|
|
985
|
-
version: row.
|
|
1175
|
+
version: row.sequence
|
|
986
1176
|
}));
|
|
987
1177
|
}
|
|
988
1178
|
);
|
|
989
1179
|
const verify = seam.scoped(
|
|
990
1180
|
async (tx, params) => {
|
|
991
|
-
const
|
|
992
|
-
|
|
993
|
-
const rows = await revisionRowsFor(tx, tenantId, name, params.ref, lineage);
|
|
994
|
-
let previousHash = null;
|
|
995
|
-
for (const row of rows) {
|
|
996
|
-
const expected = hashOf({
|
|
997
|
-
actor: row.actor,
|
|
998
|
-
at: row.at,
|
|
999
|
-
body: row.body,
|
|
1000
|
-
entity: name,
|
|
1001
|
-
kind: row.kind,
|
|
1002
|
-
lineage,
|
|
1003
|
-
prevHash: previousHash,
|
|
1004
|
-
reason: row.reason,
|
|
1005
|
-
ref: params.ref,
|
|
1006
|
-
tenant: tenantId,
|
|
1007
|
-
version: row.version
|
|
1008
|
-
});
|
|
1009
|
-
if (expected !== row.hash) {
|
|
1010
|
-
return { brokenAt: row.version, expected, found: row.hash, ok: false };
|
|
1011
|
-
}
|
|
1012
|
-
previousHash = row.hash;
|
|
1013
|
-
}
|
|
1014
|
-
return { ok: true, versions: rows.length };
|
|
1181
|
+
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
1182
|
+
return needsLog().verify(tx, stream(params, lineage));
|
|
1015
1183
|
}
|
|
1016
1184
|
);
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
const now = await currentDbTime(scoped);
|
|
1020
|
-
const existing = await lockedProjection(scoped, tenantId, name, output.ref);
|
|
1021
|
-
if (existing === void 0) {
|
|
1022
|
-
throw new import_db2.DbRefusal(
|
|
1023
|
-
`"${output.ref}" (${name}) has nothing to undo \u2014 the row v${output.version} wrote is gone`
|
|
1024
|
-
);
|
|
1025
|
-
}
|
|
1026
|
-
const version = existing.version + 1;
|
|
1027
|
-
const reason = `undo of v${output.version}`;
|
|
1028
|
-
if (output.previous === null) {
|
|
1029
|
-
const head2 = definition.headOf(
|
|
1030
|
-
readBody(definition, name, output.ref, existing.body)
|
|
1031
|
-
);
|
|
1032
|
-
const hash2 = await insertRevisionRow(scoped, {
|
|
1033
|
-
actor: UNDO_ACTOR,
|
|
1034
|
-
at: now,
|
|
1035
|
-
body: existing.body,
|
|
1036
|
-
compensationOf: output.version,
|
|
1037
|
-
entityName: name,
|
|
1038
|
-
kind: "retired",
|
|
1039
|
-
lineage: output.lineage,
|
|
1040
|
-
prevHash: existing.hash,
|
|
1041
|
-
ref: output.ref,
|
|
1042
|
-
reason,
|
|
1043
|
-
tenantId,
|
|
1044
|
-
version
|
|
1045
|
-
});
|
|
1046
|
-
await writeProjection(scoped, {
|
|
1047
|
-
at: now,
|
|
1048
|
-
body: existing.body,
|
|
1049
|
-
create: false,
|
|
1050
|
-
entityName: name,
|
|
1051
|
-
hash: hash2,
|
|
1052
|
-
head: head2,
|
|
1053
|
-
lineage: output.lineage,
|
|
1054
|
-
ref: output.ref,
|
|
1055
|
-
retiredAt: now,
|
|
1056
|
-
tenantId,
|
|
1057
|
-
version
|
|
1058
|
-
});
|
|
1059
|
-
return;
|
|
1060
|
-
}
|
|
1061
|
-
const prior = await revisionRowAt(
|
|
1062
|
-
scoped,
|
|
1063
|
-
tenantId,
|
|
1064
|
-
name,
|
|
1065
|
-
output.ref,
|
|
1066
|
-
output.lineage,
|
|
1067
|
-
output.previous.version
|
|
1068
|
-
);
|
|
1069
|
-
if (prior === void 0) {
|
|
1070
|
-
throw new import_db2.DbRefusal(
|
|
1071
|
-
`"${output.ref}" (${name}) version ${output.previous.version} is gone \u2014 cannot undo v${output.version} without it`
|
|
1072
|
-
);
|
|
1073
|
-
}
|
|
1074
|
-
const head = definition.headOf(readBody(definition, name, output.ref, prior.body));
|
|
1075
|
-
const hash = await insertRevisionRow(scoped, {
|
|
1185
|
+
const compensate = async (scoped, tenantId, output, kind, body, retired) => {
|
|
1186
|
+
const appended = await append(scoped, {
|
|
1076
1187
|
actor: UNDO_ACTOR,
|
|
1077
|
-
|
|
1078
|
-
body: prior.body,
|
|
1188
|
+
body,
|
|
1079
1189
|
compensationOf: output.version,
|
|
1080
|
-
|
|
1081
|
-
kind: "committed",
|
|
1190
|
+
kind,
|
|
1082
1191
|
lineage: output.lineage,
|
|
1083
|
-
|
|
1192
|
+
reason: `undo of v${output.version}`,
|
|
1084
1193
|
ref: output.ref,
|
|
1085
|
-
|
|
1086
|
-
tenantId,
|
|
1087
|
-
version
|
|
1194
|
+
tenantId
|
|
1088
1195
|
});
|
|
1089
|
-
await
|
|
1090
|
-
at:
|
|
1091
|
-
body
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
hash,
|
|
1095
|
-
head,
|
|
1196
|
+
await project(scoped, {
|
|
1197
|
+
at: appended.at,
|
|
1198
|
+
body,
|
|
1199
|
+
hash: appended.hash,
|
|
1200
|
+
head: storedHead(output.ref, body),
|
|
1096
1201
|
lineage: output.lineage,
|
|
1097
1202
|
ref: output.ref,
|
|
1098
|
-
retiredAt: null,
|
|
1203
|
+
retiredAt: retired ? appended.at : null,
|
|
1099
1204
|
tenantId,
|
|
1100
|
-
version
|
|
1205
|
+
version: appended.version
|
|
1101
1206
|
});
|
|
1102
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
|
+
};
|
|
1103
1217
|
const undos = {
|
|
1104
|
-
commit: (tx, { output, tenantId }) => seam.inTenant(tx, tenantId, (scoped) =>
|
|
1105
|
-
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
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) {
|
|
1109
1230
|
throw new import_db2.DbRefusal(
|
|
1110
|
-
`"${output.ref}" (${name})
|
|
1231
|
+
`"${output.ref}" (${name}) version ${output.previous.version} is gone \u2014 cannot undo v${output.version} without it`
|
|
1111
1232
|
);
|
|
1112
1233
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
);
|
|
1117
|
-
|
|
1118
|
-
actor: UNDO_ACTOR,
|
|
1119
|
-
at: now,
|
|
1120
|
-
body: existing.body,
|
|
1121
|
-
compensationOf: output.version,
|
|
1122
|
-
entityName: name,
|
|
1123
|
-
kind: "reinstated",
|
|
1124
|
-
lineage: output.lineage,
|
|
1125
|
-
prevHash: existing.hash,
|
|
1126
|
-
ref: output.ref,
|
|
1127
|
-
reason: `undo of v${output.version}`,
|
|
1128
|
-
tenantId,
|
|
1129
|
-
version
|
|
1130
|
-
});
|
|
1131
|
-
await writeProjection(scoped, {
|
|
1132
|
-
at: now,
|
|
1133
|
-
body: existing.body,
|
|
1134
|
-
create: false,
|
|
1135
|
-
entityName: name,
|
|
1136
|
-
hash,
|
|
1137
|
-
head,
|
|
1138
|
-
lineage: output.lineage,
|
|
1139
|
-
ref: output.ref,
|
|
1140
|
-
retiredAt: null,
|
|
1141
|
-
tenantId,
|
|
1142
|
-
version
|
|
1143
|
-
});
|
|
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);
|
|
1144
1239
|
})
|
|
1145
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
|
+
};
|
|
1146
1255
|
return {
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
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
|
|
1159
1271
|
};
|
|
1160
1272
|
};
|
|
1161
|
-
const
|
|
1162
|
-
|
|
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])
|
|
1163
1277
|
);
|
|
1164
|
-
const heads = seam.scoped(
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
text(` and ${quote(createdAtColumn)} between `),
|
|
1184
|
-
bound(where.createdAt[0]),
|
|
1185
|
-
text(" and "),
|
|
1186
|
-
bound(where.createdAt[1])
|
|
1187
|
-
);
|
|
1188
|
-
}
|
|
1189
|
-
predicates.push(
|
|
1190
|
-
text(
|
|
1191
|
-
` order by ${order.map((one) => `${quote(columnOf(one.column))} ${one.direction === "desc" ? "desc" : "asc"}`).join(", ")}`
|
|
1192
|
-
)
|
|
1193
|
-
);
|
|
1194
|
-
return (0, import_conformance.rowsOf)(
|
|
1195
|
-
await seam.send(tx, chainAll(predicates))
|
|
1196
|
-
);
|
|
1197
|
-
});
|
|
1198
|
-
const aggregate = seam.scoped(async (tx, params) => {
|
|
1199
|
-
const { by, entities, entity, measures, where } = params;
|
|
1200
|
-
const tenantId = params[tenantKey];
|
|
1201
|
-
const wantsCount = measures.includes("count");
|
|
1202
|
-
const wantsSum = measures.includes("sum:total");
|
|
1203
|
-
const groupSelect = by === void 0 ? [] : [`${byExpr(by, columnOf)} as "by"`];
|
|
1204
|
-
const measureSelect = [
|
|
1205
|
-
...wantsCount ? ['count(*)::int as "count"'] : [],
|
|
1206
|
-
...wantsSum ? [`sum(${quote(columnOf("total"))})::text as "sum"`] : []
|
|
1207
|
-
];
|
|
1208
|
-
const predicates = [
|
|
1209
|
-
text(
|
|
1210
|
-
`select ${[...groupSelect, ...measureSelect].join(", ")} from ${quote(table)} where ${quote(tenantColumn)} = `
|
|
1211
|
-
),
|
|
1212
|
-
bound(tenantId),
|
|
1213
|
-
text(` and ${quote(retiredAtColumn)} is null`)
|
|
1214
|
-
];
|
|
1215
|
-
if (entity !== void 0)
|
|
1216
|
-
predicates.push(text(` and ${quote(entityColumn)} = `), bound(entity));
|
|
1217
|
-
if (entities !== void 0) {
|
|
1218
|
-
predicates.push(text(` and ${quote(entityColumn)} = any(`), bound(entities), text(")"));
|
|
1219
|
-
}
|
|
1220
|
-
if (where?.status !== void 0) {
|
|
1221
|
-
predicates.push(text(` and ${quote(columnOf("status"))} = `), bound(where.status));
|
|
1222
|
-
}
|
|
1223
|
-
if (where !== void 0 && "issueDate" in where) {
|
|
1224
|
-
predicates.push(
|
|
1225
|
-
text(` and ${quote(columnOf("issueDate"))} between `),
|
|
1226
|
-
bound(where.issueDate[0]),
|
|
1227
|
-
text(" and "),
|
|
1228
|
-
bound(where.issueDate[1])
|
|
1229
|
-
);
|
|
1230
|
-
}
|
|
1231
|
-
if (where !== void 0 && "createdAt" in where) {
|
|
1232
|
-
predicates.push(
|
|
1233
|
-
text(` and ${quote(createdAtColumn)} between `),
|
|
1234
|
-
bound(where.createdAt[0]),
|
|
1235
|
-
text(" and "),
|
|
1236
|
-
bound(where.createdAt[1])
|
|
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
|
+
)
|
|
1237
1297
|
);
|
|
1238
1298
|
}
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
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
|
+
});
|
|
1245
1317
|
}
|
|
1246
|
-
|
|
1247
|
-
...by === void 0 ? {} : { by: row.by },
|
|
1248
|
-
...wantsCount ? { count: row.count ?? 0 } : {},
|
|
1249
|
-
...wantsSum ? { sum: row.sum ?? "0" } : {}
|
|
1250
|
-
}));
|
|
1251
|
-
});
|
|
1318
|
+
);
|
|
1252
1319
|
const rebuild = seam.scoped(
|
|
1253
1320
|
async (tx, params) => {
|
|
1254
1321
|
const log = needsLog();
|
|
1255
|
-
const
|
|
1256
|
-
const
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
)
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
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
|
+
}
|
|
1264
1331
|
}
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
)
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
const definition = registry.named(row.entity);
|
|
1273
|
-
if (definition === void 0) continue;
|
|
1274
|
-
const clean = readBody(definition, row.entity, row.ref, row.body);
|
|
1275
|
-
const head = definition.headOf(clean);
|
|
1276
|
-
await upsertProjection(tx, {
|
|
1277
|
-
at: Number(row.createdAt),
|
|
1278
|
-
body: row.body,
|
|
1279
|
-
entityName: row.entity,
|
|
1280
|
-
hash: row.hash,
|
|
1281
|
-
head,
|
|
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,
|
|
1282
1339
|
lineage: row.lineage,
|
|
1283
1340
|
ref: row.ref,
|
|
1284
|
-
|
|
1285
|
-
tenantId,
|
|
1286
|
-
version: row.version
|
|
1341
|
+
sequence: 1
|
|
1287
1342
|
});
|
|
1343
|
+
await restore(tx, row, first?.at ?? row.at);
|
|
1344
|
+
rows += 1;
|
|
1288
1345
|
}
|
|
1289
|
-
return { rows
|
|
1346
|
+
return { rows };
|
|
1290
1347
|
}
|
|
1291
1348
|
);
|
|
1292
1349
|
return { ...rawEntities, aggregate, heads, rebuild };
|
|
@@ -1299,6 +1356,7 @@ var documentStore = (options) => {
|
|
|
1299
1356
|
DEFAULT_REVISION_KIND_COLUMN,
|
|
1300
1357
|
DOCUMENT_MIGRATION_PLACEHOLDERS,
|
|
1301
1358
|
Document,
|
|
1359
|
+
HEAD_FIELDS,
|
|
1302
1360
|
appendOnlyTables,
|
|
1303
1361
|
canTransition,
|
|
1304
1362
|
canonicalOf,
|
|
@@ -1308,11 +1366,13 @@ var documentStore = (options) => {
|
|
|
1308
1366
|
documentMigrations,
|
|
1309
1367
|
documentRoleStatements,
|
|
1310
1368
|
documentStore,
|
|
1369
|
+
documentSurface,
|
|
1311
1370
|
fieldPatterns,
|
|
1312
1371
|
hashOf,
|
|
1313
1372
|
isSystemActor,
|
|
1314
1373
|
isTerminalStatus,
|
|
1315
1374
|
migrateBody,
|
|
1375
|
+
physicalColumnsOf,
|
|
1316
1376
|
stampBody,
|
|
1317
1377
|
systemActor,
|
|
1318
1378
|
validatorOf
|