@afora/core 0.0.8 → 0.0.11

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 (226) hide show
  1. package/README.md +149 -23
  2. package/dist/adapters.d.ts +25 -0
  3. package/dist/adapters.js +32 -0
  4. package/dist/core/application/application.d.ts +30 -0
  5. package/dist/core/application/application.js +1291 -0
  6. package/dist/core/application/exceptions.d.ts +17 -0
  7. package/dist/core/application/exceptions.js +85 -0
  8. package/dist/core/application/migrate.d.ts +19 -0
  9. package/dist/core/application/migrate.js +67 -0
  10. package/dist/core/application/providers.js +108 -0
  11. package/dist/core/application/registry.js +23 -0
  12. package/dist/core/application/schema.d.ts +30 -0
  13. package/dist/core/application/schema.js +398 -0
  14. package/dist/core/application/start.js +162 -0
  15. package/dist/core/application/types/application.interface.d.ts +60 -3
  16. package/dist/core/auth/jwt.d.ts +30 -0
  17. package/dist/core/auth/jwt.js +200 -0
  18. package/dist/core/auth/types/jwt.interface.d.ts +74 -0
  19. package/dist/core/blueprint/blueprint-plugin.js +7 -0
  20. package/dist/core/blueprint/blueprint.js +146 -0
  21. package/dist/core/blueprint/callable.js +13 -0
  22. package/dist/core/blueprint/exceptions.d.ts +5 -0
  23. package/dist/core/blueprint/exceptions.js +20 -0
  24. package/dist/core/blueprint/is-blueprint.js +4 -0
  25. package/dist/core/blueprint/list.js +19 -0
  26. package/dist/core/blueprint/optional.d.ts +3 -1
  27. package/dist/core/blueprint/optional.js +69 -0
  28. package/dist/core/blueprint/record-field.js +14 -0
  29. package/dist/core/blueprint/slice.js +127 -0
  30. package/dist/core/blueprint/types/blueprint.interface.d.ts +95 -5
  31. package/dist/core/blueprint/variant.d.ts +32 -0
  32. package/dist/core/blueprint/variant.js +72 -0
  33. package/dist/core/command/command.js +428 -0
  34. package/dist/core/command/exceptions.js +30 -0
  35. package/dist/core/command/types/command.interface.d.ts +10 -1
  36. package/dist/core/entity/abstract-entity.d.ts +24 -0
  37. package/dist/core/entity/abstract-entity.js +181 -0
  38. package/dist/core/entity/entity.d.ts +1 -1
  39. package/dist/core/entity/entity.js +209 -0
  40. package/dist/core/entity/event.d.ts +39 -0
  41. package/dist/core/entity/event.js +80 -0
  42. package/dist/core/entity/exceptions.d.ts +9 -0
  43. package/dist/core/entity/exceptions.js +27 -0
  44. package/dist/core/entity/trust.d.ts +24 -0
  45. package/dist/core/entity/trust.js +7 -0
  46. package/dist/core/entity/types/entity.interface.d.ts +30 -0
  47. package/dist/core/entity/version.js +13 -0
  48. package/dist/core/environment/environment.d.ts +26 -3
  49. package/dist/core/environment/environment.js +71 -0
  50. package/dist/core/event-policy/event-policy.js +76 -0
  51. package/dist/core/event-policy/exceptions.js +12 -0
  52. package/dist/core/event-policy/publish.js +98 -0
  53. package/dist/core/event-policy/publishers.js +18 -0
  54. package/dist/core/event-policy/types/event-policy.interface.d.ts +8 -2
  55. package/dist/core/exception/enums/exception-layers.js +15 -0
  56. package/dist/core/exception/exception.d.ts +15 -0
  57. package/dist/core/exception/exception.js +128 -0
  58. package/dist/core/exception/exported.js +49 -0
  59. package/dist/core/exception/guard.d.ts +20 -1
  60. package/dist/core/exception/guard.js +16 -0
  61. package/dist/core/exception/types/exception.interface.d.ts +13 -0
  62. package/dist/core/json.js +19 -0
  63. package/dist/core/logger/exceptions.js +7 -0
  64. package/dist/core/logger/logger.d.ts +14 -1
  65. package/dist/core/logger/logger.js +178 -0
  66. package/dist/core/openapi/exceptions.js +7 -0
  67. package/dist/core/openapi/openapi.js +132 -0
  68. package/dist/core/openapi/types/openapi.interface.d.ts +7 -1
  69. package/dist/core/output/access.d.ts +16 -0
  70. package/dist/core/output/access.js +66 -0
  71. package/dist/core/output/coerce.js +38 -0
  72. package/dist/core/output/exceptions.d.ts +16 -0
  73. package/dist/core/output/exceptions.js +22 -0
  74. package/dist/core/output/http.d.ts +36 -0
  75. package/dist/core/output/http.js +351 -0
  76. package/dist/core/output/openapi-sink.d.ts +33 -4
  77. package/dist/core/output/openapi-sink.js +571 -0
  78. package/dist/core/output/output.js +192 -0
  79. package/dist/core/output/problem.js +30 -0
  80. package/dist/core/output/select.js +28 -0
  81. package/dist/core/output/stage.d.ts +7 -2
  82. package/dist/core/output/stage.js +111 -0
  83. package/dist/core/output/status.d.ts +1 -1
  84. package/dist/core/output/status.js +52 -0
  85. package/dist/core/output/types/http.interface.d.ts +134 -0
  86. package/dist/core/output/types/output.interface.d.ts +49 -5
  87. package/dist/core/parse/issue.d.ts +28 -3
  88. package/dist/core/parse/issue.js +31 -0
  89. package/dist/core/parse/parse.d.ts +16 -0
  90. package/dist/core/parse/parse.js +323 -0
  91. package/dist/core/parse/strip.js +48 -0
  92. package/dist/core/property/abstract-property.d.ts +9 -0
  93. package/dist/core/property/abstract-property.js +37 -0
  94. package/dist/core/property/define-property.d.ts +34 -6
  95. package/dist/core/property/define-property.js +209 -0
  96. package/dist/core/property/enums/property-states.js +8 -0
  97. package/dist/core/property/exceptions.js +12 -0
  98. package/dist/core/property/input.js +10 -0
  99. package/dist/core/property/is-property.js +3 -0
  100. package/dist/core/property/lineage.js +8 -0
  101. package/dist/core/property/property.d.ts +27 -0
  102. package/dist/core/property/property.js +46 -0
  103. package/dist/core/property/refine.js +103 -0
  104. package/dist/core/property/types/property-options.interface.d.ts +10 -0
  105. package/dist/core/property/types/property-type.interface.d.ts +4 -0
  106. package/dist/core/record/abstract-record.d.ts +8 -0
  107. package/dist/core/record/abstract-record.js +307 -0
  108. package/dist/core/record/exceptions.js +49 -0
  109. package/dist/core/record/marks.js +7 -0
  110. package/dist/core/record/mask.js +60 -0
  111. package/dist/core/record/name.js +23 -0
  112. package/dist/core/record/plugin-catalog.js +115 -0
  113. package/dist/core/record/plugin.js +198 -0
  114. package/dist/core/record/record.js +117 -0
  115. package/dist/core/record/types/record.interface.d.ts +17 -1
  116. package/dist/core/repository/cursor.js +83 -0
  117. package/dist/core/repository/exceptions.d.ts +13 -0
  118. package/dist/core/repository/exceptions.js +72 -0
  119. package/dist/core/repository/in-memory.d.ts +2 -2
  120. package/dist/core/repository/in-memory.js +196 -0
  121. package/dist/core/repository/migrate.d.ts +20 -0
  122. package/dist/core/repository/migrate.js +254 -0
  123. package/dist/core/repository/plan.js +27 -0
  124. package/dist/core/repository/provider.js +29 -0
  125. package/dist/core/repository/query.js +153 -0
  126. package/dist/core/repository/references.js +196 -0
  127. package/dist/core/repository/repository.d.ts +22 -2
  128. package/dist/core/repository/repository.js +720 -0
  129. package/dist/core/repository/state.d.ts +2 -0
  130. package/dist/core/repository/state.js +138 -0
  131. package/dist/core/repository/types/query.interface.d.ts +13 -0
  132. package/dist/core/repository/types/repository.interface.d.ts +50 -5
  133. package/dist/core/repository/types/schema.interface.d.ts +132 -0
  134. package/dist/core/repository/upcast.d.ts +64 -0
  135. package/dist/core/repository/upcast.js +179 -0
  136. package/dist/core/schema/schema.d.ts +37 -0
  137. package/dist/core/schema/schema.js +542 -0
  138. package/dist/core/standard/standard-schema.js +1 -0
  139. package/dist/core/standard/standard.js +23 -0
  140. package/dist/core/symbols/demo.d.ts +4 -0
  141. package/dist/core/symbols/demo.js +1 -0
  142. package/dist/core/symbols/kind.d.ts +1 -1
  143. package/dist/core/symbols/kind.js +1 -0
  144. package/dist/core/symbols/meta.js +1 -0
  145. package/dist/core/symbols/state.d.ts +5 -0
  146. package/dist/core/symbols/state.js +1 -0
  147. package/dist/core/tracing/exceptions.d.ts +4 -0
  148. package/dist/core/tracing/exceptions.js +7 -0
  149. package/dist/core/tracing/tracing.d.ts +72 -0
  150. package/dist/core/tracing/tracing.js +101 -0
  151. package/dist/core/use-case/access.d.ts +12 -0
  152. package/dist/core/use-case/access.js +56 -0
  153. package/dist/core/use-case/decorators.d.ts +40 -0
  154. package/dist/core/use-case/decorators.js +65 -0
  155. package/dist/core/use-case/exceptions.d.ts +5 -1
  156. package/dist/core/use-case/exceptions.js +48 -0
  157. package/dist/core/use-case/plugin-catalog.js +68 -0
  158. package/dist/core/use-case/plugin.js +133 -0
  159. package/dist/core/use-case/route.d.ts +18 -0
  160. package/dist/core/use-case/route.js +200 -0
  161. package/dist/core/use-case/scope.d.ts +5 -0
  162. package/dist/core/use-case/scope.js +4 -0
  163. package/dist/core/use-case/steps/describe.d.ts +5 -1
  164. package/dist/core/use-case/steps/describe.js +24 -0
  165. package/dist/core/use-case/steps/references.js +105 -0
  166. package/dist/core/use-case/steps/run.js +139 -0
  167. package/dist/core/use-case/steps/types/check.interface.d.ts +233 -0
  168. package/dist/core/use-case/steps/types/steps.interface.d.ts +8 -1
  169. package/dist/core/use-case/steps/validate.js +368 -0
  170. package/dist/core/use-case/target.js +100 -0
  171. package/dist/core/use-case/types/access.interface.d.ts +39 -0
  172. package/dist/core/use-case/types/route.interface.d.ts +86 -0
  173. package/dist/core/use-case/use-case.d.ts +127 -2
  174. package/dist/core/use-case/use-case.js +493 -0
  175. package/dist/experimental.d.ts +18 -1
  176. package/dist/experimental.js +18 -123
  177. package/dist/index.d.ts +49 -51
  178. package/dist/index.js +213 -443
  179. package/dist/std/blueprint-plugin.js +42 -0
  180. package/dist/std/blueprint.d.ts +21 -0
  181. package/dist/std/blueprint.js +3 -0
  182. package/dist/std/command.js +3 -0
  183. package/dist/std/entity.d.ts +20 -1
  184. package/dist/std/entity.js +30 -0
  185. package/dist/std/event-policies.js +3 -0
  186. package/dist/std/properties/contact.d.ts +3 -0
  187. package/dist/std/properties/contact.js +29 -0
  188. package/dist/std/properties/enum.js +26 -0
  189. package/dist/std/properties/exceptions.js +14 -0
  190. package/dist/std/properties/headless.js +45 -0
  191. package/dist/std/properties/identity.js +28 -0
  192. package/dist/std/properties/json.js +161 -0
  193. package/dist/std/properties/password.d.ts +0 -2
  194. package/dist/std/properties/password.js +79 -0
  195. package/dist/std/properties/primitives.d.ts +4 -0
  196. package/dist/std/properties/primitives.js +28 -0
  197. package/dist/std/properties/time.js +55 -0
  198. package/dist/std/record-plugins.d.ts +2 -2
  199. package/dist/std/record-plugins.js +73 -0
  200. package/dist/std/record.d.ts +20 -1
  201. package/dist/std/record.js +3 -0
  202. package/dist/std/resource.d.ts +31 -0
  203. package/dist/std/resource.js +39 -0
  204. package/dist/std/runtime.d.ts +6 -0
  205. package/dist/std/runtime.js +193 -0
  206. package/dist/std/stages.js +4 -0
  207. package/dist/std/use-cases/auth.d.ts +5 -1
  208. package/dist/std/use-cases/auth.js +193 -0
  209. package/dist/std/use-cases/crud.js +261 -0
  210. package/dist/std/use-cases/index.d.ts +1 -1
  211. package/dist/std/use-cases/index.js +12 -0
  212. package/dist/std/use-cases/lifecycle.js +97 -0
  213. package/dist/std/use-cases/shared.d.ts +2 -2
  214. package/dist/std/use-cases/shared.js +232 -0
  215. package/dist/std/use-cases/types.d.ts +102 -2
  216. package/dist/std/use-cases/types.js +0 -0
  217. package/dist/testing/http-provider-contract.d.ts +32 -0
  218. package/dist/testing/http-provider-contract.js +344 -0
  219. package/dist/testing/index.d.ts +2 -0
  220. package/dist/testing/index.js +3 -944
  221. package/dist/testing/memory-publisher.js +12 -0
  222. package/dist/testing/provider-contract.d.ts +26 -1
  223. package/dist/testing/provider-contract.js +1071 -0
  224. package/package.json +23 -3
  225. package/dist/index-qxfkyd6k.js +0 -1721
  226. package/dist/index-t5qwtyj5.js +0 -8194
