@geonosis/document 0.9.3 → 0.10.1
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 +62 -0
- package/README.md +78 -13
- package/dist/core/actor.d.cts +0 -1
- package/dist/core/actor.d.ts +0 -1
- package/dist/core/body-version.d.cts +0 -9
- package/dist/core/body-version.d.ts +0 -9
- package/dist/core/canonical.cjs +2 -3
- package/dist/core/canonical.d.cts +0 -4
- package/dist/core/canonical.d.ts +0 -4
- package/dist/core/canonical.js +2 -3
- package/dist/core/document.cjs +28 -25
- package/dist/core/document.d.cts +3 -22
- package/dist/core/document.d.ts +3 -22
- package/dist/core/document.js +28 -25
- package/dist/core/entities.d.cts +0 -19
- package/dist/core/entities.d.ts +0 -19
- package/dist/core/naming.d.cts +0 -5
- package/dist/core/naming.d.ts +0 -5
- package/dist/core/numbering.d.cts +0 -4
- package/dist/core/numbering.d.ts +0 -4
- package/dist/core/ports.d.cts +0 -6
- package/dist/core/ports.d.ts +0 -6
- package/dist/core/schema.cjs +44 -76
- package/dist/core/schema.d.cts +1 -13
- package/dist/core/schema.d.ts +1 -13
- package/dist/core/schema.js +44 -76
- package/dist/core/status.d.cts +0 -1
- package/dist/core/status.d.ts +0 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -0
- package/dist/store/ddl.cjs +35 -38
- package/dist/store/ddl.d.cts +6 -6
- package/dist/store/ddl.d.ts +6 -6
- package/dist/store/ddl.js +37 -39
- package/dist/store/hash.d.cts +0 -1
- package/dist/store/hash.d.ts +0 -1
- package/dist/store/number-for.cjs +52 -0
- package/dist/store/number-for.d.cts +11 -0
- package/dist/store/number-for.d.ts +11 -0
- package/dist/store/number-for.js +12 -0
- package/dist/store/store.cjs +188 -162
- package/dist/store/store.d.cts +57 -16
- package/dist/store/store.d.ts +57 -16
- package/dist/store/store.js +190 -163
- package/dist/store.cjs +2 -0
- package/dist/store.d.cts +3 -2
- package/dist/store.d.ts +3 -2
- package/dist/store.js +2 -0
- package/dist/surface/table.cjs +129 -66
- package/dist/surface/table.d.cts +20 -28
- package/dist/surface/table.d.ts +20 -28
- package/dist/surface/table.js +132 -66
- package/dist/workflows/bindings.cjs +48 -0
- package/dist/workflows/bindings.d.cts +15 -0
- package/dist/workflows/bindings.d.ts +15 -0
- package/dist/workflows/bindings.js +10 -0
- package/dist/workflows/document-workflows.cjs +29 -40
- package/dist/workflows/document-workflows.d.cts +0 -12
- package/dist/workflows/document-workflows.d.ts +0 -12
- package/dist/workflows/document-workflows.js +29 -40
- package/dist/workflows/effects.d.cts +0 -12
- package/dist/workflows/effects.d.ts +0 -12
- package/dist/workflows/ports.d.cts +0 -17
- package/dist/workflows/ports.d.ts +0 -17
- package/dist/workflows/refusals.cjs +30 -0
- package/dist/workflows/refusals.d.cts +37 -9
- package/dist/workflows/refusals.d.ts +37 -9
- package/dist/workflows/refusals.js +30 -0
- package/dist/workflows/shared.cjs +93 -0
- package/dist/workflows/shared.d.cts +28 -0
- package/dist/workflows/shared.d.ts +28 -0
- package/dist/workflows/shared.js +60 -0
- package/dist/workflows.cjs +16 -1
- package/dist/workflows.d.cts +3 -1
- package/dist/workflows.d.ts +3 -1
- package/dist/workflows.js +31 -1
- package/package.json +4 -4
package/dist/store/store.cjs
CHANGED
|
@@ -48,20 +48,39 @@ var import_body_version = require("../core/body-version.cjs");
|
|
|
48
48
|
var import_canonical = require("../core/canonical.cjs");
|
|
49
49
|
var import_ddl = require("./ddl.cjs");
|
|
50
50
|
var import_hash = require("./hash.cjs");
|
|
51
|
-
var DEFAULT_VERSION_COLUMN = "version";
|
|
52
51
|
var UNDO_ACTOR = import_actor.systemActor("document.undo");
|
|
53
|
-
var nameOf = (value, fallback) => value === undefined ? fallback : typeof value === "string" ? value : value.name;
|
|
54
52
|
var readBody = (entity, entityName, ref, stored) => {
|
|
55
53
|
const migrated = import_body_version.migrateBody(entity, stored);
|
|
56
54
|
const result = entity.body["~standard"].validate(migrated);
|
|
57
55
|
if (result instanceof Promise) {
|
|
58
|
-
throw new
|
|
56
|
+
throw new import_db.DbRefusal(`${entityName} ${ref}: this entity's body schema validates asynchronously, and a stored body can only be read synchronously here`, { code: "body-refused" });
|
|
59
57
|
}
|
|
60
58
|
if (result.issues !== undefined) {
|
|
61
|
-
throw new
|
|
59
|
+
throw new import_db.DbRefusal(`${entityName} ${ref}'s stored body no longer satisfies its own schema after migration: ${result.issues.map((issue) => issue.message).join("; ")}`, { code: "body-refused" });
|
|
62
60
|
}
|
|
63
61
|
return result.value;
|
|
64
62
|
};
|
|
63
|
+
var refusesAsArchiveSegment = (segment) => segment === "" || segment === "." || segment === "..";
|
|
64
|
+
var archiveKeyPrefix = (tenant, entity, ref) => {
|
|
65
|
+
const refused = [tenant, entity].find((part) => part.split(/[/\\]/).some(refusesAsArchiveSegment)) ?? (refusesAsArchiveSegment(ref) ? ref : undefined);
|
|
66
|
+
if (refused !== undefined) {
|
|
67
|
+
throw new import_db.DbRefusal(`${JSON.stringify(refused)} cannot name an archive object: an empty, "." or ".." path segment lets a key reach another document's space`);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return `${tenant}/${entity}/${encodeURIComponent(ref)}`;
|
|
71
|
+
} catch (cause) {
|
|
72
|
+
throw new import_db.DbRefusal(`the ref ${JSON.stringify(ref)} is not text an archive key can carry`, {
|
|
73
|
+
cause
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var ALL_HEAD_COLUMNS = [
|
|
78
|
+
...import_ddl.HEAD_FIELDS,
|
|
79
|
+
"createdAt",
|
|
80
|
+
"entity",
|
|
81
|
+
"number",
|
|
82
|
+
"updatedAt"
|
|
83
|
+
];
|
|
65
84
|
var headFrom = (found) => found === undefined ? undefined : {
|
|
66
85
|
body: found.body,
|
|
67
86
|
hash: found.hash,
|
|
@@ -73,17 +92,9 @@ var documentStore = (options) => {
|
|
|
73
92
|
const table = options.table ?? import_ddl.DEFAULT_DOCUMENT_TABLE;
|
|
74
93
|
const revisionKindColumn = options.columns?.revisionKind ?? import_ddl.DEFAULT_REVISION_KIND_COLUMN;
|
|
75
94
|
const physical = import_ddl.physicalColumnsOf(options.columns);
|
|
76
|
-
const {
|
|
77
|
-
entity: _entity,
|
|
78
|
-
heads: _heads,
|
|
79
|
-
revisionKind: _revisionKind,
|
|
80
|
-
total: _total,
|
|
81
|
-
version,
|
|
82
|
-
...entityColumns
|
|
83
|
-
} = options.columns ?? {};
|
|
84
|
-
const versionColumn = nameOf(version, DEFAULT_VERSION_COLUMN);
|
|
95
|
+
const { rest: entityColumns, version: versionColumn } = import_ddl.splitColumns(options.columns);
|
|
85
96
|
const entityColumn = physical.entity;
|
|
86
|
-
const { archive, registry, schema, seam } = options;
|
|
97
|
+
const { archive, registry, schema, search, seam, views } = options;
|
|
87
98
|
const tenantKey = seam.tenantKey;
|
|
88
99
|
const placed = schema === undefined ? {} : { schema };
|
|
89
100
|
const declared = import_ddl.documentTable({
|
|
@@ -95,6 +106,11 @@ var documentStore = (options) => {
|
|
|
95
106
|
const tenanted = (tenantId) => ({ [tenantKey]: tenantId });
|
|
96
107
|
const columnOf = (column) => column === "number" ? "number" : physical[column];
|
|
97
108
|
const physicalHeads = import_ddl.HEAD_FIELDS.map(columnOf);
|
|
109
|
+
const headColumnsOf = (head) => Object.fromEntries(Object.entries(head).map(([key, held]) => [columnOf(key), held]));
|
|
110
|
+
const orderOf = (one) => ({
|
|
111
|
+
column: columnOf(one.column),
|
|
112
|
+
...one.direction === undefined ? {} : { direction: one.direction }
|
|
113
|
+
});
|
|
98
114
|
const narrowed = (where) => Object.fromEntries(Object.entries(where ?? {}).map(([column, held]) => [
|
|
99
115
|
columnOf(column),
|
|
100
116
|
held
|
|
@@ -140,32 +156,53 @@ var documentStore = (options) => {
|
|
|
140
156
|
sortable: [...physicalHeads, entityColumn, "number", physical.createdAt, physical.updatedAt],
|
|
141
157
|
table: declared
|
|
142
158
|
});
|
|
159
|
+
const instanceFor = (name, stored, storedBody) => {
|
|
160
|
+
const definition = registry.named(name);
|
|
161
|
+
const body = readBody(definition, name, stored.number, storedBody);
|
|
162
|
+
return registry.instance(name, {
|
|
163
|
+
...definition.headOf(body),
|
|
164
|
+
...stored,
|
|
165
|
+
body
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
const itemFor = (name) => (row) => instanceFor(name, {
|
|
169
|
+
createdAt: row.createdAt,
|
|
170
|
+
number: row.key,
|
|
171
|
+
updatedAt: row.updatedAt,
|
|
172
|
+
version: row.version ?? 1
|
|
173
|
+
}, row.body);
|
|
174
|
+
const itemsByEntity = new Map(registry.names.map((name) => [String(name), itemFor(name)]));
|
|
175
|
+
const pager = import_db.defineEntity({
|
|
176
|
+
carried: [entityColumn],
|
|
177
|
+
columns: { ...entityColumns, version: versionColumn },
|
|
178
|
+
filterable: [...physicalHeads, entityColumn, physical.createdAt, physical.updatedAt],
|
|
179
|
+
item: (row) => {
|
|
180
|
+
const entityName = String(row.carried?.[entityColumn]);
|
|
181
|
+
const make = itemsByEntity.get(entityName);
|
|
182
|
+
if (make === undefined) {
|
|
183
|
+
throw new import_db.DbRefusal(`${JSON.stringify(entityName)} is stored as the entity of "${row.key}" and this store's registry holds no such entity: it holds ${registry.names.join(", ")}`);
|
|
184
|
+
}
|
|
185
|
+
return make(row);
|
|
186
|
+
},
|
|
187
|
+
key: { column: "number", param: "ref" },
|
|
188
|
+
order: [{ column: entityColumn }, { column: "number" }],
|
|
189
|
+
seam,
|
|
190
|
+
sortable: [...physicalHeads, entityColumn, "number", physical.createdAt, physical.updatedAt],
|
|
191
|
+
table: declared,
|
|
192
|
+
tiebreak: [entityColumn, "number"]
|
|
193
|
+
});
|
|
194
|
+
const listEverywhere = (handle, params) => pager.list(handle, {
|
|
195
|
+
...params,
|
|
196
|
+
filter: {
|
|
197
|
+
...narrowed(params.filter),
|
|
198
|
+
...params.entities === undefined ? {} : { [entityColumn]: params.entities }
|
|
199
|
+
},
|
|
200
|
+
...params.order === undefined ? {} : { order: orderOf(params.order) }
|
|
201
|
+
});
|
|
143
202
|
const entityStoreFor = (name) => {
|
|
144
203
|
const definition = registry.named(name);
|
|
145
|
-
const item = (
|
|
146
|
-
|
|
147
|
-
const head = definition.headOf(body);
|
|
148
|
-
return registry.instance(name, {
|
|
149
|
-
...head,
|
|
150
|
-
body,
|
|
151
|
-
createdAt: row.createdAt,
|
|
152
|
-
number: row.key,
|
|
153
|
-
updatedAt: row.updatedAt,
|
|
154
|
-
version: row.version ?? 1
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
const instanceFromRevision = (ref, row) => {
|
|
158
|
-
const body = readBody(definition, name, ref, row.body);
|
|
159
|
-
const head = definition.headOf(body);
|
|
160
|
-
return registry.instance(name, {
|
|
161
|
-
...head,
|
|
162
|
-
body,
|
|
163
|
-
createdAt: row.at,
|
|
164
|
-
number: ref,
|
|
165
|
-
updatedAt: row.at,
|
|
166
|
-
version: row.sequence
|
|
167
|
-
});
|
|
168
|
-
};
|
|
204
|
+
const item = itemFor(name);
|
|
205
|
+
const instanceFromRevision = (ref, row) => instanceFor(name, { createdAt: row.at, number: ref, updatedAt: row.at, version: row.sequence }, row.body);
|
|
169
206
|
const projection = import_db.projectionOf({
|
|
170
207
|
carried: ["lineage"],
|
|
171
208
|
columns: { ...entityColumns, hash: "hash", sequence: versionColumn },
|
|
@@ -182,12 +219,34 @@ var documentStore = (options) => {
|
|
|
182
219
|
const list = (handle, params) => projection.list(handle, {
|
|
183
220
|
...params,
|
|
184
221
|
...params.filter === undefined ? {} : { filter: narrowed(params.filter) },
|
|
185
|
-
...params.order === undefined ? {} : {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
222
|
+
...params.order === undefined ? {} : { order: orderOf(params.order) }
|
|
223
|
+
});
|
|
224
|
+
const count = (handle, params) => readers.count(handle, {
|
|
225
|
+
...tenanted(params[tenantKey]),
|
|
226
|
+
filter: { ...narrowed(params.where), [entityColumn]: name }
|
|
227
|
+
});
|
|
228
|
+
const keys = async (handle, params) => {
|
|
229
|
+
const order = params.order ?? [];
|
|
230
|
+
const rows = await readers.project(handle, {
|
|
231
|
+
...tenanted(params[tenantKey]),
|
|
232
|
+
columns: [],
|
|
233
|
+
order: [
|
|
234
|
+
...order.map(orderOf),
|
|
235
|
+
...order.some((one) => one.column === "number") ? [] : [{ column: "number" }]
|
|
236
|
+
],
|
|
237
|
+
where: { ...narrowed(params.where), [entityColumn]: name }
|
|
238
|
+
});
|
|
239
|
+
return rows.map((row) => row.key);
|
|
240
|
+
};
|
|
241
|
+
const render = seam.scoped(async (tx, params) => {
|
|
242
|
+
const view = views?.[name];
|
|
243
|
+
if (view === undefined && definition.view !== definition.body) {
|
|
244
|
+
throw new import_db.DbRefusal(`"${name}" declares a view of its own and this store was given no \`views.${name}\` to render it with: \`render\` would answer the body where the view is the contract`);
|
|
190
245
|
}
|
|
246
|
+
const found = await projection.get(tx, params);
|
|
247
|
+
if (found === null)
|
|
248
|
+
return null;
|
|
249
|
+
return view === undefined ? found.body : view(tx, found);
|
|
191
250
|
});
|
|
192
251
|
const locked = async (tx, params) => headFrom(await projection.lockLatest(tx, params));
|
|
193
252
|
const currentLineageOf = async (tx, params) => headFrom(await projection.head(tx, params))?.lineage ?? 1;
|
|
@@ -197,13 +256,21 @@ var documentStore = (options) => {
|
|
|
197
256
|
lineage,
|
|
198
257
|
ref: params.ref
|
|
199
258
|
});
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
259
|
+
const storedHead = (ref, body) => headColumnsOf(definition.headOf(readBody(definition, name, ref, body)));
|
|
260
|
+
const project = async (tx, write) => projection.write(tx, {
|
|
261
|
+
...tenanted(write.tenantId),
|
|
262
|
+
at: write.at,
|
|
263
|
+
body: write.body,
|
|
264
|
+
carried: { lineage: write.lineage },
|
|
265
|
+
derived: write.head,
|
|
266
|
+
hash: write.hash,
|
|
267
|
+
ref: write.ref,
|
|
268
|
+
retiredAt: write.retiredAt,
|
|
269
|
+
sequence: write.version
|
|
270
|
+
});
|
|
271
|
+
const record = async (tx, write) => {
|
|
206
272
|
const log = needsLog();
|
|
273
|
+
const head = definition.headOf(write.seen);
|
|
207
274
|
const params = {
|
|
208
275
|
...tenanted(write.tenantId),
|
|
209
276
|
actor: write.actor,
|
|
@@ -214,20 +281,34 @@ var documentStore = (options) => {
|
|
|
214
281
|
ref: write.ref,
|
|
215
282
|
...write.reason === null ? {} : { reason: write.reason }
|
|
216
283
|
};
|
|
217
|
-
const out = write.compensationOf ===
|
|
218
|
-
|
|
284
|
+
const out = write.compensationOf === undefined ? await log.append(tx, params) : await log.appendCompensation(tx, { ...params, compensationOf: write.compensationOf });
|
|
285
|
+
await project(tx, {
|
|
286
|
+
at: out.at,
|
|
287
|
+
body: write.body,
|
|
288
|
+
hash: out.hash,
|
|
289
|
+
head: headColumnsOf(head),
|
|
290
|
+
lineage: write.lineage,
|
|
291
|
+
ref: write.ref,
|
|
292
|
+
retiredAt: write.retired ? out.at : null,
|
|
293
|
+
tenantId: write.tenantId,
|
|
294
|
+
version: out.sequence
|
|
295
|
+
});
|
|
296
|
+
if (search !== undefined) {
|
|
297
|
+
await (write.kind === "retired" ? search.remove(tx, { entity: name, ref: write.ref }) : search.index(tx, {
|
|
298
|
+
entity: name,
|
|
299
|
+
head,
|
|
300
|
+
ref: write.ref,
|
|
301
|
+
text: definition.searchText(write.seen)
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
at: out.at,
|
|
306
|
+
hash: out.hash,
|
|
307
|
+
head,
|
|
308
|
+
prevHash: out.prevHash,
|
|
309
|
+
version: out.sequence
|
|
310
|
+
};
|
|
219
311
|
};
|
|
220
|
-
const project = async (tx, write) => projection.write(tx, {
|
|
221
|
-
...tenanted(write.tenantId),
|
|
222
|
-
at: write.at,
|
|
223
|
-
body: write.body,
|
|
224
|
-
carried: { lineage: write.lineage },
|
|
225
|
-
derived: write.head,
|
|
226
|
-
hash: write.hash,
|
|
227
|
-
ref: write.ref,
|
|
228
|
-
retiredAt: write.retiredAt,
|
|
229
|
-
sequence: write.version
|
|
230
|
-
});
|
|
231
312
|
const commitTx = seam.scoped(async (tx, params) => {
|
|
232
313
|
const tenantId = params[tenantKey];
|
|
233
314
|
const prepared = definition.prepare(params.body);
|
|
@@ -240,6 +321,7 @@ var documentStore = (options) => {
|
|
|
240
321
|
if (existing !== undefined && !changed) {
|
|
241
322
|
return {
|
|
242
323
|
committed: false,
|
|
324
|
+
head: definition.headOf(prepared),
|
|
243
325
|
lineage: existing.lineage,
|
|
244
326
|
ref: params.ref,
|
|
245
327
|
version: existing.version
|
|
@@ -247,27 +329,17 @@ var documentStore = (options) => {
|
|
|
247
329
|
}
|
|
248
330
|
const lineage = existing?.lineage ?? 1;
|
|
249
331
|
const reason = params.reason ?? null;
|
|
250
|
-
const appended = await
|
|
332
|
+
const appended = await record(tx, {
|
|
251
333
|
actor: params.actor,
|
|
252
334
|
body: stamped,
|
|
253
|
-
compensationOf: null,
|
|
254
335
|
kind: "committed",
|
|
255
336
|
lineage,
|
|
256
337
|
reason,
|
|
257
338
|
ref: params.ref,
|
|
339
|
+
retired: false,
|
|
340
|
+
seen: prepared,
|
|
258
341
|
tenantId
|
|
259
342
|
});
|
|
260
|
-
await project(tx, {
|
|
261
|
-
at: appended.at,
|
|
262
|
-
body: stamped,
|
|
263
|
-
hash: appended.hash,
|
|
264
|
-
head: headColumnsOf(prepared),
|
|
265
|
-
lineage,
|
|
266
|
-
ref: params.ref,
|
|
267
|
-
retiredAt: null,
|
|
268
|
-
tenantId,
|
|
269
|
-
version: appended.version
|
|
270
|
-
});
|
|
271
343
|
return {
|
|
272
344
|
archivable: {
|
|
273
345
|
actor: params.actor,
|
|
@@ -278,6 +350,7 @@ var documentStore = (options) => {
|
|
|
278
350
|
},
|
|
279
351
|
committed: true,
|
|
280
352
|
hash: appended.hash,
|
|
353
|
+
head: appended.head,
|
|
281
354
|
lineage,
|
|
282
355
|
previous: existing === undefined ? null : { hash: existing.hash, version: existing.version },
|
|
283
356
|
ref: params.ref,
|
|
@@ -285,10 +358,11 @@ var documentStore = (options) => {
|
|
|
285
358
|
};
|
|
286
359
|
});
|
|
287
360
|
const commit = async (handle, params) => {
|
|
361
|
+
const keyPrefix = archive === undefined ? undefined : archiveKeyPrefix(params[tenantKey], name, params.ref);
|
|
288
362
|
const { archivable, ...answer } = await commitTx(handle, params);
|
|
289
363
|
if (archive === undefined || !answer.committed || archivable === undefined)
|
|
290
364
|
return answer;
|
|
291
|
-
const key = `${
|
|
365
|
+
const key = `${keyPrefix}/${answer.lineage}/${answer.version}.json`;
|
|
292
366
|
const payload = {
|
|
293
367
|
actor: archivable.actor,
|
|
294
368
|
at: archivable.at,
|
|
@@ -313,43 +387,36 @@ var documentStore = (options) => {
|
|
|
313
387
|
return { ...answer, archived: false };
|
|
314
388
|
}
|
|
315
389
|
};
|
|
316
|
-
const
|
|
317
|
-
const
|
|
318
|
-
const
|
|
319
|
-
if (existing === undefined) {
|
|
320
|
-
throw new import_db.DbRefusal(`"${params.ref}" (${name}) has no document to retire`);
|
|
321
|
-
}
|
|
322
|
-
if (existing.retiredAt !== null) {
|
|
323
|
-
throw new import_db.DbRefusal(`"${params.ref}" (${name}) is already retired`);
|
|
324
|
-
}
|
|
325
|
-
const appended = await append(tx, {
|
|
390
|
+
const flip = (kind, claim) => seam.scoped(async (tx, params) => {
|
|
391
|
+
const existing = claim(await locked(tx, params), params.ref);
|
|
392
|
+
const appended = await record(tx, {
|
|
326
393
|
actor: params.actor,
|
|
327
394
|
body: existing.body,
|
|
328
|
-
|
|
329
|
-
kind: "retired",
|
|
395
|
+
kind,
|
|
330
396
|
lineage: existing.lineage,
|
|
331
397
|
reason: params.reason ?? null,
|
|
332
398
|
ref: params.ref,
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
at: appended.at,
|
|
337
|
-
body: existing.body,
|
|
338
|
-
hash: appended.hash,
|
|
339
|
-
head: storedHead(params.ref, existing.body),
|
|
340
|
-
lineage: existing.lineage,
|
|
341
|
-
ref: params.ref,
|
|
342
|
-
retiredAt: appended.at,
|
|
343
|
-
tenantId,
|
|
344
|
-
version: appended.version
|
|
399
|
+
retired: kind === "retired",
|
|
400
|
+
seen: readBody(definition, name, params.ref, existing.body),
|
|
401
|
+
tenantId: params[tenantKey]
|
|
345
402
|
});
|
|
346
403
|
return {
|
|
347
404
|
hash: appended.hash,
|
|
405
|
+
head: appended.head,
|
|
348
406
|
lineage: existing.lineage,
|
|
349
407
|
ref: params.ref,
|
|
350
408
|
version: appended.version
|
|
351
409
|
};
|
|
352
410
|
});
|
|
411
|
+
const retire = flip("retired", (existing, ref) => {
|
|
412
|
+
if (existing === undefined) {
|
|
413
|
+
throw new import_db.DbRefusal(`"${ref}" (${name}) has no document to retire`);
|
|
414
|
+
}
|
|
415
|
+
if (existing.retiredAt !== null) {
|
|
416
|
+
throw new import_db.DbRefusal(`"${ref}" (${name}) is already retired`);
|
|
417
|
+
}
|
|
418
|
+
return existing;
|
|
419
|
+
});
|
|
353
420
|
const succeed = seam.scoped(async (tx, params) => {
|
|
354
421
|
const tenantId = params[tenantKey];
|
|
355
422
|
const existing = await locked(tx, params);
|
|
@@ -359,69 +426,32 @@ var documentStore = (options) => {
|
|
|
359
426
|
const prepared = definition.prepare(params.body);
|
|
360
427
|
const stamped = import_body_version.stampBody(definition, prepared);
|
|
361
428
|
const lineage = existing.lineage + 1;
|
|
362
|
-
const appended = await
|
|
429
|
+
const appended = await record(tx, {
|
|
363
430
|
actor: params.actor,
|
|
364
431
|
body: stamped,
|
|
365
|
-
compensationOf: null,
|
|
366
432
|
kind: "committed",
|
|
367
433
|
lineage,
|
|
368
434
|
reason: params.reason ?? null,
|
|
369
435
|
ref: params.ref,
|
|
436
|
+
retired: false,
|
|
437
|
+
seen: prepared,
|
|
370
438
|
tenantId
|
|
371
439
|
});
|
|
372
|
-
await project(tx, {
|
|
373
|
-
at: appended.at,
|
|
374
|
-
body: stamped,
|
|
375
|
-
hash: appended.hash,
|
|
376
|
-
head: headColumnsOf(prepared),
|
|
377
|
-
lineage,
|
|
378
|
-
ref: params.ref,
|
|
379
|
-
retiredAt: null,
|
|
380
|
-
tenantId,
|
|
381
|
-
version: appended.version
|
|
382
|
-
});
|
|
383
440
|
return {
|
|
384
441
|
committed: true,
|
|
385
442
|
hash: appended.hash,
|
|
443
|
+
head: appended.head,
|
|
386
444
|
lineage,
|
|
387
445
|
previous: null,
|
|
388
446
|
ref: params.ref,
|
|
389
447
|
version: appended.version
|
|
390
448
|
};
|
|
391
449
|
});
|
|
392
|
-
const reinstate =
|
|
393
|
-
const tenantId = params[tenantKey];
|
|
394
|
-
const existing = await locked(tx, params);
|
|
450
|
+
const reinstate = flip("reinstated", (existing, ref) => {
|
|
395
451
|
if (existing === undefined || existing.retiredAt === null) {
|
|
396
|
-
throw new import_db.DbRefusal(`"${
|
|
452
|
+
throw new import_db.DbRefusal(`"${ref}" (${name}) is not retired: nothing to reinstate`);
|
|
397
453
|
}
|
|
398
|
-
|
|
399
|
-
actor: params.actor,
|
|
400
|
-
body: existing.body,
|
|
401
|
-
compensationOf: null,
|
|
402
|
-
kind: "reinstated",
|
|
403
|
-
lineage: existing.lineage,
|
|
404
|
-
reason: params.reason ?? null,
|
|
405
|
-
ref: params.ref,
|
|
406
|
-
tenantId
|
|
407
|
-
});
|
|
408
|
-
await project(tx, {
|
|
409
|
-
at: appended.at,
|
|
410
|
-
body: existing.body,
|
|
411
|
-
hash: appended.hash,
|
|
412
|
-
head: storedHead(params.ref, existing.body),
|
|
413
|
-
lineage: existing.lineage,
|
|
414
|
-
ref: params.ref,
|
|
415
|
-
retiredAt: null,
|
|
416
|
-
tenantId,
|
|
417
|
-
version: appended.version
|
|
418
|
-
});
|
|
419
|
-
return {
|
|
420
|
-
hash: appended.hash,
|
|
421
|
-
lineage: existing.lineage,
|
|
422
|
-
ref: params.ref,
|
|
423
|
-
version: appended.version
|
|
424
|
-
};
|
|
454
|
+
return existing;
|
|
425
455
|
});
|
|
426
456
|
const at = seam.scoped(async (tx, params) => {
|
|
427
457
|
const lineage = await currentLineageOf(tx, params);
|
|
@@ -454,7 +484,7 @@ var documentStore = (options) => {
|
|
|
454
484
|
return needsLog().verify(tx, stream(params, lineage));
|
|
455
485
|
});
|
|
456
486
|
const compensate = async (scoped, tenantId, output, kind, body, retired) => {
|
|
457
|
-
|
|
487
|
+
await record(scoped, {
|
|
458
488
|
actor: UNDO_ACTOR,
|
|
459
489
|
body,
|
|
460
490
|
compensationOf: output.version,
|
|
@@ -462,19 +492,10 @@ var documentStore = (options) => {
|
|
|
462
492
|
lineage: output.lineage,
|
|
463
493
|
reason: `undo of v${output.version}`,
|
|
464
494
|
ref: output.ref,
|
|
495
|
+
retired,
|
|
496
|
+
seen: readBody(definition, name, output.ref, body),
|
|
465
497
|
tenantId
|
|
466
498
|
});
|
|
467
|
-
await project(scoped, {
|
|
468
|
-
at: appended.at,
|
|
469
|
-
body,
|
|
470
|
-
hash: appended.hash,
|
|
471
|
-
head: storedHead(output.ref, body),
|
|
472
|
-
lineage: output.lineage,
|
|
473
|
-
ref: output.ref,
|
|
474
|
-
retiredAt: retired ? appended.at : null,
|
|
475
|
-
tenantId,
|
|
476
|
-
version: appended.version
|
|
477
|
-
});
|
|
478
499
|
};
|
|
479
500
|
const lockedForUndo = async (scoped, tenantId, output) => {
|
|
480
501
|
const existing = await locked(scoped, { ...tenanted(tenantId), ref: output.ref });
|
|
@@ -525,11 +546,14 @@ var documentStore = (options) => {
|
|
|
525
546
|
asOf,
|
|
526
547
|
at,
|
|
527
548
|
commit,
|
|
549
|
+
count,
|
|
528
550
|
get: projection.get,
|
|
529
551
|
history,
|
|
552
|
+
keys,
|
|
530
553
|
list,
|
|
531
554
|
listAll: projection.listAll,
|
|
532
555
|
reinstate,
|
|
556
|
+
render,
|
|
533
557
|
retire,
|
|
534
558
|
succeed,
|
|
535
559
|
undos,
|
|
@@ -542,14 +566,12 @@ var documentStore = (options) => {
|
|
|
542
566
|
const rawEntities = Object.fromEntries(built.map(([name, one]) => [name, one.door]));
|
|
543
567
|
const restorers = Object.fromEntries(built.map(([name, one]) => [name, one.restore]));
|
|
544
568
|
const heads = seam.scoped(async (tx, params) => {
|
|
545
|
-
const {
|
|
569
|
+
const { entity, order, where } = params;
|
|
570
|
+
const columns = params.columns === "all" ? ALL_HEAD_COLUMNS : params.columns;
|
|
546
571
|
const rows = await readers.project(tx, {
|
|
547
572
|
...tenanted(params[tenantKey]),
|
|
548
573
|
columns: columns.filter((column) => column !== "number").map(columnOf),
|
|
549
|
-
order: order.map(
|
|
550
|
-
column: columnOf(one.column),
|
|
551
|
-
...one.direction === undefined ? {} : { direction: one.direction }
|
|
552
|
-
})),
|
|
574
|
+
order: order.map(orderOf),
|
|
553
575
|
where: {
|
|
554
576
|
...entity === undefined ? {} : { [entityColumn]: entity },
|
|
555
577
|
...narrowed(where)
|
|
@@ -602,5 +624,9 @@ var documentStore = (options) => {
|
|
|
602
624
|
}
|
|
603
625
|
return { rows };
|
|
604
626
|
});
|
|
605
|
-
|
|
627
|
+
const tables = {
|
|
628
|
+
documents: readers,
|
|
629
|
+
...revisions === undefined ? {} : { revisions }
|
|
630
|
+
};
|
|
631
|
+
return { ...rawEntities, aggregate, heads, list: listEverywhere, rebuild, tables };
|
|
606
632
|
};
|