@afora/core 0.0.2 → 0.0.3

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.
Files changed (79) hide show
  1. package/README.md +220 -733
  2. package/dist/core/application/application.d.ts +9 -1
  3. package/dist/core/application/exceptions.d.ts +28 -0
  4. package/dist/core/application/registry.d.ts +33 -0
  5. package/dist/core/application/start.d.ts +51 -0
  6. package/dist/core/application/types/application.interface.d.ts +186 -7
  7. package/dist/core/blueprint/types/blueprint.interface.d.ts +66 -11
  8. package/dist/core/blueprint/types/slice.interface.d.ts +7 -2
  9. package/dist/core/command/command.d.ts +25 -0
  10. package/dist/core/command/exceptions.d.ts +33 -0
  11. package/dist/core/command/types/command.interface.d.ts +102 -0
  12. package/dist/core/environment/environment.d.ts +4 -1
  13. package/dist/core/exception/exception.d.ts +4 -0
  14. package/dist/core/exception/exported.d.ts +4 -0
  15. package/dist/core/json.d.ts +10 -0
  16. package/dist/core/output/exceptions.d.ts +8 -0
  17. package/dist/core/output/openapi-sink.d.ts +83 -0
  18. package/dist/core/output/output.d.ts +19 -0
  19. package/dist/core/output/problem.d.ts +15 -0
  20. package/dist/core/output/select.d.ts +14 -0
  21. package/dist/core/output/stage.d.ts +24 -0
  22. package/dist/core/output/status.d.ts +17 -0
  23. package/dist/core/output/types/output.interface.d.ts +171 -0
  24. package/dist/core/property/abstract-property.d.ts +8 -7
  25. package/dist/core/property/define-property.d.ts +8 -7
  26. package/dist/core/property/property.d.ts +26 -15
  27. package/dist/core/property/types/abstract-property-constructor.type.d.ts +8 -7
  28. package/dist/core/property/types/define-property.interface.d.ts +5 -4
  29. package/dist/core/property/types/optional-property.type.d.ts +5 -5
  30. package/dist/core/property/types/property-collection.type.d.ts +2 -2
  31. package/dist/core/property/types/property-constructor.type.d.ts +5 -1
  32. package/dist/core/property/types/property-options.interface.d.ts +17 -8
  33. package/dist/core/property/types/property-type.interface.d.ts +7 -3
  34. package/dist/core/property/types/property.interface.d.ts +5 -4
  35. package/dist/core/record/abstract-record.d.ts +12 -4
  36. package/dist/core/record/types/plugin.interface.d.ts +13 -3
  37. package/dist/core/record/types/record.interface.d.ts +20 -8
  38. package/dist/core/repository/exceptions.d.ts +4 -0
  39. package/dist/core/repository/in-memory.d.ts +3 -0
  40. package/dist/core/repository/provider.d.ts +6 -0
  41. package/dist/core/repository/query.d.ts +9 -0
  42. package/dist/core/repository/references.d.ts +4 -2
  43. package/dist/core/repository/repository.d.ts +4 -2
  44. package/dist/core/repository/state.d.ts +4 -0
  45. package/dist/core/repository/types/query.interface.d.ts +67 -0
  46. package/dist/core/repository/types/repository.interface.d.ts +13 -13
  47. package/dist/core/schema/types/schema.interface.d.ts +11 -4
  48. package/dist/core/symbols/meta.d.ts +2 -0
  49. package/dist/core/use-case/exceptions.d.ts +10 -0
  50. package/dist/core/use-case/scope.d.ts +21 -0
  51. package/dist/core/use-case/steps/describe.d.ts +13 -0
  52. package/dist/core/use-case/steps/references.d.ts +27 -0
  53. package/dist/core/use-case/steps/run.d.ts +20 -0
  54. package/dist/core/use-case/steps/types/steps.interface.d.ts +153 -0
  55. package/dist/core/use-case/steps/validate.d.ts +17 -0
  56. package/dist/core/use-case/use-case.d.ts +74 -9
  57. package/dist/experimental.d.ts +6 -0
  58. package/dist/experimental.js +123 -0
  59. package/dist/index-hn82hm31.js +1651 -0
  60. package/dist/index-sw2b9ehx.js +5019 -0
  61. package/dist/index.d.ts +30 -11
  62. package/dist/index.js +325 -4142
  63. package/dist/std/blueprint-plugin.d.ts +10 -10
  64. package/dist/std/blueprint.d.ts +10 -10
  65. package/dist/std/entity.d.ts +2 -2
  66. package/dist/std/properties/contact.d.ts +4 -4
  67. package/dist/std/properties/enum.d.ts +1 -1
  68. package/dist/std/properties/headless.d.ts +1 -1
  69. package/dist/std/properties/identity.d.ts +2 -2
  70. package/dist/std/properties/password.d.ts +1 -1
  71. package/dist/std/properties/primitives.d.ts +5 -5
  72. package/dist/std/properties/time.d.ts +2 -2
  73. package/dist/std/record-plugins.d.ts +12 -12
  74. package/dist/std/record.d.ts +1 -1
  75. package/dist/std/stages.d.ts +2 -0
  76. package/dist/testing/index.d.ts +2 -0
  77. package/dist/testing/index.js +502 -0
  78. package/dist/testing/provider-contract.d.ts +22 -0
  79. package/package.json +28 -8