@@ -1,944 +1,3 @@
1
- import"../index-qxfkyd6k.js";
2
- import {
3
- RepositoryUniqueViolation2,
4
- RepositoryEntityNotFound2,
5
- RepositoryConcurrencyConflict2,
6
- blueprintOf2,
7
- entityOf2
8
- } from "../index-t5qwtyj5.js";
9
-
10
- // src/testing/memory-publisher.ts
11
- function memoryPublisher() {
12
- const published = [];
13
- return {
14
- published,
15
- publish(topic, event) {
16
- published.push({ topic, event });
17
- },
18
- clear() {
19
- published.length = 0;
20
- }
21
- };
22
- }
23
- // src/testing/provider-contract.ts
24
- function contextOf(name = "Accounts") {
25
- class Account extends entityOf2(blueprintOf2((ctx) => ({ email: ctx.string, name: ctx.string }))) {
26
- }
27
- return {
28
- entity: Account,
29
- name,
30
- unique: ["id", "email"],
31
- env: {}
32
- };
33
- }
34
- var sequence = 0;
35
- function row(overrides = {}) {
36
- sequence += 1;
37
- const id = `id-${String(sequence).padStart(4, "0")}`;
38
- return { id, email: `${id}@example.com`, name: id, ...overrides };
39
- }
40
- async function failure(promise) {
41
- try {
42
- await promise;
43
- } catch (error) {
44
- return error;
45
- }
46
- return;
47
- }
48
- var ids = (rows) => rows.map((r) => r.id);
49
- function providerContract(runner, makeProvider, options = {}) {
50
- const { describe, it, expect } = runner;
51
- async function fixture() {
52
- const provider = await makeProvider();
53
- const context = contextOf();
54
- const seed = async (...rows) => {
55
- for (const data of rows)
56
- await provider.insert(context, data);
57
- };
58
- return { provider, context, seed };
59
- }
60
- const gated = (capability) => (name, body) => it(name, async () => {
61
- const enabled = options[capability] ?? (await makeProvider()).capabilities?.[capability];
62
- if (enabled === true)
63
- await body();
64
- });
65
- describe("provider contract", () => {
66
- describe("writes and reads", () => {
67
- it("inserts and finds by id, empty provider counts zero", async () => {
68
- const { provider, context } = await fixture();
69
- expect(await provider.count(context)).toBe(0);
70
- expect(await provider.findOne(context)).toBe(undefined);
71
- expect(await provider.findMany(context)).toEqual([]);
72
- const data = row({ age: 30 });
73
- await provider.insert(context, data);
74
- expect(await provider.count(context)).toBe(1);
75
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual(data);
76
- expect(await provider.findMany(context)).toEqual([data]);
77
- });
78
- it("findOne is undefined when nothing matches", async () => {
79
- const { provider, context, seed } = await fixture();
80
- await seed(row());
81
- expect(await provider.findOne(context, { where: { id: "missing" } })).toBe(undefined);
82
- expect(await provider.findMany(context, { where: { id: "missing" } })).toEqual([]);
83
- });
84
- it("update replaces the stored value", async () => {
85
- const { provider, context, seed } = await fixture();
86
- const data = row({ name: "Ana" });
87
- await seed(data);
88
- await provider.update(context, { ...data, name: "Bia" });
89
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Bia" });
90
- expect(await provider.count(context)).toBe(1);
91
- });
92
- it("remove deletes the row", async () => {
93
- const { provider, context, seed } = await fixture();
94
- const [a, b] = [row(), row()];
95
- await seed(a, b);
96
- await provider.remove(context, a.id);
97
- expect(ids(await provider.findMany(context))).toEqual([b.id]);
98
- expect(await provider.count(context)).toBe(1);
99
- });
100
- it("update and remove of an unknown id throw RepositoryEntityNotFound", async () => {
101
- const { provider, context, seed } = await fixture();
102
- await seed(row());
103
- const updated = await failure(provider.update(context, row()));
104
- expect(updated instanceof RepositoryEntityNotFound2).toBe(true);
105
- const removed = await failure(provider.remove(context, "missing"));
106
- expect(removed instanceof RepositoryEntityNotFound2).toBe(true);
107
- expect(await provider.count(context)).toBe(1);
108
- });
109
- it("keeps tables of different contexts apart", async () => {
110
- const provider = await makeProvider();
111
- const [one, two] = [contextOf("One"), contextOf("Two")];
112
- await provider.insert(one, row());
113
- expect(await provider.count(one)).toBe(1);
114
- expect(await provider.count(two)).toBe(0);
115
- });
116
- });
117
- describe("unique keys", () => {
118
- it("insert throws RepositoryUniqueViolation for a repeated id", async () => {
119
- const { provider, context, seed } = await fixture();
120
- const data = row();
121
- await seed(data);
122
- const error = await failure(provider.insert(context, { ...row(), id: data.id }));
123
- expect(error instanceof RepositoryUniqueViolation2).toBe(true);
124
- expect(error.details.key).toBe("id");
125
- expect(await provider.count(context)).toBe(1);
126
- });
127
- it("insert throws for a repeated unique key", async () => {
128
- const { provider, context, seed } = await fixture();
129
- await seed(row({ email: "a@example.com" }));
130
- const error = await failure(provider.insert(context, row({ email: "a@example.com" })));
131
- expect(error instanceof RepositoryUniqueViolation2).toBe(true);
132
- expect(error.details.key).toBe("email");
133
- expect(await provider.count(context)).toBe(1);
134
- });
135
- it("update throws when taking the unique value of another row", async () => {
136
- const { provider, context, seed } = await fixture();
137
- const [a, b] = [row({ email: "a@example.com" }), row()];
138
- await seed(a, b);
139
- const error = await failure(provider.update(context, { ...b, email: "a@example.com" }));
140
- expect(error instanceof RepositoryUniqueViolation2).toBe(true);
141
- expect(error.details.key).toBe("email");
142
- expect(await provider.findOne(context, { where: { id: b.id } })).toEqual(b);
143
- });
144
- it("update may keep the unique values of its own row", async () => {
145
- const { provider, context, seed } = await fixture();
146
- const data = row({ email: "a@example.com" });
147
- await seed(data);
148
- await provider.update(context, { ...data, name: "Other" });
149
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Other" });
150
- });
151
- it("update clears a value that is left out", async () => {
152
- const { provider, context, seed } = await fixture();
153
- const data = row({ deletedAt: "2024-01-01T00:00:00.000Z" });
154
- await seed(data);
155
- const { deletedAt: _deletedAt, ...alive } = data;
156
- await provider.update(context, alive);
157
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual(alive);
158
- });
159
- it("soft-deleted rows keep occupying unique values", async () => {
160
- const { provider, context, seed } = await fixture();
161
- await seed(row({
162
- email: "a@example.com",
163
- deletedAt: "2024-01-01T00:00:00.000Z"
164
- }));
165
- const inserted = await failure(provider.insert(context, row({ email: "a@example.com" })));
166
- expect(inserted instanceof RepositoryUniqueViolation2).toBe(true);
167
- const other = row();
168
- await seed(other);
169
- const updated = await failure(provider.update(context, { ...other, email: "a@example.com" }));
170
- expect(updated instanceof RepositoryUniqueViolation2).toBe(true);
171
- });
172
- });
173
- describe("where", () => {
174
- it("matches scalars by equality", async () => {
175
- const { provider, context, seed } = await fixture();
176
- const [a, b, c] = [
177
- row({ age: 20, name: "Ana", active: true }),
178
- row({ age: 30, name: "Bia", active: false }),
179
- row({ age: 30, name: "Cy", active: true })
180
- ];
181
- await seed(a, b, c);
182
- const find = (where) => provider.findMany(context, { where });
183
- expect(ids(await find({ age: 30 }))).toEqual([b.id, c.id]);
184
- expect(ids(await find({ name: "Ana" }))).toEqual([a.id]);
185
- expect(ids(await find({ active: false }))).toEqual([b.id]);
186
- expect(ids(await find({ age: 31 }))).toEqual([]);
187
- expect(ids(await find({}))).toEqual([a.id, b.id, c.id]);
188
- });
189
- it("matches structured values by structure", async () => {
190
- const { provider, context, seed } = await fixture();
191
- const [a, b] = [
192
- row({ address: { city: "Rio", zip: "1" }, tags: ["x", "y"] }),
193
- row({ address: { city: "Rio", zip: "2" }, tags: ["y", "x"] })
194
- ];
195
- await seed(a, b);
196
- const find = (where) => provider.findMany(context, { where });
197
- expect(ids(await find({ address: { city: "Rio", zip: "1" } }))).toEqual([a.id]);
198
- expect(ids(await find({ address: { city: "Rio" } }))).toEqual([]);
199
- expect(ids(await find({ tags: ["y", "x"] }))).toEqual([b.id]);
200
- expect(ids(await find({ tags: ["x"] }))).toEqual([]);
201
- });
202
- it("matches membership with in", async () => {
203
- const { provider, context, seed } = await fixture();
204
- const [a, b, c] = [row({ age: 1 }), row({ age: 2 }), row({ age: 3 })];
205
- await seed(a, b, c);
206
- const find = (where) => provider.findMany(context, { where });
207
- expect(ids(await find({ age: { in: [1, 3] } }))).toEqual([a.id, c.id]);
208
- expect(ids(await find({ id: { in: [b.id, "missing"] } }))).toEqual([
209
- b.id
210
- ]);
211
- expect(ids(await find({ age: { in: [] } }))).toEqual([]);
212
- expect(await provider.count(context, { where: { age: { in: [] } } })).toBe(0);
213
- expect(await provider.findOne(context, { where: { age: { in: [] } } })).toBe(undefined);
214
- });
215
- it("ANDs conditions on several keys", async () => {
216
- const { provider, context, seed } = await fixture();
217
- const [a, b, c] = [
218
- row({ age: 30, name: "Ana" }),
219
- row({ age: 30, name: "Bia" }),
220
- row({ age: 20, name: "Bia" })
221
- ];
222
- await seed(a, b, c);
223
- const find = (where) => provider.findMany(context, { where });
224
- expect(ids(await find({ age: 30, name: "Bia" }))).toEqual([b.id]);
225
- expect(ids(await find({ age: { in: [20, 30] }, name: { in: ["Bia"] } }))).toEqual([b.id, c.id]);
226
- expect(ids(await find({ age: 20, name: "Ana" }))).toEqual([]);
227
- });
228
- });
229
- describe("orderBy", () => {
230
- const order = async (rows, orderBy) => {
231
- const { provider, context, seed } = await fixture();
232
- await seed(...rows);
233
- return ids(await provider.findMany(context, { orderBy }));
234
- };
235
- it("is ascending by default and descending on request", async () => {
236
- const [a, b, c] = [row({ age: 2 }), row({ age: 3 }), row({ age: 1 })];
237
- expect(await order([a, b, c], [{ key: "age" }])).toEqual([
238
- c.id,
239
- a.id,
240
- b.id
241
- ]);
242
- expect(await order([a, b, c], [{ key: "age", direction: "asc" }])).toEqual([c.id, a.id, b.id]);
243
- expect(await order([a, b, c], [{ key: "age", direction: "desc" }])).toEqual([b.id, a.id, c.id]);
244
- });
245
- it("sorts numbers numerically", async () => {
246
- const [a, b, c] = [
247
- row({ age: 10 }),
248
- row({ age: 9 }),
249
- row({ age: 100 })
250
- ];
251
- expect(await order([a, b, c], [{ key: "age" }])).toEqual([
252
- b.id,
253
- a.id,
254
- c.id
255
- ]);
256
- });
257
- it("sorts strings by code point", async () => {
258
- const [lower, upper, wide, emoji] = [
259
- row({ name: "a" }),
260
- row({ name: "Z" }),
261
- row({ name: "z" }),
262
- row({ name: "\uD83D\uDE00" })
263
- ];
264
- expect(await order([emoji, lower, wide, upper], [{ key: "name" }])).toEqual([upper.id, lower.id, wide.id, emoji.id]);
265
- });
266
- it("puts missing values first when ascending and last when descending", async () => {
267
- const [a, b, c] = [row({ age: 2 }), row(), row({ age: 1 })];
268
- expect(await order([a, b, c], [{ key: "age" }])).toEqual([
269
- b.id,
270
- c.id,
271
- a.id
272
- ]);
273
- expect(await order([a, b, c], [{ key: "age", direction: "desc" }])).toEqual([a.id, c.id, b.id]);
274
- });
275
- it("keeps the insertion order of ties", async () => {
276
- const [a, b, c, d] = [
277
- row({ group: 1 }),
278
- row({ group: 0 }),
279
- row({ group: 1 }),
280
- row({ group: 0 })
281
- ];
282
- expect(await order([a, b, c, d], [{ key: "group" }])).toEqual([
283
- b.id,
284
- d.id,
285
- a.id,
286
- c.id
287
- ]);
288
- expect(await order([a, b, c, d], [{ key: "group", direction: "desc" }])).toEqual([a.id, c.id, b.id, d.id]);
289
- });
290
- it("sorts by several keys in turn", async () => {
291
- const [a, b, c, d] = [
292
- row({ group: 1, name: "b" }),
293
- row({ group: 0, name: "z" }),
294
- row({ group: 1, name: "a" }),
295
- row({ group: 0, name: "y" })
296
- ];
297
- expect(await order([a, b, c, d], [{ key: "group" }, { key: "name", direction: "desc" }])).toEqual([b.id, d.id, a.id, c.id]);
298
- expect(await order([a, b, c, d], [{ key: "group", direction: "desc" }, { key: "name" }])).toEqual([c.id, a.id, d.id, b.id]);
299
- });
300
- });
301
- describe("limit and offset", () => {
302
- const seeded = async () => {
303
- const f = await fixture();
304
- const rows = [1, 2, 3, 4, 5].map((n) => row({ n }));
305
- await f.seed(...rows);
306
- return { ...f, rows };
307
- };
308
- const byN = [{ key: "n" }];
309
- it("limit takes the first rows, offset skips rows before it", async () => {
310
- const { provider, context, rows } = await seeded();
311
- const find = (limit, offset) => provider.findMany(context, { orderBy: byN, limit, offset }).then(ids);
312
- expect(await find(2)).toEqual([rows[0]?.id, rows[1]?.id]);
313
- expect(await find(undefined, 3)).toEqual([rows[3]?.id, rows[4]?.id]);
314
- expect(await find(2, 1)).toEqual([rows[1]?.id, rows[2]?.id]);
315
- expect(await find(10, 4)).toEqual([rows[4]?.id]);
316
- expect(await find(2, 5)).toEqual([]);
317
- });
318
- it("limit 0 returns nothing", async () => {
319
- const { provider, context } = await seeded();
320
- expect(await provider.findMany(context, { limit: 0 })).toEqual([]);
321
- expect(await provider.findOne(context, { limit: 0 })).toBe(undefined);
322
- });
323
- it("findOne returns the first row after the offset", async () => {
324
- const { provider, context, rows } = await seeded();
325
- expect((await provider.findOne(context, { orderBy: byN, offset: 2 }))?.id).toBe(rows[2]?.id);
326
- expect(await provider.findOne(context, { orderBy: byN, offset: 5 })).toBe(undefined);
327
- });
328
- it("count ignores order, limit and offset", async () => {
329
- const { provider, context } = await seeded();
330
- expect(await provider.count(context, {
331
- orderBy: [{ key: "n", direction: "desc" }],
332
- limit: 1,
333
- offset: 3
334
- })).toBe(5);
335
- expect(await provider.count(context, {
336
- where: { n: { in: [1, 2] } },
337
- limit: 0,
338
- offset: 9
339
- })).toBe(2);
340
- });
341
- });
342
- describe("withDeleted", () => {
343
- const deletedAt = "2024-01-01T00:00:00.000Z";
344
- const seeded = async () => {
345
- const f = await fixture();
346
- const [live, gone] = [row({ n: 1 }), row({ n: 2, deletedAt })];
347
- await f.seed(live, gone);
348
- return { ...f, live, gone };
349
- };
350
- it("excludes deleted rows by default", async () => {
351
- const { provider, context, live, gone } = await seeded();
352
- expect(ids(await provider.findMany(context))).toEqual([live.id]);
353
- expect(ids(await provider.findMany(context, { withDeleted: false }))).toEqual([live.id]);
354
- expect(await provider.count(context)).toBe(1);
355
- expect(await provider.findOne(context, { where: { id: gone.id } })).toBe(undefined);
356
- });
357
- it("includes them with withDeleted", async () => {
358
- const { provider, context, live, gone } = await seeded();
359
- const query = { withDeleted: true };
360
- expect(ids(await provider.findMany(context, query))).toEqual([
361
- live.id,
362
- gone.id
363
- ]);
364
- expect(await provider.count(context, query)).toBe(2);
365
- expect(await provider.findOne(context, {
366
- where: { id: gone.id },
367
- withDeleted: true
368
- })).toEqual(gone);
369
- });
370
- });
371
- describe("copies", () => {
372
- it("does not share data with the caller on write", async () => {
373
- const { provider, context } = await fixture();
374
- const data = row({ address: { city: "Rio" }, tags: ["x"] });
375
- await provider.insert(context, data);
376
- data.address.city = "SP";
377
- data.tags.push("y");
378
- const stored = await provider.findOne(context, {
379
- where: { id: data.id }
380
- });
381
- expect(stored?.address).toEqual({ city: "Rio" });
382
- expect(stored?.tags).toEqual(["x"]);
383
- });
384
- it("does not share data with the caller on read", async () => {
385
- const { provider, context, seed } = await fixture();
386
- const data = row({ address: { city: "Rio" }, tags: ["x"] });
387
- await seed(data);
388
- const one = await provider.findOne(context, {
389
- where: { id: data.id }
390
- });
391
- one.address.city = "SP";
392
- one.name = "changed";
393
- const many = await provider.findMany(context);
394
- many[0].tags.push("y");
395
- const stored = await provider.findOne(context, {
396
- where: { id: data.id }
397
- });
398
- expect(stored).toEqual(data);
399
- });
400
- });
401
- describe("keyset", () => {
402
- const keyset = (name, body) => it(name, async () => {
403
- const enabled = options.keyset ?? (await makeProvider()).capabilities?.keyset;
404
- if (enabled === true)
405
- await body();
406
- });
407
- const seeded = async () => {
408
- const f = await fixture();
409
- const [a, b, c, d, e, g] = [
410
- row({ n: 1, group: 1, name: "Gil" }),
411
- row({ group: 1, name: "Érico" }),
412
- row({ n: 2, group: 2, name: "Ana" }),
413
- row({ n: 2, group: 1, name: "ana" }),
414
- row({ group: 2, name: "Bia" }),
415
- row({ n: 3, group: 2, name: "Zoe" })
416
- ];
417
- await f.seed(a, b, c, d, e, g);
418
- const find = (query) => f.provider.findMany(f.context, query).then(ids);
419
- return { ...f, a, b, c, d, e, g, find };
420
- };
421
- const by = (key, direction = "asc") => [{ key, direction }, { key: "id" }];
422
- keyset("gives the rows strictly after the position, ascending", async () => {
423
- const { find, a, c, d, g } = await seeded();
424
- const orderBy = by("n");
425
- expect(await find({ orderBy, after: [1, a.id] })).toEqual([
426
- c.id,
427
- d.id,
428
- g.id
429
- ]);
430
- expect(await find({ orderBy, after: [2, c.id] })).toEqual([
431
- d.id,
432
- g.id
433
- ]);
434
- expect(await find({ orderBy, after: [3, g.id] })).toEqual([]);
435
- });
436
- keyset("follows a descending order", async () => {
437
- const { find, a, c, d, g } = await seeded();
438
- const orderBy = by("n", "desc");
439
- expect(await find({
440
- orderBy,
441
- after: [3, g.id],
442
- where: { n: { in: [1, 2] } }
443
- })).toEqual([c.id, d.id, a.id]);
444
- expect(await find({
445
- orderBy,
446
- after: [2, d.id],
447
- where: { n: { in: [1, 2] } }
448
- })).toEqual([a.id]);
449
- });
450
- keyset("missing values are first ascending and last descending", async () => {
451
- const { find, a, b, c, d, e, g } = await seeded();
452
- expect(await find({ orderBy: by("n"), after: [undefined, b.id] })).toEqual([e.id, a.id, c.id, d.id, g.id]);
453
- expect(await find({ orderBy: by("n"), after: [undefined, e.id] })).toEqual([a.id, c.id, d.id, g.id]);
454
- expect(await find({ orderBy: by("n", "desc"), after: [1, a.id] })).toEqual([b.id, e.id]);
455
- expect(await find({ orderBy: by("n", "desc"), after: [undefined, b.id] })).toEqual([e.id]);
456
- expect(await find({ orderBy: by("n", "desc"), after: [undefined, e.id] })).toEqual([]);
457
- });
458
- keyset("compares strings by code point", async () => {
459
- const { find, a, b, c, d, e, g } = await seeded();
460
- expect(await find({ orderBy: by("name"), after: ["Bia", e.id] })).toEqual([a.id, g.id, d.id, b.id]);
461
- expect(await find({ orderBy: by("name", "desc"), after: ["ana", d.id] })).toEqual([g.id, a.id, e.id, c.id]);
462
- });
463
- keyset("combines with where, withDeleted and limit; count ignores it", async () => {
464
- const { provider, context, find, a, b, d } = await seeded();
465
- const orderBy = [{ key: "id" }];
466
- expect(await find({
467
- orderBy,
468
- after: [a.id],
469
- where: { group: 1 },
470
- limit: 1
471
- })).toEqual([b.id]);
472
- expect(await find({ orderBy, after: [b.id], where: { group: 1 } })).toEqual([d.id]);
473
- await provider.update(context, {
474
- ...d,
475
- deletedAt: "2024-01-01T00:00:00.000Z"
476
- });
477
- expect(await find({ orderBy, after: [b.id], where: { group: 1 } })).toEqual([]);
478
- expect(await find({
479
- orderBy,
480
- after: [b.id],
481
- where: { group: 1 },
482
- withDeleted: true
483
- })).toEqual([d.id]);
484
- expect(await provider.count(context, {
485
- orderBy,
486
- after: [b.id]
487
- })).toBe(5);
488
- });
489
- });
490
- describe("indexes", () => {
491
- const indexed = (name, body) => it(name, async () => {
492
- const enabled = options.indexes ?? (await makeProvider()).capabilities?.indexes;
493
- if (enabled === true)
494
- await body();
495
- });
496
- const seeded = async () => {
497
- const f = await fixture();
498
- const context = {
499
- ...f.context,
500
- indexes: {
501
- byGroup: { where: ["group"], orderBy: "n" },
502
- byName: { where: ["name"] }
503
- }
504
- };
505
- const rows = [
506
- row({ n: 3, group: 1, name: "Gil" }),
507
- row({ group: 1, name: "Érico" }),
508
- row({ n: 2, group: 2, name: "Ana" }),
509
- row({ n: 2, group: 1, name: "Ana" }),
510
- row({ n: 1, group: 1, name: "Bia" }),
511
- row({ n: 4, group: 2, name: "Zoe" })
512
- ];
513
- for (const data of rows)
514
- await f.provider.insert(context, data);
515
- await f.provider.update(context, {
516
- ...rows[4],
517
- deletedAt: "2024-01-01T00:00:00.000Z"
518
- });
519
- return { ...f, context };
520
- };
521
- indexed("an indexed query gives what the scan gives", async () => {
522
- const { provider, context } = await seeded();
523
- const queries = [
524
- ["byGroup", { where: { group: 1 } }],
525
- [
526
- "byGroup",
527
- {
528
- where: { group: 1 },
529
- orderBy: [{ key: "n" }, { key: "id" }]
530
- }
531
- ],
532
- [
533
- "byGroup",
534
- {
535
- where: { group: 1, name: "Ana" },
536
- orderBy: [{ key: "n", direction: "desc" }, { key: "id" }],
537
- limit: 2,
538
- offset: 1
539
- }
540
- ],
541
- [
542
- "byGroup",
543
- {
544
- where: { group: 1 },
545
- orderBy: [{ key: "n" }, { key: "id" }],
546
- withDeleted: true
547
- }
548
- ],
549
- ["byName", { where: { name: "Ana" }, orderBy: [{ key: "id" }] }]
550
- ];
551
- for (const [index, query] of queries) {
552
- expect(ids(await provider.findMany(context, { ...query, index }))).toEqual(ids(await provider.findMany(context, query)));
553
- expect(await provider.count(context, {
554
- ...query,
555
- index
556
- })).toBe(await provider.count(context, query));
557
- expect((await provider.findOne(context, {
558
- ...query,
559
- index
560
- }))?.id).toBe((await provider.findOne(context, query))?.id);
561
- }
562
- });
563
- });
564
- const withTransactions = async () => {
565
- const enabled = options.transactions ?? (await makeProvider()).capabilities?.transactions;
566
- return enabled === true;
567
- };
568
- describe("transactions", () => {
569
- const transactional = (name, body) => it(name, async () => {
570
- if (!await withTransactions())
571
- return;
572
- await body();
573
- });
574
- const open = async () => {
575
- const f = await fixture();
576
- const transaction = f.provider.transaction?.bind(f.provider);
577
- if (!transaction) {
578
- throw new Error("the provider advertises transactions but has none");
579
- }
580
- return { ...f, transaction };
581
- };
582
- transactional("commits the writes when the callback resolves", async () => {
583
- const { provider, context, seed, transaction } = await open();
584
- const [a, b] = [row({ name: "a" }), row()];
585
- await seed(a);
586
- const result = await transaction(async (tx) => {
587
- await tx.insert(context, b);
588
- await tx.update(context, { ...a, name: "changed" });
589
- expect(await tx.count(context)).toBe(2);
590
- return "done";
591
- });
592
- expect(result).toBe("done");
593
- expect(ids(await provider.findMany(context))).toEqual([a.id, b.id]);
594
- expect((await provider.findOne(context, { where: { id: a.id } }))?.name).toBe("changed");
595
- });
596
- transactional("rolls back every write when the callback rejects", async () => {
597
- const { provider, context, seed, transaction } = await open();
598
- const [a, b] = [row({ name: "a" }), row()];
599
- await seed(a, b);
600
- const boom = new Error("boom");
601
- const error = await failure(transaction(async (tx) => {
602
- await tx.insert(context, row());
603
- await tx.update(context, { ...a, name: "changed" });
604
- await tx.remove(context, b.id);
605
- throw boom;
606
- }));
607
- expect(error).toBe(boom);
608
- expect(await provider.findMany(context)).toEqual([a, b]);
609
- });
610
- transactional("rolls back after a failed write inside", async () => {
611
- const { provider, context, seed, transaction } = await open();
612
- const a = row({ email: "a@example.com" });
613
- await seed(a);
614
- const error = await failure(transaction(async (tx) => {
615
- await tx.insert(context, row());
616
- await tx.insert(context, row({ email: "a@example.com" }));
617
- }));
618
- expect(error instanceof RepositoryUniqueViolation2).toBe(true);
619
- expect(await provider.findMany(context)).toEqual([a]);
620
- });
621
- transactional("does not lose writes made outside meanwhile", async () => {
622
- const { provider, context, transaction } = await open();
623
- const [inside, outside] = [row(), row()];
624
- let release = () => {};
625
- const gate = new Promise((resolve) => {
626
- release = resolve;
627
- });
628
- let started = () => {};
629
- const entered = new Promise((resolve) => {
630
- started = resolve;
631
- });
632
- const running = failure(transaction(async (tx) => {
633
- await tx.insert(context, inside);
634
- started();
635
- await gate;
636
- throw new Error("rollback");
637
- }));
638
- await entered;
639
- const writing = provider.insert(context, outside);
640
- release();
641
- await running;
642
- await writing;
643
- expect(ids(await provider.findMany(context))).toEqual([outside.id]);
644
- });
645
- transactional("rejects the scoped provider after the callback", async () => {
646
- const { context, transaction } = await open();
647
- let scoped;
648
- await transaction(async (tx) => {
649
- scoped = tx;
650
- });
651
- const error = await failure(scoped.insert(context, row()));
652
- expect(error instanceof Error).toBe(true);
653
- const read = await failure(scoped.count(context));
654
- expect(read instanceof Error).toBe(true);
655
- });
656
- transactional("treats a nested transaction as a savepoint", async () => {
657
- const { provider, context, transaction } = await open();
658
- const [outer, kept, dropped] = [row(), row(), row()];
659
- await transaction(async (tx) => {
660
- expect(typeof tx.transaction).toBe("function");
661
- await tx.insert(context, outer);
662
- await tx.transaction?.(async (inner) => {
663
- await inner.insert(context, kept);
664
- });
665
- const error = await failure(tx.transaction?.(async (inner) => {
666
- await inner.insert(context, dropped);
667
- throw new Error("inner");
668
- }) ?? Promise.resolve());
669
- expect(error.message).toBe("inner");
670
- expect(ids(await tx.findMany(context))).toEqual([outer.id, kept.id]);
671
- });
672
- expect(ids(await provider.findMany(context))).toEqual([
673
- outer.id,
674
- kept.id
675
- ]);
676
- });
677
- transactional("rolls the outer back even if an inner one committed", async () => {
678
- const { provider, context, transaction } = await open();
679
- await failure(transaction(async (tx) => {
680
- await tx.transaction?.(async (inner) => {
681
- await inner.insert(context, row());
682
- });
683
- throw new Error("outer");
684
- }));
685
- expect(await provider.count(context)).toBe(0);
686
- });
687
- });
688
- describe("versioning", () => {
689
- const versioned = gated("versioning");
690
- versioned("update writes with the expected version, and throws RepositoryConcurrencyConflict with a stale one", async () => {
691
- const { provider, context, seed } = await fixture();
692
- const data = row({ version: 1 });
693
- await seed(data);
694
- await provider.update(context, { ...data, name: "Bia", version: 2 }, { expectedVersion: 1 });
695
- const stale = await failure(provider.update(context, { ...data, name: "Caio", version: 2 }, { expectedVersion: 1 }));
696
- expect(stale instanceof RepositoryConcurrencyConflict2).toBe(true);
697
- const { details } = stale;
698
- expect([details.id, details.expected]).toEqual([data.id, 1]);
699
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Bia", version: 2 });
700
- });
701
- versioned("two updates with the same expected version: exactly one succeeds", async () => {
702
- const { provider, context, seed } = await fixture();
703
- const data = row({ version: 1 });
704
- await seed(data);
705
- const results = await Promise.allSettled(["Bia", "Caio"].map((name) => provider.update(context, { ...data, name, version: 2 }, { expectedVersion: 1 })));
706
- const rejected = results.filter((result) => result.status === "rejected");
707
- expect(rejected.length).toBe(1);
708
- expect(rejected[0]?.reason instanceof RepositoryConcurrencyConflict2).toBe(true);
709
- const stored = await provider.findOne(context, {
710
- where: { id: data.id }
711
- });
712
- expect(stored?.version).toBe(2);
713
- });
714
- versioned("update without an expected version ignores the stored one", async () => {
715
- const { provider, context, seed } = await fixture();
716
- const data = row({ version: 3 });
717
- await seed(data);
718
- await provider.update(context, { ...data, name: "Bia", version: 1 });
719
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual({ ...data, name: "Bia", version: 1 });
720
- });
721
- versioned("an unknown id is still RepositoryEntityNotFound", async () => {
722
- const { provider, context } = await fixture();
723
- const error = await failure(provider.update(context, row({ version: 2 }), {
724
- expectedVersion: 1
725
- }));
726
- expect(error instanceof RepositoryEntityNotFound2).toBe(true);
727
- });
728
- });
729
- describe("atomic", () => {
730
- const atomic = gated("atomic");
731
- const increment = (provider, ...args) => provider.increment(...args);
732
- atomic("increment adds the deltas, stores set and gives the row", async () => {
733
- const { provider, context, seed } = await fixture();
734
- const data = row({ visits: 2, age: 30 });
735
- await seed(data);
736
- const stored = await increment(provider, context, data.id, { visits: 3, age: -1, n: 5 }, { set: { name: "Bia" } });
737
- const expected = { ...data, visits: 5, age: 29, n: 5, name: "Bia" };
738
- expect(stored).toEqual(expected);
739
- expect(await provider.findOne(context, { where: { id: data.id } })).toEqual(expected);
740
- });
741
- atomic("N concurrent increments sum to N", async () => {
742
- const { provider, context, seed } = await fixture();
743
- const data = row({ visits: 0 });
744
- await seed(data);
745
- await Promise.all(Array.from({ length: 25 }, () => increment(provider, context, data.id, { visits: 1 })));
746
- const stored = await provider.findOne(context, {
747
- where: { id: data.id }
748
- });
749
- expect(stored?.visits).toBe(25);
750
- });
751
- atomic("a missing id is RepositoryEntityNotFound without create, and inserted with it", async () => {
752
- const { provider, context } = await fixture();
753
- const data = row({ visits: 1 });
754
- const missing = await failure(increment(provider, context, data.id, { visits: 1 }));
755
- expect(missing instanceof RepositoryEntityNotFound2).toBe(true);
756
- expect(await provider.count(context)).toBe(0);
757
- const created = await increment(provider, context, data.id, { visits: 1 }, { create: data, set: { name: "Bia" } });
758
- expect(created).toEqual(data);
759
- const again = await increment(provider, context, data.id, { visits: 1 }, { create: data });
760
- expect(again.visits).toBe(2);
761
- expect(await provider.count(context)).toBe(1);
762
- });
763
- atomic("concurrent increments with create lose nothing", async () => {
764
- const { provider, context } = await fixture();
765
- const data = row({ visits: 1 });
766
- await Promise.all(Array.from({ length: 10 }, () => increment(provider, context, data.id, { visits: 1 }, { create: data })));
767
- const stored = await provider.findOne(context, {
768
- where: { id: data.id }
769
- });
770
- expect(stored?.visits).toBe(10);
771
- });
772
- atomic("soft-deleted rows are not incremented", async () => {
773
- const { provider, context, seed } = await fixture();
774
- const data = row({ visits: 1, deletedAt: "2024-01-01T00:00:00.000Z" });
775
- await seed(data);
776
- const error = await failure(increment(provider, context, data.id, { visits: 1 }));
777
- expect(error instanceof RepositoryEntityNotFound2).toBe(true);
778
- const stored = await provider.findOne(context, {
779
- where: { id: data.id },
780
- withDeleted: true
781
- });
782
- expect(stored?.visits).toBe(1);
783
- });
784
- atomic("an increment of version makes an update with the old one conflict", async () => {
785
- const { provider, context, seed } = await fixture();
786
- if ((options.versioning ?? provider.capabilities?.versioning) !== true)
787
- return;
788
- const data = row({ visits: 0, version: 1 });
789
- await seed(data);
790
- const stored = await increment(provider, context, data.id, {
791
- visits: 1,
792
- version: 1
793
- });
794
- expect(stored.version).toBe(2);
795
- const error = await failure(provider.update(context, { ...data, name: "Bia", version: 2 }, { expectedVersion: 1 }));
796
- expect(error instanceof RepositoryConcurrencyConflict2).toBe(true);
797
- expect((await provider.findOne(context, { where: { id: data.id } }))?.visits).toBe(1);
798
- });
799
- atomic("an expired row counts as absent: not found, or replaced by create", async () => {
800
- const provider = await makeProvider();
801
- if ((options.expiry ?? provider.capabilities?.expiry) !== true)
802
- return;
803
- const now = "2026-06-01T12:00:00.000Z";
804
- const context = {
805
- ...contextOf(),
806
- expiry: { key: "expiresAt" }
807
- };
808
- const window = row({
809
- visits: 7,
810
- expiresAt: "2026-06-01T11:00:00.000Z"
811
- });
812
- const live = row({
813
- visits: 1,
814
- expiresAt: "2026-06-01T13:00:00.000Z"
815
- });
816
- await provider.insert(context, window);
817
- await provider.insert(context, live);
818
- const missing = await failure(increment(provider, context, window.id, { visits: 1 }, {
819
- now
820
- }));
821
- expect(missing instanceof RepositoryEntityNotFound2).toBe(true);
822
- const fresh = {
823
- id: window.id,
824
- email: window.email,
825
- name: "fresh",
826
- visits: 1,
827
- expiresAt: "2026-06-01T13:00:00.000Z"
828
- };
829
- const created = await increment(provider, context, window.id, { visits: 1 }, { create: fresh, set: { name: "incremented" }, now });
830
- expect(created).toEqual(fresh);
831
- const again = await increment(provider, context, window.id, { visits: 1 }, { create: fresh, set: { name: "incremented" }, now });
832
- expect([again.visits, again.name]).toEqual([2, "incremented"]);
833
- const counted = await increment(provider, context, live.id, { visits: 1 }, { create: { ...live, visits: 100 }, now });
834
- expect(counted.visits).toBe(2);
835
- });
836
- });
837
- describe("expiry", () => {
838
- const expiring = gated("expiry");
839
- const NOW = "2026-06-01T12:00:00.000Z";
840
- const seeded = async () => {
841
- const provider = await makeProvider();
842
- const context = {
843
- ...contextOf(),
844
- expiry: { key: "expiresAt" }
845
- };
846
- const [past, edge, never, future] = [
847
- row({ n: 1, expiresAt: "2026-05-01T00:00:00.000Z" }),
848
- row({ n: 2, expiresAt: NOW }),
849
- row({ n: 3 }),
850
- row({ n: 4, expiresAt: "2026-07-01T00:00:00.000Z" })
851
- ];
852
- for (const data of [past, edge, never, future])
853
- await provider.insert(context, data);
854
- return { provider, context, past, edge, never, future };
855
- };
856
- expiring("reads leave out rows whose date is at or before now", async () => {
857
- const { provider, context, past, edge, never, future } = await seeded();
858
- const query = { now: NOW };
859
- expect(ids(await provider.findMany(context, query))).toEqual([
860
- never.id,
861
- future.id
862
- ]);
863
- expect(await provider.count(context, query)).toBe(2);
864
- expect(await provider.findOne(context, {
865
- ...query,
866
- where: { id: past.id }
867
- })).toBe(undefined);
868
- expect(await provider.findOne(context, {
869
- ...query,
870
- where: { id: edge.id }
871
- })).toBe(undefined);
872
- expect((await provider.findOne(context, {
873
- ...query,
874
- where: { id: future.id }
875
- }))?.id).toBe(future.id);
876
- expect(ids(await provider.findMany(context, {
877
- ...query,
878
- where: { id: { in: [past.id, never.id] } }
879
- }))).toEqual([never.id]);
880
- });
881
- expiring("a row without a date never expires", async () => {
882
- const { provider, context, never } = await seeded();
883
- const later = { now: "2999-01-01T00:00:00.000Z" };
884
- expect(ids(await provider.findMany(context, later))).toEqual([
885
- never.id
886
- ]);
887
- expect(await provider.count(context, later)).toBe(1);
888
- });
889
- expiring("withExpired gives the expired rows too", async () => {
890
- const { provider, context, past, edge, never, future } = await seeded();
891
- const query = { now: NOW, withExpired: true };
892
- expect(ids(await provider.findMany(context, query))).toEqual([
893
- past.id,
894
- edge.id,
895
- never.id,
896
- future.id
897
- ]);
898
- expect(await provider.count(context, query)).toBe(4);
899
- expect((await provider.findOne(context, {
900
- ...query,
901
- where: { id: past.id }
902
- }))?.id).toBe(past.id);
903
- });
904
- expiring("leaves expired rows out of a read after a position", async () => {
905
- const enabled = options.keyset ?? (await makeProvider()).capabilities?.keyset;
906
- if (enabled !== true)
907
- return;
908
- const { provider, context, past, edge, never, future } = await seeded();
909
- const orderBy = [{ key: "n" }, { key: "id" }];
910
- expect(ids(await provider.findMany(context, {
911
- now: NOW,
912
- orderBy,
913
- after: [0, ""]
914
- }))).toEqual([never.id, future.id]);
915
- expect(ids(await provider.findMany(context, {
916
- now: NOW,
917
- orderBy,
918
- after: [1, past.id],
919
- withExpired: true
920
- }))).toEqual([edge.id, never.id, future.id]);
921
- });
922
- expiring("unique keys still count expired rows", async () => {
923
- const { provider, context, past } = await seeded();
924
- const error = await failure(provider.insert(context, row({ email: past.email })));
925
- expect(error instanceof RepositoryUniqueViolation2).toBe(true);
926
- expect(error.details.key).toBe("email");
927
- expect(await provider.count(context, { now: NOW, withExpired: true })).toBe(4);
928
- });
929
- expiring("an entity that does not expire ignores now", async () => {
930
- const { provider, context, seed } = await fixture();
931
- const data = row({ expiresAt: "2000-01-01T00:00:00.000Z" });
932
- await seed(data);
933
- expect(ids(await provider.findMany(context, { now: NOW }))).toEqual([
934
- data.id
935
- ]);
936
- expect(await provider.count(context, { now: NOW })).toBe(1);
937
- });
938
- });
939
- });
940
- }
941
- export {
942
- memoryPublisher,
943
- providerContract
944
- };
1
+ export { httpProviderContract } from "./http-provider-contract.js";
2
+ export { memoryPublisher } from "./memory-publisher.js";
3
+ export { providerContract } from "./provider-contract.js";