@geonosis/document 0.9.0 → 0.9.2
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 +16 -0
- package/dist/core/actor.cjs +48 -0
- package/dist/core/actor.d.cts +3 -0
- package/dist/core/actor.d.ts +3 -0
- package/dist/core/actor.js +8 -0
- package/dist/core/body-version.cjs +75 -0
- package/dist/core/body-version.d.cts +25 -0
- package/dist/core/body-version.d.ts +25 -0
- package/dist/core/body-version.js +35 -0
- package/dist/core/canonical.cjs +55 -0
- package/dist/core/canonical.d.cts +5 -0
- package/dist/core/canonical.d.ts +5 -0
- package/dist/core/canonical.js +15 -0
- package/dist/core/document.cjs +136 -0
- package/dist/core/document.d.cts +137 -0
- package/dist/core/document.d.ts +137 -0
- package/dist/core/document.js +96 -0
- package/dist/core/entities.cjs +68 -0
- package/dist/core/entities.d.cts +42 -0
- package/dist/core/entities.d.ts +42 -0
- package/dist/core/entities.js +28 -0
- package/dist/core/naming.cjs +56 -0
- package/dist/core/naming.d.cts +17 -0
- package/dist/core/naming.d.ts +17 -0
- package/dist/core/naming.js +16 -0
- package/dist/core/numbering.cjs +41 -0
- package/dist/core/numbering.d.cts +12 -0
- package/dist/core/numbering.d.ts +12 -0
- package/dist/core/numbering.js +0 -0
- package/dist/core/ports.cjs +41 -0
- package/dist/core/ports.d.cts +29 -0
- package/dist/core/ports.d.ts +29 -0
- package/dist/core/ports.js +0 -0
- package/dist/core/schema.cjs +173 -0
- package/dist/core/schema.d.cts +33 -0
- package/dist/core/schema.d.ts +33 -0
- package/dist/{chunk-NA3DLROL.js → core/schema.js} +34 -72
- package/dist/core/status.cjs +47 -0
- package/dist/core/status.d.cts +8 -0
- package/dist/core/status.d.ts +8 -0
- package/dist/core/status.js +7 -0
- package/dist/index.cjs +70 -398
- package/dist/index.d.cts +11 -32
- package/dist/index.d.ts +11 -32
- package/dist/index.js +13 -25
- package/dist/store/ddl.cjs +209 -0
- package/dist/store/ddl.d.cts +56 -0
- package/dist/store/ddl.d.ts +56 -0
- package/dist/store/ddl.js +175 -0
- package/dist/store/hash.cjs +50 -0
- package/dist/store/hash.d.cts +2 -0
- package/dist/store/hash.d.ts +2 -0
- package/dist/store/hash.js +10 -0
- package/dist/store/store.cjs +606 -0
- package/dist/store/store.d.cts +169 -0
- package/dist/store/store.d.ts +169 -0
- package/dist/store/store.js +578 -0
- package/dist/store.cjs +51 -1358
- package/dist/store.d.cts +4 -277
- package/dist/store.d.ts +4 -277
- package/dist/store.js +17 -1114
- package/dist/surface/table.cjs +326 -0
- package/dist/surface/table.d.cts +70 -0
- package/dist/surface/table.d.ts +70 -0
- package/dist/surface/table.js +296 -0
- package/dist/workflows/document-workflows.cjs +314 -0
- package/dist/workflows/document-workflows.d.cts +112 -0
- package/dist/workflows/document-workflows.d.ts +112 -0
- package/dist/workflows/document-workflows.js +281 -0
- package/dist/workflows/effects.cjs +41 -0
- package/dist/workflows/effects.d.cts +68 -0
- package/dist/workflows/effects.d.ts +68 -0
- package/dist/workflows/effects.js +0 -0
- package/dist/workflows/ports.cjs +50 -0
- package/dist/workflows/ports.d.cts +72 -0
- package/dist/workflows/ports.d.ts +72 -0
- package/dist/workflows/ports.js +10 -0
- package/dist/workflows/refusals.cjs +80 -0
- package/dist/workflows/refusals.d.cts +59 -0
- package/dist/workflows/refusals.d.ts +59 -0
- package/dist/workflows/refusals.js +40 -0
- package/dist/workflows.cjs +52 -504
- package/dist/workflows.d.cts +4 -310
- package/dist/workflows.d.ts +4 -310
- package/dist/workflows.js +21 -368
- package/package.json +8 -8
- package/dist/chunk-A6ZU4QU3.js +0 -171
- package/dist/entities-DTNJMeVf.d.cts +0 -205
- package/dist/entities-DTNJMeVf.d.ts +0 -205
- package/dist/ports-C_OfflMQ.d.cts +0 -66
- package/dist/ports-C_OfflMQ.d.ts +0 -66
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
// src/store/store.ts
|
|
2
|
+
import {
|
|
3
|
+
DbRefusal,
|
|
4
|
+
defineEntity,
|
|
5
|
+
defineLog,
|
|
6
|
+
projectionOf
|
|
7
|
+
} from "@geonosis/db";
|
|
8
|
+
import { systemActor } from "../core/actor.js";
|
|
9
|
+
import { migrateBody, stampBody } from "../core/body-version.js";
|
|
10
|
+
import { canonicalOf } from "../core/canonical.js";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_DOCUMENT_TABLE,
|
|
13
|
+
DEFAULT_REVISION_KIND_COLUMN,
|
|
14
|
+
DEFAULT_REVISIONS_TABLE,
|
|
15
|
+
documentTable,
|
|
16
|
+
HEAD_FIELDS,
|
|
17
|
+
physicalColumnsOf
|
|
18
|
+
} from "./ddl.js";
|
|
19
|
+
import { hashOf } from "./hash.js";
|
|
20
|
+
var DEFAULT_VERSION_COLUMN = "version";
|
|
21
|
+
var UNDO_ACTOR = systemActor("document.undo");
|
|
22
|
+
var nameOf = (value, fallback) => value === undefined ? fallback : typeof value === "string" ? value : value.name;
|
|
23
|
+
var readBody = (entity, entityName, ref, stored) => {
|
|
24
|
+
const migrated = migrateBody(entity, stored);
|
|
25
|
+
const result = entity.body["~standard"].validate(migrated);
|
|
26
|
+
if (result instanceof Promise) {
|
|
27
|
+
throw new Error(`${entityName} ${ref}: this entity's body schema validates asynchronously, and a stored body can only be read synchronously here`);
|
|
28
|
+
}
|
|
29
|
+
if (result.issues !== undefined) {
|
|
30
|
+
throw new Error(`${entityName} ${ref}'s stored body no longer satisfies its own schema after migration: ${result.issues.map((issue) => issue.message).join("; ")}`);
|
|
31
|
+
}
|
|
32
|
+
return result.value;
|
|
33
|
+
};
|
|
34
|
+
var headFrom = (found) => found === undefined ? undefined : {
|
|
35
|
+
body: found.body,
|
|
36
|
+
hash: found.hash,
|
|
37
|
+
lineage: Number(found.carried.lineage ?? 1),
|
|
38
|
+
retiredAt: found.retiredAt,
|
|
39
|
+
version: found.sequence
|
|
40
|
+
};
|
|
41
|
+
var documentStore = (options) => {
|
|
42
|
+
const table = options.table ?? DEFAULT_DOCUMENT_TABLE;
|
|
43
|
+
const revisionKindColumn = options.columns?.revisionKind ?? DEFAULT_REVISION_KIND_COLUMN;
|
|
44
|
+
const physical = physicalColumnsOf(options.columns);
|
|
45
|
+
const {
|
|
46
|
+
entity: _entity,
|
|
47
|
+
heads: _heads,
|
|
48
|
+
revisionKind: _revisionKind,
|
|
49
|
+
total: _total,
|
|
50
|
+
version,
|
|
51
|
+
...entityColumns
|
|
52
|
+
} = options.columns ?? {};
|
|
53
|
+
const versionColumn = nameOf(version, DEFAULT_VERSION_COLUMN);
|
|
54
|
+
const entityColumn = physical.entity;
|
|
55
|
+
const { archive, registry, schema, seam } = options;
|
|
56
|
+
const tenantKey = seam.tenantKey;
|
|
57
|
+
const placed = schema === undefined ? {} : { schema };
|
|
58
|
+
const declared = documentTable({
|
|
59
|
+
...options.columns === undefined ? {} : { columns: options.columns },
|
|
60
|
+
...placed,
|
|
61
|
+
table
|
|
62
|
+
});
|
|
63
|
+
const revisionsTable = options.revisions === false ? undefined : options.revisions?.table ?? DEFAULT_REVISIONS_TABLE;
|
|
64
|
+
const tenanted = (tenantId) => ({ [tenantKey]: tenantId });
|
|
65
|
+
const columnOf = (column) => column === "number" ? "number" : physical[column];
|
|
66
|
+
const physicalHeads = HEAD_FIELDS.map(columnOf);
|
|
67
|
+
const narrowed = (where) => Object.fromEntries(Object.entries(where ?? {}).map(([column, held]) => [
|
|
68
|
+
columnOf(column),
|
|
69
|
+
held
|
|
70
|
+
]));
|
|
71
|
+
const revisions = revisionsTable === undefined ? undefined : defineLog({
|
|
72
|
+
columns: { tenant: physical.tenant },
|
|
73
|
+
extra: [{ column: revisionKindColumn, param: "kind" }],
|
|
74
|
+
hash: (row) => hashOf({
|
|
75
|
+
actor: row.actor,
|
|
76
|
+
at: row.at,
|
|
77
|
+
body: row.body,
|
|
78
|
+
entity: row.entity,
|
|
79
|
+
kind: row.kind,
|
|
80
|
+
lineage: row.lineage,
|
|
81
|
+
prevHash: row.prevHash,
|
|
82
|
+
reason: row.reason,
|
|
83
|
+
ref: row.ref,
|
|
84
|
+
tenant: row.tenant,
|
|
85
|
+
version: row.sequence
|
|
86
|
+
}),
|
|
87
|
+
key: [
|
|
88
|
+
{ column: entityColumn, param: "entity" },
|
|
89
|
+
{ column: "ref", param: "ref" },
|
|
90
|
+
{ column: "lineage", param: "lineage" }
|
|
91
|
+
],
|
|
92
|
+
...placed,
|
|
93
|
+
seam,
|
|
94
|
+
table: revisionsTable
|
|
95
|
+
});
|
|
96
|
+
const needsLog = () => {
|
|
97
|
+
if (revisions === undefined) {
|
|
98
|
+
throw new DbRefusal("this store has no log — `documentStore({ revisions: false })` opted out of the append-only record entirely, and the write model has no other door");
|
|
99
|
+
}
|
|
100
|
+
return revisions;
|
|
101
|
+
};
|
|
102
|
+
const readers = defineEntity({
|
|
103
|
+
columns: { ...entityColumns, version: versionColumn },
|
|
104
|
+
filterable: [...physicalHeads, entityColumn, physical.createdAt, physical.updatedAt],
|
|
105
|
+
item: (row) => row,
|
|
106
|
+
key: { column: "number", param: "ref" },
|
|
107
|
+
order: [{ column: "number" }],
|
|
108
|
+
seam,
|
|
109
|
+
sortable: [...physicalHeads, entityColumn, "number", physical.createdAt, physical.updatedAt],
|
|
110
|
+
table: declared
|
|
111
|
+
});
|
|
112
|
+
const entityStoreFor = (name) => {
|
|
113
|
+
const definition = registry.named(name);
|
|
114
|
+
const item = (row) => {
|
|
115
|
+
const body = readBody(definition, name, row.key, row.body);
|
|
116
|
+
const head = definition.headOf(body);
|
|
117
|
+
return registry.instance(name, {
|
|
118
|
+
...head,
|
|
119
|
+
body,
|
|
120
|
+
createdAt: row.createdAt,
|
|
121
|
+
number: row.key,
|
|
122
|
+
updatedAt: row.updatedAt,
|
|
123
|
+
version: row.version ?? 1
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
const instanceFromRevision = (ref, row) => {
|
|
127
|
+
const body = readBody(definition, name, ref, row.body);
|
|
128
|
+
const head = definition.headOf(body);
|
|
129
|
+
return registry.instance(name, {
|
|
130
|
+
...head,
|
|
131
|
+
body,
|
|
132
|
+
createdAt: row.at,
|
|
133
|
+
number: ref,
|
|
134
|
+
updatedAt: row.at,
|
|
135
|
+
version: row.sequence
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
const projection = projectionOf({
|
|
139
|
+
carried: ["lineage"],
|
|
140
|
+
columns: { ...entityColumns, hash: "hash", sequence: versionColumn },
|
|
141
|
+
constants: { [entityColumn]: name },
|
|
142
|
+
filterable: physicalHeads,
|
|
143
|
+
item,
|
|
144
|
+
key: { column: "number", param: "ref" },
|
|
145
|
+
order: [{ column: "number" }],
|
|
146
|
+
...placed,
|
|
147
|
+
seam,
|
|
148
|
+
sortable: [...physicalHeads, "number", physical.createdAt, physical.updatedAt],
|
|
149
|
+
table: declared
|
|
150
|
+
});
|
|
151
|
+
const list = (handle, params) => projection.list(handle, {
|
|
152
|
+
...params,
|
|
153
|
+
...params.filter === undefined ? {} : { filter: narrowed(params.filter) },
|
|
154
|
+
...params.order === undefined ? {} : {
|
|
155
|
+
order: {
|
|
156
|
+
column: columnOf(params.order.column),
|
|
157
|
+
...params.order.direction === undefined ? {} : { direction: params.order.direction }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
const locked = async (tx, params) => headFrom(await projection.lockLatest(tx, params));
|
|
162
|
+
const currentLineageOf = async (tx, params) => headFrom(await projection.head(tx, params))?.lineage ?? 1;
|
|
163
|
+
const stream = (params, lineage) => ({
|
|
164
|
+
...tenanted(params[tenantKey]),
|
|
165
|
+
entity: name,
|
|
166
|
+
lineage,
|
|
167
|
+
ref: params.ref
|
|
168
|
+
});
|
|
169
|
+
const headColumnsOf = (body) => Object.fromEntries(Object.entries(definition.headOf(body)).map(([key, held]) => [
|
|
170
|
+
columnOf(key),
|
|
171
|
+
held
|
|
172
|
+
]));
|
|
173
|
+
const storedHead = (ref, body) => headColumnsOf(readBody(definition, name, ref, body));
|
|
174
|
+
const append = async (tx, write) => {
|
|
175
|
+
const log = needsLog();
|
|
176
|
+
const params = {
|
|
177
|
+
...tenanted(write.tenantId),
|
|
178
|
+
actor: write.actor,
|
|
179
|
+
body: write.body,
|
|
180
|
+
entity: name,
|
|
181
|
+
kind: write.kind,
|
|
182
|
+
lineage: write.lineage,
|
|
183
|
+
ref: write.ref,
|
|
184
|
+
...write.reason === null ? {} : { reason: write.reason }
|
|
185
|
+
};
|
|
186
|
+
const out = write.compensationOf === null ? await log.append(tx, params) : await log.appendCompensation(tx, { ...params, compensationOf: write.compensationOf });
|
|
187
|
+
return { at: out.at, hash: out.hash, prevHash: out.prevHash, version: out.sequence };
|
|
188
|
+
};
|
|
189
|
+
const project = async (tx, write) => projection.write(tx, {
|
|
190
|
+
...tenanted(write.tenantId),
|
|
191
|
+
at: write.at,
|
|
192
|
+
body: write.body,
|
|
193
|
+
carried: { lineage: write.lineage },
|
|
194
|
+
derived: write.head,
|
|
195
|
+
hash: write.hash,
|
|
196
|
+
ref: write.ref,
|
|
197
|
+
retiredAt: write.retiredAt,
|
|
198
|
+
sequence: write.version
|
|
199
|
+
});
|
|
200
|
+
const commitTx = seam.scoped(async (tx, params) => {
|
|
201
|
+
const tenantId = params[tenantKey];
|
|
202
|
+
const prepared = definition.prepare(params.body);
|
|
203
|
+
const stamped = stampBody(definition, prepared);
|
|
204
|
+
const existing = await locked(tx, params);
|
|
205
|
+
if (existing !== undefined && existing.retiredAt !== null) {
|
|
206
|
+
throw new DbRefusal(`"${params.ref}" (${name}) is retired: commit refuses a retired document — succeed opens a new lineage for it`);
|
|
207
|
+
}
|
|
208
|
+
const changed = existing === undefined || JSON.stringify(canonicalOf(existing.body)) !== JSON.stringify(canonicalOf(stamped));
|
|
209
|
+
if (existing !== undefined && !changed) {
|
|
210
|
+
return {
|
|
211
|
+
committed: false,
|
|
212
|
+
lineage: existing.lineage,
|
|
213
|
+
ref: params.ref,
|
|
214
|
+
version: existing.version
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const lineage = existing?.lineage ?? 1;
|
|
218
|
+
const reason = params.reason ?? null;
|
|
219
|
+
const appended = await append(tx, {
|
|
220
|
+
actor: params.actor,
|
|
221
|
+
body: stamped,
|
|
222
|
+
compensationOf: null,
|
|
223
|
+
kind: "committed",
|
|
224
|
+
lineage,
|
|
225
|
+
reason,
|
|
226
|
+
ref: params.ref,
|
|
227
|
+
tenantId
|
|
228
|
+
});
|
|
229
|
+
await project(tx, {
|
|
230
|
+
at: appended.at,
|
|
231
|
+
body: stamped,
|
|
232
|
+
hash: appended.hash,
|
|
233
|
+
head: headColumnsOf(prepared),
|
|
234
|
+
lineage,
|
|
235
|
+
ref: params.ref,
|
|
236
|
+
retiredAt: null,
|
|
237
|
+
tenantId,
|
|
238
|
+
version: appended.version
|
|
239
|
+
});
|
|
240
|
+
return {
|
|
241
|
+
archivable: {
|
|
242
|
+
actor: params.actor,
|
|
243
|
+
at: appended.at,
|
|
244
|
+
body: stamped,
|
|
245
|
+
prevHash: appended.prevHash,
|
|
246
|
+
reason
|
|
247
|
+
},
|
|
248
|
+
committed: true,
|
|
249
|
+
hash: appended.hash,
|
|
250
|
+
lineage,
|
|
251
|
+
previous: existing === undefined ? null : { hash: existing.hash, version: existing.version },
|
|
252
|
+
ref: params.ref,
|
|
253
|
+
version: appended.version
|
|
254
|
+
};
|
|
255
|
+
});
|
|
256
|
+
const commit = async (handle, params) => {
|
|
257
|
+
const { archivable, ...answer } = await commitTx(handle, params);
|
|
258
|
+
if (archive === undefined || !answer.committed || archivable === undefined)
|
|
259
|
+
return answer;
|
|
260
|
+
const key = `${params[tenantKey]}/${name}/${answer.lineage}/${answer.version}.json`;
|
|
261
|
+
const payload = {
|
|
262
|
+
actor: archivable.actor,
|
|
263
|
+
at: archivable.at,
|
|
264
|
+
body: archivable.body,
|
|
265
|
+
entity: name,
|
|
266
|
+
hash: answer.hash,
|
|
267
|
+
kind: "committed",
|
|
268
|
+
lineage: answer.lineage,
|
|
269
|
+
prevHash: archivable.prevHash,
|
|
270
|
+
ref: params.ref,
|
|
271
|
+
reason: archivable.reason,
|
|
272
|
+
tenant: params[tenantKey],
|
|
273
|
+
version: answer.version
|
|
274
|
+
};
|
|
275
|
+
try {
|
|
276
|
+
await archive.put(key, new TextEncoder().encode(JSON.stringify(payload)), {
|
|
277
|
+
hash: answer.hash,
|
|
278
|
+
immutable: true
|
|
279
|
+
});
|
|
280
|
+
return { ...answer, archived: true };
|
|
281
|
+
} catch {
|
|
282
|
+
return { ...answer, archived: false };
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
const retire = seam.scoped(async (tx, params) => {
|
|
286
|
+
const tenantId = params[tenantKey];
|
|
287
|
+
const existing = await locked(tx, params);
|
|
288
|
+
if (existing === undefined) {
|
|
289
|
+
throw new DbRefusal(`"${params.ref}" (${name}) has no document to retire`);
|
|
290
|
+
}
|
|
291
|
+
if (existing.retiredAt !== null) {
|
|
292
|
+
throw new DbRefusal(`"${params.ref}" (${name}) is already retired`);
|
|
293
|
+
}
|
|
294
|
+
const appended = await append(tx, {
|
|
295
|
+
actor: params.actor,
|
|
296
|
+
body: existing.body,
|
|
297
|
+
compensationOf: null,
|
|
298
|
+
kind: "retired",
|
|
299
|
+
lineage: existing.lineage,
|
|
300
|
+
reason: params.reason ?? null,
|
|
301
|
+
ref: params.ref,
|
|
302
|
+
tenantId
|
|
303
|
+
});
|
|
304
|
+
await project(tx, {
|
|
305
|
+
at: appended.at,
|
|
306
|
+
body: existing.body,
|
|
307
|
+
hash: appended.hash,
|
|
308
|
+
head: storedHead(params.ref, existing.body),
|
|
309
|
+
lineage: existing.lineage,
|
|
310
|
+
ref: params.ref,
|
|
311
|
+
retiredAt: appended.at,
|
|
312
|
+
tenantId,
|
|
313
|
+
version: appended.version
|
|
314
|
+
});
|
|
315
|
+
return {
|
|
316
|
+
hash: appended.hash,
|
|
317
|
+
lineage: existing.lineage,
|
|
318
|
+
ref: params.ref,
|
|
319
|
+
version: appended.version
|
|
320
|
+
};
|
|
321
|
+
});
|
|
322
|
+
const succeed = seam.scoped(async (tx, params) => {
|
|
323
|
+
const tenantId = params[tenantKey];
|
|
324
|
+
const existing = await locked(tx, params);
|
|
325
|
+
if (existing === undefined || existing.retiredAt === null) {
|
|
326
|
+
throw new DbRefusal(`"${params.ref}" (${name}) is not retired: succeed opens a new lineage only for a retired document — commit is the door for a live one`);
|
|
327
|
+
}
|
|
328
|
+
const prepared = definition.prepare(params.body);
|
|
329
|
+
const stamped = stampBody(definition, prepared);
|
|
330
|
+
const lineage = existing.lineage + 1;
|
|
331
|
+
const appended = await append(tx, {
|
|
332
|
+
actor: params.actor,
|
|
333
|
+
body: stamped,
|
|
334
|
+
compensationOf: null,
|
|
335
|
+
kind: "committed",
|
|
336
|
+
lineage,
|
|
337
|
+
reason: params.reason ?? null,
|
|
338
|
+
ref: params.ref,
|
|
339
|
+
tenantId
|
|
340
|
+
});
|
|
341
|
+
await project(tx, {
|
|
342
|
+
at: appended.at,
|
|
343
|
+
body: stamped,
|
|
344
|
+
hash: appended.hash,
|
|
345
|
+
head: headColumnsOf(prepared),
|
|
346
|
+
lineage,
|
|
347
|
+
ref: params.ref,
|
|
348
|
+
retiredAt: null,
|
|
349
|
+
tenantId,
|
|
350
|
+
version: appended.version
|
|
351
|
+
});
|
|
352
|
+
return {
|
|
353
|
+
committed: true,
|
|
354
|
+
hash: appended.hash,
|
|
355
|
+
lineage,
|
|
356
|
+
previous: null,
|
|
357
|
+
ref: params.ref,
|
|
358
|
+
version: appended.version
|
|
359
|
+
};
|
|
360
|
+
});
|
|
361
|
+
const reinstate = seam.scoped(async (tx, params) => {
|
|
362
|
+
const tenantId = params[tenantKey];
|
|
363
|
+
const existing = await locked(tx, params);
|
|
364
|
+
if (existing === undefined || existing.retiredAt === null) {
|
|
365
|
+
throw new DbRefusal(`"${params.ref}" (${name}) is not retired: nothing to reinstate`);
|
|
366
|
+
}
|
|
367
|
+
const appended = await append(tx, {
|
|
368
|
+
actor: params.actor,
|
|
369
|
+
body: existing.body,
|
|
370
|
+
compensationOf: null,
|
|
371
|
+
kind: "reinstated",
|
|
372
|
+
lineage: existing.lineage,
|
|
373
|
+
reason: params.reason ?? null,
|
|
374
|
+
ref: params.ref,
|
|
375
|
+
tenantId
|
|
376
|
+
});
|
|
377
|
+
await project(tx, {
|
|
378
|
+
at: appended.at,
|
|
379
|
+
body: existing.body,
|
|
380
|
+
hash: appended.hash,
|
|
381
|
+
head: storedHead(params.ref, existing.body),
|
|
382
|
+
lineage: existing.lineage,
|
|
383
|
+
ref: params.ref,
|
|
384
|
+
retiredAt: null,
|
|
385
|
+
tenantId,
|
|
386
|
+
version: appended.version
|
|
387
|
+
});
|
|
388
|
+
return {
|
|
389
|
+
hash: appended.hash,
|
|
390
|
+
lineage: existing.lineage,
|
|
391
|
+
ref: params.ref,
|
|
392
|
+
version: appended.version
|
|
393
|
+
};
|
|
394
|
+
});
|
|
395
|
+
const at = seam.scoped(async (tx, params) => {
|
|
396
|
+
const lineage = await currentLineageOf(tx, params);
|
|
397
|
+
const row = await needsLog().at(tx, {
|
|
398
|
+
...stream(params, lineage),
|
|
399
|
+
sequence: params.version
|
|
400
|
+
});
|
|
401
|
+
return row === null ? null : instanceFromRevision(params.ref, row);
|
|
402
|
+
});
|
|
403
|
+
const asOf = seam.scoped(async (tx, params) => {
|
|
404
|
+
const lineage = await currentLineageOf(tx, params);
|
|
405
|
+
const row = await needsLog().asOf(tx, { ...stream(params, lineage), at: params.at });
|
|
406
|
+
return row === null ? null : instanceFromRevision(params.ref, row);
|
|
407
|
+
});
|
|
408
|
+
const history = seam.scoped(async (tx, params) => {
|
|
409
|
+
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
410
|
+
const rows = await needsLog().history(tx, stream(params, lineage));
|
|
411
|
+
return rows.map((row) => ({
|
|
412
|
+
actor: row.actor,
|
|
413
|
+
at: row.at,
|
|
414
|
+
body: readBody(definition, name, `${params.ref} version ${row.sequence}`, row.body),
|
|
415
|
+
hash: row.hash,
|
|
416
|
+
kind: row.kind,
|
|
417
|
+
reason: row.reason,
|
|
418
|
+
version: row.sequence
|
|
419
|
+
}));
|
|
420
|
+
});
|
|
421
|
+
const verify = seam.scoped(async (tx, params) => {
|
|
422
|
+
const lineage = params.lineage ?? await currentLineageOf(tx, params);
|
|
423
|
+
return needsLog().verify(tx, stream(params, lineage));
|
|
424
|
+
});
|
|
425
|
+
const compensate = async (scoped, tenantId, output, kind, body, retired) => {
|
|
426
|
+
const appended = await append(scoped, {
|
|
427
|
+
actor: UNDO_ACTOR,
|
|
428
|
+
body,
|
|
429
|
+
compensationOf: output.version,
|
|
430
|
+
kind,
|
|
431
|
+
lineage: output.lineage,
|
|
432
|
+
reason: `undo of v${output.version}`,
|
|
433
|
+
ref: output.ref,
|
|
434
|
+
tenantId
|
|
435
|
+
});
|
|
436
|
+
await project(scoped, {
|
|
437
|
+
at: appended.at,
|
|
438
|
+
body,
|
|
439
|
+
hash: appended.hash,
|
|
440
|
+
head: storedHead(output.ref, body),
|
|
441
|
+
lineage: output.lineage,
|
|
442
|
+
ref: output.ref,
|
|
443
|
+
retiredAt: retired ? appended.at : null,
|
|
444
|
+
tenantId,
|
|
445
|
+
version: appended.version
|
|
446
|
+
});
|
|
447
|
+
};
|
|
448
|
+
const lockedForUndo = async (scoped, tenantId, output) => {
|
|
449
|
+
const existing = await locked(scoped, { ...tenanted(tenantId), ref: output.ref });
|
|
450
|
+
if (existing === undefined) {
|
|
451
|
+
throw new DbRefusal(`"${output.ref}" (${name}) has nothing to undo — the row v${output.version} wrote is gone`);
|
|
452
|
+
}
|
|
453
|
+
return existing;
|
|
454
|
+
};
|
|
455
|
+
const undos = {
|
|
456
|
+
commit: (tx, { output, tenantId }) => seam.inTenant(tx, tenantId, async (scoped) => {
|
|
457
|
+
if (!output.committed)
|
|
458
|
+
return;
|
|
459
|
+
const existing = await lockedForUndo(scoped, tenantId, output);
|
|
460
|
+
if (output.previous === null) {
|
|
461
|
+
await compensate(scoped, tenantId, output, "retired", existing.body, true);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const prior = await needsLog().at(scoped, {
|
|
465
|
+
...stream({ ...tenanted(tenantId), ref: output.ref }, output.lineage),
|
|
466
|
+
sequence: output.previous.version
|
|
467
|
+
});
|
|
468
|
+
if (prior === null) {
|
|
469
|
+
throw new DbRefusal(`"${output.ref}" (${name}) version ${output.previous.version} is gone — cannot undo v${output.version} without it`);
|
|
470
|
+
}
|
|
471
|
+
await compensate(scoped, tenantId, output, "committed", prior.body, false);
|
|
472
|
+
}),
|
|
473
|
+
retire: (tx, { output, tenantId }) => seam.inTenant(tx, tenantId, async (scoped) => {
|
|
474
|
+
const existing = await lockedForUndo(scoped, tenantId, output);
|
|
475
|
+
await compensate(scoped, tenantId, output, "reinstated", existing.body, false);
|
|
476
|
+
})
|
|
477
|
+
};
|
|
478
|
+
const restore = async (tx, row, createdAt) => {
|
|
479
|
+
const ref = String(row.ref);
|
|
480
|
+
await project(tx, {
|
|
481
|
+
at: createdAt,
|
|
482
|
+
body: row.body,
|
|
483
|
+
hash: row.hash,
|
|
484
|
+
head: storedHead(ref, row.body),
|
|
485
|
+
lineage: Number(row.lineage),
|
|
486
|
+
ref,
|
|
487
|
+
retiredAt: row.kind === "retired" ? row.at : null,
|
|
488
|
+
tenantId: row.tenant,
|
|
489
|
+
version: row.sequence
|
|
490
|
+
});
|
|
491
|
+
};
|
|
492
|
+
return {
|
|
493
|
+
door: {
|
|
494
|
+
asOf,
|
|
495
|
+
at,
|
|
496
|
+
commit,
|
|
497
|
+
get: projection.get,
|
|
498
|
+
history,
|
|
499
|
+
list,
|
|
500
|
+
listAll: projection.listAll,
|
|
501
|
+
reinstate,
|
|
502
|
+
retire,
|
|
503
|
+
succeed,
|
|
504
|
+
undos,
|
|
505
|
+
verify
|
|
506
|
+
},
|
|
507
|
+
restore
|
|
508
|
+
};
|
|
509
|
+
};
|
|
510
|
+
const built = registry.names.map((name) => [name, entityStoreFor(name)]);
|
|
511
|
+
const rawEntities = Object.fromEntries(built.map(([name, one]) => [name, one.door]));
|
|
512
|
+
const restorers = Object.fromEntries(built.map(([name, one]) => [name, one.restore]));
|
|
513
|
+
const heads = seam.scoped(async (tx, params) => {
|
|
514
|
+
const { columns, entity, order, where } = params;
|
|
515
|
+
const rows = await readers.project(tx, {
|
|
516
|
+
...tenanted(params[tenantKey]),
|
|
517
|
+
columns: columns.filter((column) => column !== "number").map(columnOf),
|
|
518
|
+
order: order.map((one) => ({
|
|
519
|
+
column: columnOf(one.column),
|
|
520
|
+
...one.direction === undefined ? {} : { direction: one.direction }
|
|
521
|
+
})),
|
|
522
|
+
where: {
|
|
523
|
+
...entity === undefined ? {} : { [entityColumn]: entity },
|
|
524
|
+
...narrowed(where)
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
return rows.map((row) => Object.fromEntries(columns.map((column) => [column, column === "number" ? row.key : row[columnOf(column)]])));
|
|
528
|
+
});
|
|
529
|
+
const aggregate = seam.scoped((tx, params) => {
|
|
530
|
+
const { by, entities, entity, measures, where } = params;
|
|
531
|
+
return readers.aggregate(tx, {
|
|
532
|
+
...tenanted(params[tenantKey]),
|
|
533
|
+
...by === undefined ? {} : {
|
|
534
|
+
by: typeof by === "string" ? columnOf(by) : { bucket: by.bucket, of: columnOf(by.of) }
|
|
535
|
+
},
|
|
536
|
+
measures: measures.map((one) => one === "sum:total" ? `sum:${physical.total}` : one),
|
|
537
|
+
where: {
|
|
538
|
+
...entity === undefined ? {} : { [entityColumn]: entity },
|
|
539
|
+
...entities === undefined ? {} : { [entityColumn]: entities },
|
|
540
|
+
...narrowed(where)
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
const rebuild = seam.scoped(async (tx, params) => {
|
|
545
|
+
const log = needsLog();
|
|
546
|
+
const tenant = tenanted(params[tenantKey]);
|
|
547
|
+
const latest = new Map;
|
|
548
|
+
for (const row of await log.latestPerStream(tx, tenant)) {
|
|
549
|
+
if (params.entity !== undefined && row.entity !== params.entity)
|
|
550
|
+
continue;
|
|
551
|
+
const address = `${String(row.entity)} ${String(row.ref)}`;
|
|
552
|
+
const held = latest.get(address);
|
|
553
|
+
if (held === undefined || Number(row.lineage) > Number(held.lineage)) {
|
|
554
|
+
latest.set(address, row);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
let rows = 0;
|
|
558
|
+
for (const row of latest.values()) {
|
|
559
|
+
const restore = restorers[String(row.entity)];
|
|
560
|
+
if (restore === undefined)
|
|
561
|
+
continue;
|
|
562
|
+
const first = await log.at(tx, {
|
|
563
|
+
...tenant,
|
|
564
|
+
entity: row.entity,
|
|
565
|
+
lineage: row.lineage,
|
|
566
|
+
ref: row.ref,
|
|
567
|
+
sequence: 1
|
|
568
|
+
});
|
|
569
|
+
await restore(tx, row, first?.at ?? row.at);
|
|
570
|
+
rows += 1;
|
|
571
|
+
}
|
|
572
|
+
return { rows };
|
|
573
|
+
});
|
|
574
|
+
return { ...rawEntities, aggregate, heads, rebuild };
|
|
575
|
+
};
|
|
576
|
+
export {
|
|
577
|
+
documentStore
|
|
578
|
+
};
|