@@ -0,0 +1,502 @@
1
+ import"../index-hn82hm31.js";
2
+ import {
3
+ RepositoryUniqueViolation2,
4
+ RepositoryEntityNotFound2,
5
+ blueprintOf2,
6
+ entityOf2
7
+ } from "../index-sw2b9ehx.js";
8
+
9
+ // src/testing/provider-contract.ts
10
+ function contextOf(name = "Accounts") {
11
+ class Account extends entityOf2(blueprintOf2((ctx) => ({ email: ctx.string, name: ctx.string }))) {
12
+ }
13
+ return {
14
+ entity: Account,
15
+ name,
16
+ unique: ["id", "email"],
17
+ env: {}
18
+ };
19
+ }
20
+ var sequence = 0;
21
+ function row(overrides = {}) {
22
+ sequence += 1;
23
+ const id = `id-${String(sequence).padStart(4, "0")}`;
24
+ return { id, email: `${id}@example.com`, name: id, ...overrides };
25
+ }
26
+ async function failure(promise) {
27
+ try {
28
+ await promise;
29
+ } catch (error) {
30
+ return error;
31
+ }
32
+ return;
33
+ }
34
+ var ids = (rows) => rows.map((r) => r.id);
35
+ function providerContract(runner, makeProvider, options = {}) {
36
+ const { describe, it, expect } = runner;
37
+ async function fixture() {
38
+ const provider = await makeProvider();
39
+ const context = contextOf();
40
+ const seed = async (...rows) => {
41
+ for (const data of rows)
42
+ await provider.insert(context, data);
43
+ };
44
+ return { provider, context, seed };
45
+ }
46
+ describe("provider contract", () => {
47
+ describe("writes and reads", () => {
48
+ it("inserts and finds by id, empty provider counts zero", async () => {
49
+ const { provider, context } = await fixture();
50
+ expect(await provider.count(context)).toBe(0);
51
+ expect(await provider.findOne(context)).toBe(undefined);
52
+ expect(await provider.findMany(context)).toEqual([]);
53
+ const data = row({ age: 30 });
54
+ await provider.insert(context, data);
55
+ expect(await provider.count(context)).toBe(1);
56
+ expect(await provider.findOne(context, { where: { id: data.id } })).toEqual(data);
57
+ expect(await provider.findMany(context)).toEqual([data]);
58
+ });
59
+ it("findOne is undefined when nothing matches", async () => {
60
+ const { provider, context, seed } = await fixture();
61
+ await seed(row());
62
+ expect(await provider.findOne(context, { where: { id: "missing" } })).toBe(undefined);
63
+ expect(await provider.findMany(context, { where: { id: "missing" } })).toEqual([]);
64
+ });
65
+ it("update replaces the stored value", async () => {
66
+ const { provider, context, seed } = await fixture();
67
+ const data = row({ name: "Ana" });
68
+ await seed(data);
69
+ await provider.update(context, { ...data, name: "Bia" });
70
+ expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Bia" });
71
+ expect(await provider.count(context)).toBe(1);
72
+ });
73
+ it("remove deletes the row", async () => {
74
+ const { provider, context, seed } = await fixture();
75
+ const [a, b] = [row(), row()];
76
+ await seed(a, b);
77
+ await provider.remove(context, a.id);
78
+ expect(ids(await provider.findMany(context))).toEqual([b.id]);
79
+ expect(await provider.count(context)).toBe(1);
80
+ });
81
+ it("update and remove of an unknown id throw RepositoryEntityNotFound", async () => {
82
+ const { provider, context, seed } = await fixture();
83
+ await seed(row());
84
+ const updated = await failure(provider.update(context, row()));
85
+ expect(updated instanceof RepositoryEntityNotFound2).toBe(true);
86
+ const removed = await failure(provider.remove(context, "missing"));
87
+ expect(removed instanceof RepositoryEntityNotFound2).toBe(true);
88
+ expect(await provider.count(context)).toBe(1);
89
+ });
90
+ it("keeps tables of different contexts apart", async () => {
91
+ const provider = await makeProvider();
92
+ const [one, two] = [contextOf("One"), contextOf("Two")];
93
+ await provider.insert(one, row());
94
+ expect(await provider.count(one)).toBe(1);
95
+ expect(await provider.count(two)).toBe(0);
96
+ });
97
+ });
98
+ describe("unique keys", () => {
99
+ it("insert throws RepositoryUniqueViolation for a repeated id", async () => {
100
+ const { provider, context, seed } = await fixture();
101
+ const data = row();
102
+ await seed(data);
103
+ const error = await failure(provider.insert(context, { ...row(), id: data.id }));
104
+ expect(error instanceof RepositoryUniqueViolation2).toBe(true);
105
+ expect(error.details.key).toBe("id");
106
+ expect(await provider.count(context)).toBe(1);
107
+ });
108
+ it("insert throws for a repeated unique key", async () => {
109
+ const { provider, context, seed } = await fixture();
110
+ await seed(row({ email: "a@example.com" }));
111
+ const error = await failure(provider.insert(context, row({ email: "a@example.com" })));
112
+ expect(error instanceof RepositoryUniqueViolation2).toBe(true);
113
+ expect(error.details.key).toBe("email");
114
+ expect(await provider.count(context)).toBe(1);
115
+ });
116
+ it("update throws when taking the unique value of another row", async () => {
117
+ const { provider, context, seed } = await fixture();
118
+ const [a, b] = [row({ email: "a@example.com" }), row()];
119
+ await seed(a, b);
120
+ const error = await failure(provider.update(context, { ...b, email: "a@example.com" }));
121
+ expect(error instanceof RepositoryUniqueViolation2).toBe(true);
122
+ expect(error.details.key).toBe("email");
123
+ expect(await provider.findOne(context, { where: { id: b.id } })).toEqual(b);
124
+ });
125
+ it("update may keep the unique values of its own row", async () => {
126
+ const { provider, context, seed } = await fixture();
127
+ const data = row({ email: "a@example.com" });
128
+ await seed(data);
129
+ await provider.update(context, { ...data, name: "Other" });
130
+ expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Other" });
131
+ });
132
+ it("soft-deleted rows keep occupying unique values", async () => {
133
+ const { provider, context, seed } = await fixture();
134
+ await seed(row({
135
+ email: "a@example.com",
136
+ deletedAt: "2024-01-01T00:00:00.000Z"
137
+ }));
138
+ const inserted = await failure(provider.insert(context, row({ email: "a@example.com" })));
139
+ expect(inserted instanceof RepositoryUniqueViolation2).toBe(true);
140
+ const other = row();
141
+ await seed(other);
142
+ const updated = await failure(provider.update(context, { ...other, email: "a@example.com" }));
143
+ expect(updated instanceof RepositoryUniqueViolation2).toBe(true);
144
+ });
145
+ });
146
+ describe("where", () => {
147
+ it("matches scalars by equality", async () => {
148
+ const { provider, context, seed } = await fixture();
149
+ const [a, b, c] = [
150
+ row({ age: 20, name: "Ana", active: true }),
151
+ row({ age: 30, name: "Bia", active: false }),
152
+ row({ age: 30, name: "Cy", active: true })
153
+ ];
154
+ await seed(a, b, c);
155
+ const find = (where) => provider.findMany(context, { where });
156
+ expect(ids(await find({ age: 30 }))).toEqual([b.id, c.id]);
157
+ expect(ids(await find({ name: "Ana" }))).toEqual([a.id]);
158
+ expect(ids(await find({ active: false }))).toEqual([b.id]);
159
+ expect(ids(await find({ age: 31 }))).toEqual([]);
160
+ expect(ids(await find({}))).toEqual([a.id, b.id, c.id]);
161
+ });
162
+ it("matches structured values by structure", async () => {
163
+ const { provider, context, seed } = await fixture();
164
+ const [a, b] = [
165
+ row({ address: { city: "Rio", zip: "1" }, tags: ["x", "y"] }),
166
+ row({ address: { city: "Rio", zip: "2" }, tags: ["y", "x"] })
167
+ ];
168
+ await seed(a, b);
169
+ const find = (where) => provider.findMany(context, { where });
170
+ expect(ids(await find({ address: { city: "Rio", zip: "1" } }))).toEqual([a.id]);
171
+ expect(ids(await find({ address: { city: "Rio" } }))).toEqual([]);
172
+ expect(ids(await find({ tags: ["y", "x"] }))).toEqual([b.id]);
173
+ expect(ids(await find({ tags: ["x"] }))).toEqual([]);
174
+ });
175
+ it("matches membership with in", async () => {
176
+ const { provider, context, seed } = await fixture();
177
+ const [a, b, c] = [row({ age: 1 }), row({ age: 2 }), row({ age: 3 })];
178
+ await seed(a, b, c);
179
+ const find = (where) => provider.findMany(context, { where });
180
+ expect(ids(await find({ age: { in: [1, 3] } }))).toEqual([a.id, c.id]);
181
+ expect(ids(await find({ id: { in: [b.id, "missing"] } }))).toEqual([
182
+ b.id
183
+ ]);
184
+ expect(ids(await find({ age: { in: [] } }))).toEqual([]);
185
+ expect(await provider.count(context, { where: { age: { in: [] } } })).toBe(0);
186
+ expect(await provider.findOne(context, { where: { age: { in: [] } } })).toBe(undefined);
187
+ });
188
+ it("ANDs conditions on several keys", async () => {
189
+ const { provider, context, seed } = await fixture();
190
+ const [a, b, c] = [
191
+ row({ age: 30, name: "Ana" }),
192
+ row({ age: 30, name: "Bia" }),
193
+ row({ age: 20, name: "Bia" })
194
+ ];
195
+ await seed(a, b, c);
196
+ const find = (where) => provider.findMany(context, { where });
197
+ expect(ids(await find({ age: 30, name: "Bia" }))).toEqual([b.id]);
198
+ expect(ids(await find({ age: { in: [20, 30] }, name: { in: ["Bia"] } }))).toEqual([b.id, c.id]);
199
+ expect(ids(await find({ age: 20, name: "Ana" }))).toEqual([]);
200
+ });
201
+ });
202
+ describe("orderBy", () => {
203
+ const order = async (rows, orderBy) => {
204
+ const { provider, context, seed } = await fixture();
205
+ await seed(...rows);
206
+ return ids(await provider.findMany(context, { orderBy }));
207
+ };
208
+ it("is ascending by default and descending on request", async () => {
209
+ const [a, b, c] = [row({ age: 2 }), row({ age: 3 }), row({ age: 1 })];
210
+ expect(await order([a, b, c], [{ key: "age" }])).toEqual([
211
+ c.id,
212
+ a.id,
213
+ b.id
214
+ ]);
215
+ expect(await order([a, b, c], [{ key: "age", direction: "asc" }])).toEqual([c.id, a.id, b.id]);
216
+ expect(await order([a, b, c], [{ key: "age", direction: "desc" }])).toEqual([b.id, a.id, c.id]);
217
+ });
218
+ it("sorts numbers numerically", async () => {
219
+ const [a, b, c] = [
220
+ row({ age: 10 }),
221
+ row({ age: 9 }),
222
+ row({ age: 100 })
223
+ ];
224
+ expect(await order([a, b, c], [{ key: "age" }])).toEqual([
225
+ b.id,
226
+ a.id,
227
+ c.id
228
+ ]);
229
+ });
230
+ it("sorts strings by code point", async () => {
231
+ const [lower, upper, wide, emoji] = [
232
+ row({ name: "a" }),
233
+ row({ name: "Z" }),
234
+ row({ name: "z" }),
235
+ row({ name: "\uD83D\uDE00" })
236
+ ];
237
+ expect(await order([emoji, lower, wide, upper], [{ key: "name" }])).toEqual([upper.id, lower.id, wide.id, emoji.id]);
238
+ });
239
+ it("puts missing values first when ascending and last when descending", async () => {
240
+ const [a, b, c] = [row({ age: 2 }), row(), row({ age: 1 })];
241
+ expect(await order([a, b, c], [{ key: "age" }])).toEqual([
242
+ b.id,
243
+ c.id,
244
+ a.id
245
+ ]);
246
+ expect(await order([a, b, c], [{ key: "age", direction: "desc" }])).toEqual([a.id, c.id, b.id]);
247
+ });
248
+ it("keeps the insertion order of ties", async () => {
249
+ const [a, b, c, d] = [
250
+ row({ group: 1 }),
251
+ row({ group: 0 }),
252
+ row({ group: 1 }),
253
+ row({ group: 0 })
254
+ ];
255
+ expect(await order([a, b, c, d], [{ key: "group" }])).toEqual([
256
+ b.id,
257
+ d.id,
258
+ a.id,
259
+ c.id
260
+ ]);
261
+ expect(await order([a, b, c, d], [{ key: "group", direction: "desc" }])).toEqual([a.id, c.id, b.id, d.id]);
262
+ });
263
+ it("sorts by several keys in turn", async () => {
264
+ const [a, b, c, d] = [
265
+ row({ group: 1, name: "b" }),
266
+ row({ group: 0, name: "z" }),
267
+ row({ group: 1, name: "a" }),
268
+ row({ group: 0, name: "y" })
269
+ ];
270
+ expect(await order([a, b, c, d], [{ key: "group" }, { key: "name", direction: "desc" }])).toEqual([b.id, d.id, a.id, c.id]);
271
+ expect(await order([a, b, c, d], [{ key: "group", direction: "desc" }, { key: "name" }])).toEqual([c.id, a.id, d.id, b.id]);
272
+ });
273
+ });
274
+ describe("limit and offset", () => {
275
+ const seeded = async () => {
276
+ const f = await fixture();
277
+ const rows = [1, 2, 3, 4, 5].map((n) => row({ n }));
278
+ await f.seed(...rows);
279
+ return { ...f, rows };
280
+ };
281
+ const byN = [{ key: "n" }];
282
+ it("limit takes the first rows, offset skips rows before it", async () => {
283
+ const { provider, context, rows } = await seeded();
284
+ const find = (limit, offset) => provider.findMany(context, { orderBy: byN, limit, offset }).then(ids);
285
+ expect(await find(2)).toEqual([rows[0]?.id, rows[1]?.id]);
286
+ expect(await find(undefined, 3)).toEqual([rows[3]?.id, rows[4]?.id]);
287
+ expect(await find(2, 1)).toEqual([rows[1]?.id, rows[2]?.id]);
288
+ expect(await find(10, 4)).toEqual([rows[4]?.id]);
289
+ expect(await find(2, 5)).toEqual([]);
290
+ });
291
+ it("limit 0 returns nothing", async () => {
292
+ const { provider, context } = await seeded();
293
+ expect(await provider.findMany(context, { limit: 0 })).toEqual([]);
294
+ expect(await provider.findOne(context, { limit: 0 })).toBe(undefined);
295
+ });
296
+ it("findOne returns the first row after the offset", async () => {
297
+ const { provider, context, rows } = await seeded();
298
+ expect((await provider.findOne(context, { orderBy: byN, offset: 2 }))?.id).toBe(rows[2]?.id);
299
+ expect(await provider.findOne(context, { orderBy: byN, offset: 5 })).toBe(undefined);
300
+ });
301
+ it("count ignores order, limit and offset", async () => {
302
+ const { provider, context } = await seeded();
303
+ expect(await provider.count(context, {
304
+ orderBy: [{ key: "n", direction: "desc" }],
305
+ limit: 1,
306
+ offset: 3
307
+ })).toBe(5);
308
+ expect(await provider.count(context, {
309
+ where: { n: { in: [1, 2] } },
310
+ limit: 0,
311
+ offset: 9
312
+ })).toBe(2);
313
+ });
314
+ });
315
+ describe("withDeleted", () => {
316
+ const deletedAt = "2024-01-01T00:00:00.000Z";
317
+ const seeded = async () => {
318
+ const f = await fixture();
319
+ const [live, gone] = [row({ n: 1 }), row({ n: 2, deletedAt })];
320
+ await f.seed(live, gone);
321
+ return { ...f, live, gone };
322
+ };
323
+ it("excludes deleted rows by default", async () => {
324
+ const { provider, context, live, gone } = await seeded();
325
+ expect(ids(await provider.findMany(context))).toEqual([live.id]);
326
+ expect(ids(await provider.findMany(context, { withDeleted: false }))).toEqual([live.id]);
327
+ expect(await provider.count(context)).toBe(1);
328
+ expect(await provider.findOne(context, { where: { id: gone.id } })).toBe(undefined);
329
+ });
330
+ it("includes them with withDeleted", async () => {
331
+ const { provider, context, live, gone } = await seeded();
332
+ const query = { withDeleted: true };
333
+ expect(ids(await provider.findMany(context, query))).toEqual([
334
+ live.id,
335
+ gone.id
336
+ ]);
337
+ expect(await provider.count(context, query)).toBe(2);
338
+ expect(await provider.findOne(context, {
339
+ where: { id: gone.id },
340
+ withDeleted: true
341
+ })).toEqual(gone);
342
+ });
343
+ });
344
+ describe("copies", () => {
345
+ it("does not share data with the caller on write", async () => {
346
+ const { provider, context } = await fixture();
347
+ const data = row({ address: { city: "Rio" }, tags: ["x"] });
348
+ await provider.insert(context, data);
349
+ data.address.city = "SP";
350
+ data.tags.push("y");
351
+ const stored = await provider.findOne(context, {
352
+ where: { id: data.id }
353
+ });
354
+ expect(stored?.address).toEqual({ city: "Rio" });
355
+ expect(stored?.tags).toEqual(["x"]);
356
+ });
357
+ it("does not share data with the caller on read", async () => {
358
+ const { provider, context, seed } = await fixture();
359
+ const data = row({ address: { city: "Rio" }, tags: ["x"] });
360
+ await seed(data);
361
+ const one = await provider.findOne(context, {
362
+ where: { id: data.id }
363
+ });
364
+ one.address.city = "SP";
365
+ one.name = "changed";
366
+ const many = await provider.findMany(context);
367
+ many[0].tags.push("y");
368
+ const stored = await provider.findOne(context, {
369
+ where: { id: data.id }
370
+ });
371
+ expect(stored).toEqual(data);
372
+ });
373
+ });
374
+ const withTransactions = async () => {
375
+ const enabled = options.transactions ?? (await makeProvider()).capabilities?.transactions;
376
+ return enabled === true;
377
+ };
378
+ describe("transactions", () => {
379
+ const transactional = (name, body) => it(name, async () => {
380
+ if (!await withTransactions())
381
+ return;
382
+ await body();
383
+ });
384
+ const open = async () => {
385
+ const f = await fixture();
386
+ const transaction = f.provider.transaction?.bind(f.provider);
387
+ if (!transaction) {
388
+ throw new Error("the provider advertises transactions but has none");
389
+ }
390
+ return { ...f, transaction };
391
+ };
392
+ transactional("commits the writes when the callback resolves", async () => {
393
+ const { provider, context, seed, transaction } = await open();
394
+ const [a, b] = [row({ name: "a" }), row()];
395
+ await seed(a);
396
+ const result = await transaction(async (tx) => {
397
+ await tx.insert(context, b);
398
+ await tx.update(context, { ...a, name: "changed" });
399
+ expect(await tx.count(context)).toBe(2);
400
+ return "done";
401
+ });
402
+ expect(result).toBe("done");
403
+ expect(ids(await provider.findMany(context))).toEqual([a.id, b.id]);
404
+ expect((await provider.findOne(context, { where: { id: a.id } }))?.name).toBe("changed");
405
+ });
406
+ transactional("rolls back every write when the callback rejects", async () => {
407
+ const { provider, context, seed, transaction } = await open();
408
+ const [a, b] = [row({ name: "a" }), row()];
409
+ await seed(a, b);
410
+ const boom = new Error("boom");
411
+ const error = await failure(transaction(async (tx) => {
412
+ await tx.insert(context, row());
413
+ await tx.update(context, { ...a, name: "changed" });
414
+ await tx.remove(context, b.id);
415
+ throw boom;
416
+ }));
417
+ expect(error).toBe(boom);
418
+ expect(await provider.findMany(context)).toEqual([a, b]);
419
+ });
420
+ transactional("rolls back after a failed write inside", async () => {
421
+ const { provider, context, seed, transaction } = await open();
422
+ const a = row({ email: "a@example.com" });
423
+ await seed(a);
424
+ const error = await failure(transaction(async (tx) => {
425
+ await tx.insert(context, row());
426
+ await tx.insert(context, row({ email: "a@example.com" }));
427
+ }));
428
+ expect(error instanceof RepositoryUniqueViolation2).toBe(true);
429
+ expect(await provider.findMany(context)).toEqual([a]);
430
+ });
431
+ transactional("does not lose writes made outside meanwhile", async () => {
432
+ const { provider, context, transaction } = await open();
433
+ const [inside, outside] = [row(), row()];
434
+ let release = () => {};
435
+ const gate = new Promise((resolve) => {
436
+ release = resolve;
437
+ });
438
+ let started = () => {};
439
+ const entered = new Promise((resolve) => {
440
+ started = resolve;
441
+ });
442
+ const running = failure(transaction(async (tx) => {
443
+ await tx.insert(context, inside);
444
+ started();
445
+ await gate;
446
+ throw new Error("rollback");
447
+ }));
448
+ await entered;
449
+ const writing = provider.insert(context, outside);
450
+ release();
451
+ await running;
452
+ await writing;
453
+ expect(ids(await provider.findMany(context))).toEqual([outside.id]);
454
+ });
455
+ transactional("rejects the scoped provider after the callback", async () => {
456
+ const { context, transaction } = await open();
457
+ let scoped;
458
+ await transaction(async (tx) => {
459
+ scoped = tx;
460
+ });
461
+ const error = await failure(scoped.insert(context, row()));
462
+ expect(error instanceof Error).toBe(true);
463
+ const read = await failure(scoped.count(context));
464
+ expect(read instanceof Error).toBe(true);
465
+ });
466
+ transactional("treats a nested transaction as a savepoint", async () => {
467
+ const { provider, context, transaction } = await open();
468
+ const [outer, kept, dropped] = [row(), row(), row()];
469
+ await transaction(async (tx) => {
470
+ expect(typeof tx.transaction).toBe("function");
471
+ await tx.insert(context, outer);
472
+ await tx.transaction?.(async (inner) => {
473
+ await inner.insert(context, kept);
474
+ });
475
+ const error = await failure(tx.transaction?.(async (inner) => {
476
+ await inner.insert(context, dropped);
477
+ throw new Error("inner");
478
+ }) ?? Promise.resolve());
479
+ expect(error.message).toBe("inner");
480
+ expect(ids(await tx.findMany(context))).toEqual([outer.id, kept.id]);
481
+ });
482
+ expect(ids(await provider.findMany(context))).toEqual([
483
+ outer.id,
484
+ kept.id
485
+ ]);
486
+ });
487
+ transactional("rolls the outer back even if an inner one committed", async () => {
488
+ const { provider, context, transaction } = await open();
489
+ await failure(transaction(async (tx) => {
490
+ await tx.transaction?.(async (inner) => {
491
+ await inner.insert(context, row());
492
+ });
493
+ throw new Error("outer");
494
+ }));
495
+ expect(await provider.count(context)).toBe(0);
496
+ });
497
+ });
498
+ });
499
+ }
500
+ export {
501
+ providerContract
502
+ };
@@ -0,0 +1,22 @@
1
+ import { type IRepositoryProvider } from "../index.js";
2
+ /** The parts of a test runner the kit uses; `bun:test`, vitest and jest fit. */
3
+ export interface ContractRunner {
4
+ describe(name: string, body: () => void): void;
5
+ it(name: string, body: () => unknown): void;
6
+ expect(value: unknown): {
7
+ toBe(expected: unknown): unknown;
8
+ toEqual(expected: unknown): unknown;
9
+ };
10
+ }
11
+ export interface ProviderContractOptions {
12
+ /**
13
+ * Run the transaction cases. Defaults to `provider.capabilities?.transactions`.
14
+ */
15
+ readonly transactions?: boolean;
16
+ }
17
+ /**
18
+ * Registers the behavior every `IRepositoryProvider` v2 must have. Pass the
19
+ * `describe`, `it` and `expect` of your runner and a factory of fresh, empty
20
+ * providers (called once per case).
21
+ */
22
+ export declare function providerContract(runner: ContractRunner, makeProvider: () => IRepositoryProvider | Promise<IRepositoryProvider>, options?: ProviderContractOptions): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afora/core",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Domain modeling on TypeBox: entities with invariants, repositories, use cases, sensitive data masking and layered exceptions, usable by any Standard Schema consumer.",
5
5
  "keywords": [
6
6
  "domain-driven-design",
@@ -40,6 +40,16 @@
40
40
  "import": "./dist/index.js",
41
41
  "default": "./dist/index.js"
42
42
  },
