@ultimat3/testing 31.7.0 → 31.9.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/package.json +22 -22
- package/src/entity-conformance-chain-writes.ts +350 -0
- package/src/entity-conformance-checks.ts +10 -0
- package/src/entity-conformance-matrix-writes.ts +75 -0
- package/src/entity-conformance-matrix.ts +5 -5
- package/src/entity-conformance-page-bytes.ts +57 -0
- package/src/entity-conformance-tenancy.ts +46 -1
- package/src/entity-conformance-unreturned.ts +355 -0
- package/src/entity-conformance-update-many.ts +359 -0
- package/src/entity-conformance-write-entities.ts +71 -0
- package/src/entity-conformance-write-support.ts +40 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// Entity conformance · `updateMany([{ id, patch }])`: each row its own patch in one write, the count
|
|
2
|
+
// of rows it reached, all or nothing — tenant-scoped, soft-deleted rows unreachable, a JS-only rule
|
|
3
|
+
// judged on every row, an uneven batch refused, sealed values sealed, the change reported and one
|
|
4
|
+
// history version per row. Past the bind ceiling it is still one write.
|
|
5
|
+
|
|
6
|
+
import { expect } from 'bun:test';
|
|
7
|
+
import type { Database } from '@ultimat3/entity';
|
|
8
|
+
import { entity, history, text } from '@ultimat3/entity';
|
|
9
|
+
import { remember } from './entity-conformance-entities';
|
|
10
|
+
import { checkOver, codeOf, idAt, inOrg } from './entity-conformance-support';
|
|
11
|
+
import {
|
|
12
|
+
ledger,
|
|
13
|
+
logged,
|
|
14
|
+
loggedHistory,
|
|
15
|
+
notes,
|
|
16
|
+
posts,
|
|
17
|
+
swept,
|
|
18
|
+
vault,
|
|
19
|
+
} from './entity-conformance-write-entities';
|
|
20
|
+
import { accountedFor, heard } from './entity-conformance-write-support';
|
|
21
|
+
import type { EntityDriverCheck } from './entity-driver-conformance';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Six hundred patched columns and a unique: 602 binds a row, so 200 rows are two statements. Wide
|
|
25
|
+
* rather than long: a `defineDriver` store sends one `update` per row, and a long batch is n² there.
|
|
26
|
+
*/
|
|
27
|
+
const BROAD_COLUMNS = Array.from({ length: 600 }, (_, index) => `c${index}`);
|
|
28
|
+
const broad = remember(
|
|
29
|
+
entity('ec_um_broad', {
|
|
30
|
+
columns: {
|
|
31
|
+
id: text({ max: 20 }).primaryKey(),
|
|
32
|
+
u: text({ max: 20 }).unique(),
|
|
33
|
+
...Object.fromEntries(BROAD_COLUMNS.map((name) => [name, text({ max: 20 })])),
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const uniques = remember(
|
|
39
|
+
entity('ec_um_uniques', {
|
|
40
|
+
columns: { id: text({ max: 20 }).primaryKey(), slug: text({ max: 20 }).unique() },
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const [ONE, TWO, THREE] = [idAt('d1'), idAt('d2'), idAt('d3')];
|
|
45
|
+
const ORG = idAt('da');
|
|
46
|
+
const OTHER_ORG = idAt('db');
|
|
47
|
+
const WHEN = new Date('2020-01-02T03:04:05.000Z');
|
|
48
|
+
/** Past 65,535 binds at 602 per row (key, `u`, six hundred columns): no one statement carries it. */
|
|
49
|
+
const PAST_CEILING = 200;
|
|
50
|
+
|
|
51
|
+
const broadRow = (id: string, value: string): Readonly<Record<string, string>> => ({
|
|
52
|
+
id,
|
|
53
|
+
u: id,
|
|
54
|
+
...Object.fromEntries(BROAD_COLUMNS.map((name) => [name, value])),
|
|
55
|
+
});
|
|
56
|
+
const broadPatch = (u: string, value: string): Readonly<Record<string, string>> => ({
|
|
57
|
+
u,
|
|
58
|
+
...Object.fromEntries(BROAD_COLUMNS.map((name) => [name, value])),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const seedNotes = async (db: Database<{ notes: typeof notes }>): Promise<void> => {
|
|
62
|
+
await db.notes.insertAll([
|
|
63
|
+
{ id: ONE, kind: 'a', title: 'one', body: 'b1', updatedAt: WHEN },
|
|
64
|
+
{ id: TWO, kind: 'a', title: 'two', body: 'b2', updatedAt: WHEN },
|
|
65
|
+
{ id: THREE, kind: 'b', title: 'three', body: 'b3', updatedAt: WHEN },
|
|
66
|
+
]);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const shape = async (db: Database<{ notes: typeof notes }>) =>
|
|
70
|
+
(await db.notes.orderBy('id').all()).map((row) => [row.id, row.title, row.body]);
|
|
71
|
+
|
|
72
|
+
const writes: readonly EntityDriverCheck[] = [
|
|
73
|
+
checkOver(
|
|
74
|
+
{ notes },
|
|
75
|
+
'updateMany writes each row its own patch, answers the count, keeps the rest',
|
|
76
|
+
async (db) => {
|
|
77
|
+
await seedNotes(db);
|
|
78
|
+
const written = await db.notes.updateMany([
|
|
79
|
+
{ id: ONE, patch: { title: 'first', body: null } },
|
|
80
|
+
{ id: THREE, patch: { title: 'third', body: 'x' } },
|
|
81
|
+
]);
|
|
82
|
+
expect(written).toBe(2);
|
|
83
|
+
expect(await shape(db)).toEqual([
|
|
84
|
+
[ONE, 'first', null],
|
|
85
|
+
[TWO, 'two', 'b2'],
|
|
86
|
+
[THREE, 'third', 'x'],
|
|
87
|
+
]);
|
|
88
|
+
},
|
|
89
|
+
),
|
|
90
|
+
checkOver(
|
|
91
|
+
{ notes },
|
|
92
|
+
'updateMany counts only rows it reached; an empty batch writes nothing and answers 0',
|
|
93
|
+
async (db) => {
|
|
94
|
+
await seedNotes(db);
|
|
95
|
+
expect(
|
|
96
|
+
await db.notes.updateMany([
|
|
97
|
+
{ id: ONE, patch: { title: 'first' } },
|
|
98
|
+
{ id: idAt('dff'), patch: { title: 'ghost' } },
|
|
99
|
+
]),
|
|
100
|
+
).toBe(1);
|
|
101
|
+
expect(await db.notes.updateMany([])).toBe(0);
|
|
102
|
+
expect(await db.notes.count()).toBe(3);
|
|
103
|
+
expect((await shape(db)).map(([, title]) => title)).toEqual(['first', 'two', 'three']);
|
|
104
|
+
},
|
|
105
|
+
),
|
|
106
|
+
checkOver(
|
|
107
|
+
{ notes },
|
|
108
|
+
'updateMany keeps a column a patch names as undefined, and stamps onUpdateNow',
|
|
109
|
+
async (db) => {
|
|
110
|
+
await seedNotes(db);
|
|
111
|
+
await db.notes.updateMany([
|
|
112
|
+
{ id: ONE, patch: { title: 'first', body: undefined } },
|
|
113
|
+
{ id: TWO, patch: { title: 'second' } },
|
|
114
|
+
]);
|
|
115
|
+
const rows = await db.notes.orderBy('id').all();
|
|
116
|
+
expect(
|
|
117
|
+
rows.map((row) => [row.title, row.body, row.updatedAt.getTime() === WHEN.getTime()]),
|
|
118
|
+
).toEqual([
|
|
119
|
+
['first', 'b1', false],
|
|
120
|
+
['second', 'b2', false],
|
|
121
|
+
['three', 'b3', true],
|
|
122
|
+
]);
|
|
123
|
+
},
|
|
124
|
+
),
|
|
125
|
+
checkOver(
|
|
126
|
+
{ notes },
|
|
127
|
+
'an uneven batch, a repeated id or an empty patch is refused and writes nothing',
|
|
128
|
+
async (db) => {
|
|
129
|
+
await seedNotes(db);
|
|
130
|
+
const uneven = () =>
|
|
131
|
+
db.notes.updateMany([
|
|
132
|
+
{ id: ONE, patch: { title: 'first' } },
|
|
133
|
+
{ id: TWO, patch: { body: 'other' } },
|
|
134
|
+
]);
|
|
135
|
+
expect(await codeOf(uneven)).toBe('X_INVARIANT_VIOLATED');
|
|
136
|
+
const twice = () =>
|
|
137
|
+
db.notes.updateMany([
|
|
138
|
+
{ id: ONE, patch: { title: 'first' } },
|
|
139
|
+
{ id: ONE, patch: { title: 'again' } },
|
|
140
|
+
]);
|
|
141
|
+
expect(await codeOf(twice)).toBe('X_INVARIANT_VIOLATED');
|
|
142
|
+
expect(await codeOf(() => db.notes.updateMany([{ id: ONE, patch: {} }]))).toBe(
|
|
143
|
+
'X_PATCH_EMPTY',
|
|
144
|
+
);
|
|
145
|
+
expect(await shape(db)).toEqual([
|
|
146
|
+
[ONE, 'one', 'b1'],
|
|
147
|
+
[TWO, 'two', 'b2'],
|
|
148
|
+
[THREE, 'three', 'b3'],
|
|
149
|
+
]);
|
|
150
|
+
},
|
|
151
|
+
),
|
|
152
|
+
checkOver(
|
|
153
|
+
{ uniques },
|
|
154
|
+
'two patches taking one unique value are refused; neither row moves',
|
|
155
|
+
async (db) => {
|
|
156
|
+
await db.uniques.insertAll([
|
|
157
|
+
{ id: 'a', slug: 'one' },
|
|
158
|
+
{ id: 'b', slug: 'two' },
|
|
159
|
+
]);
|
|
160
|
+
const clash = () =>
|
|
161
|
+
db.uniques.updateMany([
|
|
162
|
+
{ id: 'a', patch: { slug: 'same' } },
|
|
163
|
+
{ id: 'b', patch: { slug: 'same' } },
|
|
164
|
+
]);
|
|
165
|
+
expect(await codeOf(clash)).toBe('X_DB_UNIQUE_VIOLATION');
|
|
166
|
+
expect((await db.uniques.orderBy('id').all()).map((row) => row.slug)).toEqual(['one', 'two']);
|
|
167
|
+
},
|
|
168
|
+
),
|
|
169
|
+
checkOver(
|
|
170
|
+
{ uniques },
|
|
171
|
+
"a later patch taking an untouched row's unique value refuses the batch; no earlier row moves",
|
|
172
|
+
async (db) => {
|
|
173
|
+
await db.uniques.insertAll([
|
|
174
|
+
{ id: 'a', slug: 'one' },
|
|
175
|
+
{ id: 'b', slug: 'two' },
|
|
176
|
+
{ id: 'c', slug: 'taken' },
|
|
177
|
+
]);
|
|
178
|
+
const clash = () =>
|
|
179
|
+
db.uniques.updateMany([
|
|
180
|
+
{ id: 'a', patch: { slug: 'moved' } },
|
|
181
|
+
{ id: 'b', patch: { slug: 'taken' } },
|
|
182
|
+
]);
|
|
183
|
+
expect(await codeOf(clash)).toBe('X_DB_UNIQUE_VIOLATION');
|
|
184
|
+
expect((await db.uniques.orderBy('id').all()).map((row) => row.slug)).toEqual([
|
|
185
|
+
'one',
|
|
186
|
+
'two',
|
|
187
|
+
'taken',
|
|
188
|
+
]);
|
|
189
|
+
},
|
|
190
|
+
),
|
|
191
|
+
checkOver(
|
|
192
|
+
{ broad },
|
|
193
|
+
'a 200-row, 600-column updateMany past the bind ceiling lands every patch, or none of them',
|
|
194
|
+
async (db) => {
|
|
195
|
+
const ids = Array.from({ length: PAST_CEILING }, (_, index) => `r${index}`);
|
|
196
|
+
await db.broad.insertAll([...ids, 'z'].map((id) => broadRow(id, 'old')) as never[]);
|
|
197
|
+
// The LAST patch takes the stored `z`'s unique value: the refusal lands in the last statement.
|
|
198
|
+
const clash = ids.map((id, index) => ({
|
|
199
|
+
id,
|
|
200
|
+
patch: broadPatch(index === PAST_CEILING - 1 ? 'z' : `n${id}`, 'new'),
|
|
201
|
+
}));
|
|
202
|
+
expect(await codeOf(() => db.broad.updateMany(clash as never[]))).toBe(
|
|
203
|
+
'X_DB_UNIQUE_VIOLATION',
|
|
204
|
+
);
|
|
205
|
+
expect(await db.broad.where({ c0: 'new' } as never).count()).toBe(0);
|
|
206
|
+
const all = ids.map((id) => ({ id, patch: broadPatch(`n${id}`, 'new') }));
|
|
207
|
+
expect(await db.broad.updateMany(all as never[])).toBe(PAST_CEILING);
|
|
208
|
+
const fresh = await db.broad.where({ c599: 'new' } as never).count();
|
|
209
|
+
const last = await db.broad.where({ id: `r${PAST_CEILING - 1}` }).one();
|
|
210
|
+
expect([fresh, (last as Record<string, unknown> | null)?.['u']]).toEqual([
|
|
211
|
+
PAST_CEILING,
|
|
212
|
+
`nr${PAST_CEILING - 1}`,
|
|
213
|
+
]);
|
|
214
|
+
},
|
|
215
|
+
),
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
const held = async (db: Database<{ posts: typeof posts }>) =>
|
|
219
|
+
[
|
|
220
|
+
...(await db.posts.where({ orgId: ORG }).all()),
|
|
221
|
+
...(await db.posts.where({ orgId: OTHER_ORG }).all()),
|
|
222
|
+
]
|
|
223
|
+
.map((row) => `${row.orgId === ORG ? 'ours' : 'theirs'}:${row.title}`)
|
|
224
|
+
.sort();
|
|
225
|
+
|
|
226
|
+
const semantics: readonly EntityDriverCheck[] = [
|
|
227
|
+
checkOver(
|
|
228
|
+
{ posts },
|
|
229
|
+
'updateMany reaches only the actor’s tenant: another tenant’s id is not reached',
|
|
230
|
+
async (db) => {
|
|
231
|
+
await db.posts.insertAll([
|
|
232
|
+
{ id: ONE, orgId: ORG, title: 'same' },
|
|
233
|
+
{ id: TWO, orgId: OTHER_ORG, title: 'same' },
|
|
234
|
+
]);
|
|
235
|
+
const batch = [
|
|
236
|
+
{ id: ONE, patch: { title: 'x' } },
|
|
237
|
+
{ id: TWO, patch: { title: 'x' } },
|
|
238
|
+
];
|
|
239
|
+
expect(await inOrg(ORG, () => db.posts.updateMany(batch))).toBe(1);
|
|
240
|
+
expect(await held(db)).toEqual(['ours:x', 'theirs:same']);
|
|
241
|
+
expect(await codeOf(() => db.posts.updateMany(batch))).toBe('X_TENANCY_UNSCOPED');
|
|
242
|
+
const handOver = () => db.posts.updateMany([{ id: ONE, patch: { orgId: OTHER_ORG } }]);
|
|
243
|
+
expect(await inOrg(ORG, () => codeOf(handOver))).toBe('X_TENANCY_ACTOR_MISMATCH');
|
|
244
|
+
expect(await held(db)).toEqual(['ours:x', 'theirs:same']);
|
|
245
|
+
},
|
|
246
|
+
),
|
|
247
|
+
checkOver(
|
|
248
|
+
{ swept },
|
|
249
|
+
'updateMany cannot reach a soft-deleted row, and a JS-only rule refuses the batch whole',
|
|
250
|
+
async (db, { driver }) => {
|
|
251
|
+
await db.swept.insertAll([
|
|
252
|
+
{ id: ONE, kind: 'k', reason: 'r', deletedAt: null },
|
|
253
|
+
{ id: TWO, kind: 'k', reason: 'r', deletedAt: null },
|
|
254
|
+
{ id: THREE, kind: 'k', reason: 'r', deletedAt: null },
|
|
255
|
+
]);
|
|
256
|
+
await db.swept.delete(THREE);
|
|
257
|
+
// TWO's patch stamps it with no reason: the batch is judged whole, so ONE does not move.
|
|
258
|
+
const judged = () =>
|
|
259
|
+
db.swept.updateMany([
|
|
260
|
+
{ id: ONE, patch: { reason: 'fine', deletedAt: null } },
|
|
261
|
+
{ id: TWO, patch: { reason: null, deletedAt: WHEN } },
|
|
262
|
+
]);
|
|
263
|
+
expect(await codeOf(judged)).toBe('X_INVARIANT_VIOLATED');
|
|
264
|
+
expect((await db.swept.orderBy('id').all()).map((row) => row.reason)).toEqual(['r', 'r']);
|
|
265
|
+
const revive = [
|
|
266
|
+
{ id: ONE, patch: { reason: 'fine', deletedAt: null } },
|
|
267
|
+
{ id: THREE, patch: { reason: 'back', deletedAt: null } },
|
|
268
|
+
];
|
|
269
|
+
expect(await db.swept.updateMany(revive)).toBe(1);
|
|
270
|
+
const { rows } = await driver.repo(swept).findMany({
|
|
271
|
+
includeDeleted: true,
|
|
272
|
+
orderBy: [{ column: 'id', direction: 'asc' }],
|
|
273
|
+
});
|
|
274
|
+
expect(rows.map((row) => [row.reason, row.deletedAt === null])).toEqual([
|
|
275
|
+
['fine', true],
|
|
276
|
+
['r', true],
|
|
277
|
+
['r', false],
|
|
278
|
+
]);
|
|
279
|
+
},
|
|
280
|
+
),
|
|
281
|
+
checkOver({ ledger }, 'an append-only table refuses updateMany', async (db) => {
|
|
282
|
+
await db.ledger.insert({ id: ONE, account: 'cash', note: 'n' });
|
|
283
|
+
expect(await codeOf(() => db.ledger.updateMany([{ id: ONE, patch: { note: 'x' } }]))).toBe(
|
|
284
|
+
'X_ENTITY_APPEND_ONLY',
|
|
285
|
+
);
|
|
286
|
+
expect((await db.ledger.all()).map((row) => row.note)).toEqual(['n']);
|
|
287
|
+
}),
|
|
288
|
+
checkOver({ vault }, 'updateMany seals what it writes and reads back plaintext', async (db) => {
|
|
289
|
+
await db.vault.insertAll([
|
|
290
|
+
{ id: ONE, label: 'a', secret: null, email: 'a@example.com' },
|
|
291
|
+
{ id: TWO, label: 'b', secret: null, email: 'b@example.com' },
|
|
292
|
+
]);
|
|
293
|
+
await db.vault.updateMany([
|
|
294
|
+
{ id: ONE, patch: { secret: 's1', email: 'c@example.com' } },
|
|
295
|
+
{ id: TWO, patch: { secret: 's2', email: 'd@example.com' } },
|
|
296
|
+
]);
|
|
297
|
+
const rows = await db.vault.orderBy('id').all();
|
|
298
|
+
expect(rows.map((row) => [row.secret, row.email])).toEqual([
|
|
299
|
+
['s1', 'c@example.com'],
|
|
300
|
+
['s2', 'd@example.com'],
|
|
301
|
+
]);
|
|
302
|
+
// A lookup column sealed deterministically still finds its row by the new value.
|
|
303
|
+
expect((await db.vault.where({ email: 'd@example.com' }).one())?.id).toBe(TWO);
|
|
304
|
+
}),
|
|
305
|
+
];
|
|
306
|
+
|
|
307
|
+
const reported: readonly EntityDriverCheck[] = [
|
|
308
|
+
checkOver(
|
|
309
|
+
{ notes },
|
|
310
|
+
'updateMany is reported to the row observer for every row it wrote, never silence',
|
|
311
|
+
async (db) => {
|
|
312
|
+
await seedNotes(db);
|
|
313
|
+
const { answer, heard: told } = await heard('ec_wr_notes', () =>
|
|
314
|
+
db.notes.updateMany([
|
|
315
|
+
{ id: ONE, patch: { title: 'x' } },
|
|
316
|
+
{ id: TWO, patch: { title: 'y' } },
|
|
317
|
+
{ id: idAt('dff'), patch: { title: 'ghost' } },
|
|
318
|
+
]),
|
|
319
|
+
);
|
|
320
|
+
expect([answer, accountedFor(told)]).toEqual([2, 2]);
|
|
321
|
+
},
|
|
322
|
+
),
|
|
323
|
+
checkOver(
|
|
324
|
+
{ logged, loggedHistory },
|
|
325
|
+
'updateMany writes one history version per row it reached',
|
|
326
|
+
async (db, raw) => {
|
|
327
|
+
if (!raw.history) return;
|
|
328
|
+
const versions = await inOrg(ORG, async () => {
|
|
329
|
+
await db.logged.insertAll([
|
|
330
|
+
{ id: ONE, orgId: ORG, title: 'a' },
|
|
331
|
+
{ id: TWO, orgId: ORG, title: 'b' },
|
|
332
|
+
]);
|
|
333
|
+
await db.logged.updateMany([
|
|
334
|
+
{ id: ONE, patch: { title: 'a2' } },
|
|
335
|
+
{ id: TWO, patch: { title: 'b2' } },
|
|
336
|
+
]);
|
|
337
|
+
return [await history(db.loggedHistory, ONE), await history(db.loggedHistory, TWO)];
|
|
338
|
+
});
|
|
339
|
+
const title = (image: unknown): string | null =>
|
|
340
|
+
(image as { title?: string } | null)?.title ?? null;
|
|
341
|
+
expect(versions.map((all) => all.map((v) => [v.version, v.op, title(v.after)]))).toEqual([
|
|
342
|
+
[
|
|
343
|
+
[1, 'insert', 'a'],
|
|
344
|
+
[2, 'update', 'a2'],
|
|
345
|
+
],
|
|
346
|
+
[
|
|
347
|
+
[1, 'insert', 'b'],
|
|
348
|
+
[2, 'update', 'b2'],
|
|
349
|
+
],
|
|
350
|
+
]);
|
|
351
|
+
},
|
|
352
|
+
),
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
export const UPDATE_MANY_CHECKS: readonly EntityDriverCheck[] = [
|
|
356
|
+
...writes,
|
|
357
|
+
...semantics,
|
|
358
|
+
...reported,
|
|
359
|
+
];
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The tables the many-row write checks share (chain writes, `updateMany`, the unreturned and COPY
|
|
2
|
+
// writes): one per concern — plain, tenant-scoped, soft-deleting under a JS-only rule, append-only,
|
|
3
|
+
// sealed, history-keeping. Each check re-creates the tables it names, so sharing them is free.
|
|
4
|
+
|
|
5
|
+
import { expect } from 'bun:test';
|
|
6
|
+
import { entity, invariant, text, timestamp, uuid } from '@ultimat3/entity';
|
|
7
|
+
import { remember } from './entity-conformance-entities';
|
|
8
|
+
|
|
9
|
+
export const notes = remember(
|
|
10
|
+
entity('ec_wr_notes', {
|
|
11
|
+
columns: {
|
|
12
|
+
id: uuid().primaryKey(),
|
|
13
|
+
kind: text({ max: 20 }),
|
|
14
|
+
title: text({ max: 40 }),
|
|
15
|
+
body: text({ max: 40 }).nullable(),
|
|
16
|
+
updatedAt: timestamp().defaultNow().onUpdateNow(),
|
|
17
|
+
},
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export const posts = remember(
|
|
22
|
+
entity('ec_wr_posts', {
|
|
23
|
+
columns: { id: uuid().primaryKey(), orgId: uuid().tenant(), title: text({ max: 40 }) },
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const stampHasReason = (deletedAt: unknown, reason: unknown): boolean =>
|
|
28
|
+
deletedAt === null || reason !== null;
|
|
29
|
+
|
|
30
|
+
/** Soft-deleting, with a rule no CHECK can state: a stamp needs a reason. */
|
|
31
|
+
export const swept = remember(
|
|
32
|
+
entity('ec_wr_swept', {
|
|
33
|
+
columns: {
|
|
34
|
+
id: uuid().primaryKey(),
|
|
35
|
+
kind: text({ max: 20 }),
|
|
36
|
+
reason: text({ max: 40 }).nullable(),
|
|
37
|
+
deletedAt: timestamp().nullable(),
|
|
38
|
+
},
|
|
39
|
+
invariants: (c) => [
|
|
40
|
+
invariant('ec_wr_stamp_has_reason', c.satisfies(stampHasReason, ['deletedAt', 'reason'])),
|
|
41
|
+
],
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
export const ledger = remember(
|
|
46
|
+
entity('ec_wr_ledger', {
|
|
47
|
+
columns: { id: uuid().primaryKey(), account: text({ max: 20 }), note: text({ max: 20 }) },
|
|
48
|
+
appendOnly: true,
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
export const vault = remember(
|
|
53
|
+
entity('ec_wr_vault', {
|
|
54
|
+
columns: {
|
|
55
|
+
id: uuid().primaryKey(),
|
|
56
|
+
label: text({ max: 20 }),
|
|
57
|
+
secret: text({ max: 40 }).nullable().sealed(),
|
|
58
|
+
email: text().sealed({ lookup: true }),
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
export const logged = remember(
|
|
64
|
+
entity('ec_wr_logged', {
|
|
65
|
+
columns: { id: uuid().primaryKey(), orgId: uuid().tenant(), title: text({ max: 40 }) },
|
|
66
|
+
history: true,
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
export const loggedHistory = remember(
|
|
70
|
+
logged.$history ?? expect.unreachable('history: true declares a history entity'),
|
|
71
|
+
);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// What the many-row write checks share: the row observer heard through its two bus events, counted
|
|
2
|
+
// as one figure — so a write path that reports its rows as `entity.row`, as `entity.bulk` or as a
|
|
3
|
+
// mix is held to the one claim that matters, that none of them went unreported.
|
|
4
|
+
|
|
5
|
+
import type { EntityBulkEvent, EntityRowEvent } from '@ultimat3/core';
|
|
6
|
+
import { subscribe } from '@ultimat3/core';
|
|
7
|
+
|
|
8
|
+
export interface Heard {
|
|
9
|
+
readonly rows: readonly EntityRowEvent[];
|
|
10
|
+
readonly bulk: readonly EntityBulkEvent[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `work`'s answer and every change the observer reported for `entity` while it ran. The two
|
|
15
|
+
* subscriptions are this call's own and are removed whatever `work` does — never a reset of the
|
|
16
|
+
* process-wide bus, which another suite in the process may be listening on.
|
|
17
|
+
*/
|
|
18
|
+
export const heard = async <T>(
|
|
19
|
+
entity: string,
|
|
20
|
+
work: () => Promise<T>,
|
|
21
|
+
): Promise<{ readonly answer: T; readonly heard: Heard }> => {
|
|
22
|
+
const rows: EntityRowEvent[] = [];
|
|
23
|
+
const bulk: EntityBulkEvent[] = [];
|
|
24
|
+
const offRow = subscribe('entity.row', (event) => {
|
|
25
|
+
if (event.payload.entity === entity) rows.push(event.payload);
|
|
26
|
+
});
|
|
27
|
+
const offBulk = subscribe('entity.bulk', (event) => {
|
|
28
|
+
if (event.payload.entity === entity) bulk.push(event.payload);
|
|
29
|
+
});
|
|
30
|
+
try {
|
|
31
|
+
return { answer: await work(), heard: { rows, bulk } };
|
|
32
|
+
} finally {
|
|
33
|
+
offRow();
|
|
34
|
+
offBulk();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** How many written rows the observer accounted for: one per row event, `rows` per bulk event. */
|
|
39
|
+
export const accountedFor = ({ rows, bulk }: Heard): number =>
|
|
40
|
+
rows.length + bulk.reduce((sum, event) => sum + event.rows, 0);
|