43
+ "./experimental": {
44
+ "types": "./dist/experimental.d.ts",
45
+ "import": "./dist/experimental.js",
46
+ "default": "./dist/experimental.js"
47
+ },
48
+ "./testing": {
49
+ "types": "./dist/testing/index.d.ts",
50
+ "import": "./dist/testing/index.js",
51
+ "default": "./dist/testing/index.js"
52
+ },
43
53
  "./package.json": "./package.json"
44
54
  },
45
55
  "files": [
@@ -58,33 +68,43 @@
58
68
  "lint": "biome lint .",
59
69
  "fmt": "biome check --write .",
60
70
  "knip": "knip",
61
- "build": "rm -rf dist && bun build ./src/index.ts --outdir ./dist --target node --format esm --packages external && tsc -p tsconfig.build.json",
71
+ "build": "rm -rf dist && bun build ./src/index.ts ./src/experimental.ts ./src/testing/index.ts --outdir ./dist --target node --format esm --splitting --packages external && tsc -p tsconfig.build.json",
62
72
  "postbuild": "bun scripts/add-dts-extensions.ts",
63
73
  "bench": "cd bench && bun install && bun run.ts",
64
74
  "smoke": "bun run build && node scripts/smoke.mjs",
65
75
  "test:types": "bun run build && bun tests/types-compat/run.ts",
76
+ "test:type-perf": "bun run build && bun test ./tests/type-perf",
77
+ "test:api": "bun run build && bun test ./tests/api-surface",
78
+ "docs:links": "bun scripts/check-doc-links.ts",
66
79
  "test:interop": "cd examples/interop && bun install && bun test",
80
+ "test:blog": "cd examples/blog && bun install && bun test",
67
81
  "prepare": "husky",
68
82
  "prepublishOnly": "bun run typecheck && bun test && bun run smoke",
69
- "test:codegen": "bun test --cwd packages/codegen",
83
+ "test:codegen": "bun run --cwd packages/codegen test",
84
+ "test:roundtrip": "bun run --cwd packages/codegen test:roundtrip",
70
85
  "test:mcp": "bun test --cwd packages/mcp",
71
- "build:packages": "bun run build && bun run --cwd packages/codegen build && bun run --cwd packages/mcp build"
86
+ "test:hono": "bun test --cwd packages/hono",
87
+ "test:drizzle": "bun test --cwd packages/drizzle",
88
+ "build:packages": "bun run build && bun run --cwd packages/codegen build && bun run --cwd packages/mcp build && bun run --cwd packages/hono build && bun run --cwd packages/drizzle build"
72
89
  },
73
90
  "dependencies": {
74
- "slugify": "^1.6.9",
75
- "typebox": "^1.4.3"
91
+ "slugify": "^1.6.9"
76
92
  },
77
93
  "devDependencies": {
78
94
  "@biomejs/biome": "^2.6.3",
79
95
  "@commitlint/cli": "^21.2.4",
80
96
  "@commitlint/config-conventional": "^21.2.4",
97
+ "@seriousme/openapi-schema-validator": "2.11.0",
81
98
  "@types/bun": "^1.4.4",
82
99
  "husky": "^9.1.7",
83
100
  "knip": "^6.43.0",
84
- "typescript": "^7.0.2"
101
+ "typebox": "^1.4.3",
102
+ "typescript": "^7.0.2",
103
+ "typescript-language-service": "npm:typescript@5.9.3"
85
104
  },
86
105
  "peerDependencies": {
87
- "typescript": ">=5.5"
106
+ "typescript": ">=5.5",
107
+ "typebox": "^1"
88
108
  },
89
109
  "peerDependenciesMeta": {
90
110
  "typescript": {