@jskit-ai/crud-core 0.2.14 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/locales/en.json +1 -11
- package/package.json +19 -19
- package/patterns/{json-api-resource-package → crud-feature-package}/PATTERN.md +69 -37
- package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/package.json +5 -4
- package/patterns/crud-feature-package/example/packages/books/src/server/BooksFeature.js +13 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/bookResource.js +74 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/index.js +1 -0
- package/src/server/createHooksToCollectChildren.js +56 -34
- package/src/server/crudContracts.js +54 -0
- package/src/server/defineCrudFeature.js +509 -0
- package/src/server/jsonApiModule/actions.js +44 -30
- package/src/server/jsonApiModule/routes.js +2 -2
- package/src/server/jsonApiModule/service.js +213 -32
- package/src/server/jsonApiResourceContract.js +41 -1
- package/src/server/listQueryValidators.js +1 -1
- package/src/server/removedApi.js +87 -0
- package/src/server/types.js +26 -50
- package/test/assistantActionConformance.test.js +20 -21
- package/test/assistantPagination.integration.test.js +32 -42
- package/test/createHooksToCollectChildren.test.js +61 -16
- package/test/crudFeaturePackagePattern.test.js +50 -0
- package/test/crudOnHandles.test.js +454 -0
- package/test/{defineCrudJsonApiFeature.test.js → defineCrudFeature.test.js} +444 -141
- package/test/errorCodes.test.js +11 -52
- package/test/hiddenFields.test.js +62 -0
- package/test/listQueryValidators.test.js +1 -1
- package/test/routeContracts.test.js +1 -1
- package/test/support/crudJsonApiActionOptions.js +3 -3
- package/test/unservedCrudAutomation.test.js +27 -22
- package/patterns/json-api-resource-package/example/packages/books/src/server/BooksFeature.js +0 -13
- package/patterns/json-api-resource-package/example/packages/books/src/shared/bookResource.js +0 -83
- package/patterns/json-api-resource-package/example/packages/books/src/shared/index.js +0 -1
- package/src/server/createCrudServiceFromResource.js +0 -63
- package/src/server/defineCrudJsonApiFeature.js +0 -346
- package/src/server/fieldAccess.js +0 -298
- package/src/server/jsonApiModule/repository.js +0 -355
- package/src/server/lookupPathSupport.js +0 -58
- package/src/server/lookups.js +0 -40
- package/src/server/repositorySupport.js +0 -528
- package/src/server/resourceRuntime/compiledRuntime.js +0 -284
- package/src/server/resourceRuntime/index.js +0 -659
- package/src/server/resourceRuntime/listControls.js +0 -333
- package/src/server/resourceRuntime/lookupHydration.js +0 -626
- package/src/server/resourceRuntime/operationStages.js +0 -140
- package/src/server/resourceRuntime/recordValidation.js +0 -65
- package/src/server/serviceMethods.js +0 -149
- package/test/createCrudServiceFromResource.test.js +0 -323
- package/test/fieldAccess.test.js +0 -86
- package/test/jsonApiRepository.test.js +0 -333
- package/test/jsonApiRepositoryDataCosts.test.js +0 -183
- package/test/jsonApiRepositoryPageLimits.test.js +0 -158
- package/test/jsonApiRepositoryRowLocks.test.js +0 -137
- package/test/jsonApiResourcePackagePattern.test.js +0 -20
- package/test/lookups.test.js +0 -111
- package/test/repositorySupport.test.js +0 -767
- package/test/resourceRuntime.test.js +0 -1014
- package/test/serviceMethods.test.js +0 -139
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/migrations/20260815000000_books.cjs +0 -0
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/src/server/types.js +0 -0
|
@@ -1,1014 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createSchema } from "json-rest-schema";
|
|
4
|
-
import { RECORD_ID_PATTERN } from "@jskit-ai/kernel/shared/validators";
|
|
5
|
-
import { createCrudResourceRuntime } from "../src/server/resourceRuntime/index.js";
|
|
6
|
-
|
|
7
|
-
const recordIdSchema = Object.freeze({
|
|
8
|
-
type: "string",
|
|
9
|
-
minLength: 1,
|
|
10
|
-
pattern: RECORD_ID_PATTERN
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const nullableRecordIdSchema = Object.freeze({
|
|
14
|
-
...recordIdSchema,
|
|
15
|
-
nullable: true
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
function createKnexDouble(
|
|
19
|
-
rows = [],
|
|
20
|
-
{
|
|
21
|
-
insertResult = [1],
|
|
22
|
-
updateResult = 1,
|
|
23
|
-
deleteResult = 1
|
|
24
|
-
} = {}
|
|
25
|
-
) {
|
|
26
|
-
const calls = [];
|
|
27
|
-
const state = {
|
|
28
|
-
rows: Array.isArray(rows) ? rows : [],
|
|
29
|
-
insertPayloads: [],
|
|
30
|
-
updatePayloads: [],
|
|
31
|
-
deleteCount: 0
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
function createQueryBuilder() {
|
|
35
|
-
let firstMode = false;
|
|
36
|
-
const whereGroup = {
|
|
37
|
-
where(...args) {
|
|
38
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
39
|
-
calls.push(["innerWhereCallback"]);
|
|
40
|
-
args[0](whereGroup);
|
|
41
|
-
return whereGroup;
|
|
42
|
-
}
|
|
43
|
-
calls.push(["where", ...args]);
|
|
44
|
-
return whereGroup;
|
|
45
|
-
},
|
|
46
|
-
orWhere(...args) {
|
|
47
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
48
|
-
calls.push(["innerOrWhereCallback"]);
|
|
49
|
-
args[0](whereGroup);
|
|
50
|
-
return whereGroup;
|
|
51
|
-
}
|
|
52
|
-
calls.push(["orWhere", ...args]);
|
|
53
|
-
return whereGroup;
|
|
54
|
-
},
|
|
55
|
-
whereNull(...args) {
|
|
56
|
-
calls.push(["whereNull", ...args]);
|
|
57
|
-
return whereGroup;
|
|
58
|
-
},
|
|
59
|
-
orWhereNull(...args) {
|
|
60
|
-
calls.push(["orWhereNull", ...args]);
|
|
61
|
-
return whereGroup;
|
|
62
|
-
},
|
|
63
|
-
whereNotNull(...args) {
|
|
64
|
-
calls.push(["whereNotNull", ...args]);
|
|
65
|
-
return whereGroup;
|
|
66
|
-
},
|
|
67
|
-
orWhereNotNull(...args) {
|
|
68
|
-
calls.push(["orWhereNotNull", ...args]);
|
|
69
|
-
return whereGroup;
|
|
70
|
-
},
|
|
71
|
-
whereRaw(...args) {
|
|
72
|
-
calls.push(["whereRaw", ...args]);
|
|
73
|
-
return whereGroup;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const query = {
|
|
78
|
-
select(...args) {
|
|
79
|
-
calls.push(["select", ...args]);
|
|
80
|
-
return query;
|
|
81
|
-
},
|
|
82
|
-
where(...args) {
|
|
83
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
84
|
-
calls.push(["whereCallback"]);
|
|
85
|
-
args[0](whereGroup);
|
|
86
|
-
return query;
|
|
87
|
-
}
|
|
88
|
-
calls.push(["where", ...args]);
|
|
89
|
-
return query;
|
|
90
|
-
},
|
|
91
|
-
orWhere(...args) {
|
|
92
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
93
|
-
calls.push(["orWhereCallback"]);
|
|
94
|
-
args[0](whereGroup);
|
|
95
|
-
return query;
|
|
96
|
-
}
|
|
97
|
-
calls.push(["orWhere", ...args]);
|
|
98
|
-
return query;
|
|
99
|
-
},
|
|
100
|
-
whereNull(...args) {
|
|
101
|
-
calls.push(["whereNull", ...args]);
|
|
102
|
-
return query;
|
|
103
|
-
},
|
|
104
|
-
orWhereNull(...args) {
|
|
105
|
-
calls.push(["orWhereNull", ...args]);
|
|
106
|
-
return query;
|
|
107
|
-
},
|
|
108
|
-
whereNotNull(...args) {
|
|
109
|
-
calls.push(["whereNotNull", ...args]);
|
|
110
|
-
return query;
|
|
111
|
-
},
|
|
112
|
-
orWhereNotNull(...args) {
|
|
113
|
-
calls.push(["orWhereNotNull", ...args]);
|
|
114
|
-
return query;
|
|
115
|
-
},
|
|
116
|
-
whereRaw(...args) {
|
|
117
|
-
calls.push(["whereRaw", ...args]);
|
|
118
|
-
return query;
|
|
119
|
-
},
|
|
120
|
-
orderBy(...args) {
|
|
121
|
-
calls.push(["orderBy", ...args]);
|
|
122
|
-
return query;
|
|
123
|
-
},
|
|
124
|
-
orderByRaw(...args) {
|
|
125
|
-
calls.push(["orderByRaw", ...args]);
|
|
126
|
-
return query;
|
|
127
|
-
},
|
|
128
|
-
clearOrder() {
|
|
129
|
-
calls.push(["clearOrder"]);
|
|
130
|
-
return query;
|
|
131
|
-
},
|
|
132
|
-
clear(...args) {
|
|
133
|
-
calls.push(["clear", ...args]);
|
|
134
|
-
return query;
|
|
135
|
-
},
|
|
136
|
-
limit(value) {
|
|
137
|
-
calls.push(["limit", value]);
|
|
138
|
-
return query;
|
|
139
|
-
},
|
|
140
|
-
modify(callback) {
|
|
141
|
-
calls.push(["modify"]);
|
|
142
|
-
callback(query);
|
|
143
|
-
return query;
|
|
144
|
-
},
|
|
145
|
-
whereIn(...args) {
|
|
146
|
-
calls.push(["whereIn", ...args]);
|
|
147
|
-
return query;
|
|
148
|
-
},
|
|
149
|
-
forUpdate() {
|
|
150
|
-
calls.push(["forUpdate"]);
|
|
151
|
-
return query;
|
|
152
|
-
},
|
|
153
|
-
first() {
|
|
154
|
-
calls.push(["first"]);
|
|
155
|
-
firstMode = true;
|
|
156
|
-
return query;
|
|
157
|
-
},
|
|
158
|
-
insert(payload) {
|
|
159
|
-
calls.push(["insert", payload]);
|
|
160
|
-
state.insertPayloads.push(payload);
|
|
161
|
-
return Promise.resolve(insertResult);
|
|
162
|
-
},
|
|
163
|
-
update(payload) {
|
|
164
|
-
calls.push(["update", payload]);
|
|
165
|
-
state.updatePayloads.push(payload);
|
|
166
|
-
return Promise.resolve(updateResult);
|
|
167
|
-
},
|
|
168
|
-
delete() {
|
|
169
|
-
calls.push(["delete"]);
|
|
170
|
-
state.deleteCount += 1;
|
|
171
|
-
return Promise.resolve(deleteResult);
|
|
172
|
-
},
|
|
173
|
-
then(resolve, reject) {
|
|
174
|
-
const payload = firstMode ? state.rows[0] || null : state.rows;
|
|
175
|
-
return Promise.resolve(payload).then(resolve, reject);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
return query;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const knex = Object.assign((tableName) => {
|
|
183
|
-
calls.push(["table", tableName]);
|
|
184
|
-
return createQueryBuilder();
|
|
185
|
-
}, {
|
|
186
|
-
async transaction(work) {
|
|
187
|
-
return work({ trxId: "trx-1" });
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
knex,
|
|
193
|
-
calls,
|
|
194
|
-
state
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function createResourceFixture() {
|
|
199
|
-
return {
|
|
200
|
-
namespace: "contacts",
|
|
201
|
-
tableName: "contacts_table",
|
|
202
|
-
idColumn: "contact_id",
|
|
203
|
-
operations: {
|
|
204
|
-
view: {
|
|
205
|
-
output: {
|
|
206
|
-
schema: createSchema({
|
|
207
|
-
id: {
|
|
208
|
-
...recordIdSchema,
|
|
209
|
-
required: true,
|
|
210
|
-
actualField: "contact_id"
|
|
211
|
-
},
|
|
212
|
-
firstName: {
|
|
213
|
-
type: "string",
|
|
214
|
-
required: true
|
|
215
|
-
}
|
|
216
|
-
}),
|
|
217
|
-
mode: "replace"
|
|
218
|
-
}
|
|
219
|
-
},
|
|
220
|
-
create: {
|
|
221
|
-
body: {
|
|
222
|
-
schema: createSchema({
|
|
223
|
-
firstName: {
|
|
224
|
-
type: "string"
|
|
225
|
-
}
|
|
226
|
-
}),
|
|
227
|
-
mode: "create"
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function createLookupResourceFixture() {
|
|
235
|
-
return {
|
|
236
|
-
namespace: "contacts",
|
|
237
|
-
tableName: "contacts_table",
|
|
238
|
-
idColumn: "contact_id",
|
|
239
|
-
operations: {
|
|
240
|
-
view: {
|
|
241
|
-
output: {
|
|
242
|
-
schema: createSchema({
|
|
243
|
-
id: {
|
|
244
|
-
...recordIdSchema,
|
|
245
|
-
required: true,
|
|
246
|
-
actualField: "contact_id"
|
|
247
|
-
},
|
|
248
|
-
firstName: {
|
|
249
|
-
type: "string",
|
|
250
|
-
required: true
|
|
251
|
-
},
|
|
252
|
-
primaryVetId: {
|
|
253
|
-
...recordIdSchema,
|
|
254
|
-
actualField: "primary_vet_id",
|
|
255
|
-
relation: {
|
|
256
|
-
kind: "lookup",
|
|
257
|
-
namespace: "vets",
|
|
258
|
-
valueKey: "id"
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
secondaryVetId: {
|
|
262
|
-
...nullableRecordIdSchema,
|
|
263
|
-
actualField: "secondary_vet_id",
|
|
264
|
-
relation: {
|
|
265
|
-
kind: "lookup",
|
|
266
|
-
namespace: "vets",
|
|
267
|
-
valueKey: "id"
|
|
268
|
-
}
|
|
269
|
-
},
|
|
270
|
-
lookups: {
|
|
271
|
-
type: "object"
|
|
272
|
-
}
|
|
273
|
-
}),
|
|
274
|
-
mode: "replace"
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
create: {
|
|
278
|
-
body: {
|
|
279
|
-
schema: createSchema({
|
|
280
|
-
firstName: {
|
|
281
|
-
type: "string"
|
|
282
|
-
}
|
|
283
|
-
}),
|
|
284
|
-
mode: "create"
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function createLeafLookupResourceFixture() {
|
|
292
|
-
return {
|
|
293
|
-
namespace: "users",
|
|
294
|
-
tableName: "users_table",
|
|
295
|
-
idColumn: "user_id",
|
|
296
|
-
operations: {
|
|
297
|
-
view: {
|
|
298
|
-
output: {
|
|
299
|
-
schema: createSchema({
|
|
300
|
-
id: {
|
|
301
|
-
...recordIdSchema,
|
|
302
|
-
required: true,
|
|
303
|
-
actualField: "user_id"
|
|
304
|
-
},
|
|
305
|
-
name: {
|
|
306
|
-
type: "string",
|
|
307
|
-
required: true,
|
|
308
|
-
actualField: "display_name"
|
|
309
|
-
}
|
|
310
|
-
}),
|
|
311
|
-
mode: "replace"
|
|
312
|
-
}
|
|
313
|
-
},
|
|
314
|
-
create: {
|
|
315
|
-
body: {
|
|
316
|
-
schema: createSchema({}),
|
|
317
|
-
mode: "create"
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
function createNormalizedWriteResourceFixture() {
|
|
325
|
-
return {
|
|
326
|
-
namespace: "contacts",
|
|
327
|
-
tableName: "contacts_table",
|
|
328
|
-
idColumn: "contact_id",
|
|
329
|
-
operations: {
|
|
330
|
-
view: {
|
|
331
|
-
output: {
|
|
332
|
-
schema: createSchema({
|
|
333
|
-
id: {
|
|
334
|
-
...recordIdSchema,
|
|
335
|
-
required: true,
|
|
336
|
-
actualField: "contact_id"
|
|
337
|
-
},
|
|
338
|
-
firstName: {
|
|
339
|
-
type: "string",
|
|
340
|
-
required: true,
|
|
341
|
-
actualField: "first_name"
|
|
342
|
-
}
|
|
343
|
-
}),
|
|
344
|
-
mode: "replace"
|
|
345
|
-
}
|
|
346
|
-
},
|
|
347
|
-
create: {
|
|
348
|
-
body: {
|
|
349
|
-
schema: createSchema({
|
|
350
|
-
firstName: {
|
|
351
|
-
type: "string",
|
|
352
|
-
required: true,
|
|
353
|
-
minLength: 1,
|
|
354
|
-
actualField: "first_name"
|
|
355
|
-
}
|
|
356
|
-
}),
|
|
357
|
-
mode: "create"
|
|
358
|
-
}
|
|
359
|
-
},
|
|
360
|
-
patch: {
|
|
361
|
-
body: {
|
|
362
|
-
schema: createSchema({
|
|
363
|
-
firstName: {
|
|
364
|
-
type: "string",
|
|
365
|
-
required: false,
|
|
366
|
-
minLength: 1,
|
|
367
|
-
actualField: "first_name"
|
|
368
|
-
},
|
|
369
|
-
lastSeenAt: {
|
|
370
|
-
type: "string",
|
|
371
|
-
required: false,
|
|
372
|
-
actualField: "last_seen_at"
|
|
373
|
-
}
|
|
374
|
-
}),
|
|
375
|
-
mode: "patch"
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
function createVirtualProjectionResourceFixture() {
|
|
383
|
-
return {
|
|
384
|
-
namespace: "receivals",
|
|
385
|
-
tableName: "receivals_table",
|
|
386
|
-
idColumn: "receival_id",
|
|
387
|
-
operations: {
|
|
388
|
-
view: {
|
|
389
|
-
output: {
|
|
390
|
-
schema: createSchema({
|
|
391
|
-
id: {
|
|
392
|
-
...recordIdSchema,
|
|
393
|
-
required: true,
|
|
394
|
-
actualField: "receival_id"
|
|
395
|
-
},
|
|
396
|
-
firstName: {
|
|
397
|
-
type: "string",
|
|
398
|
-
required: true
|
|
399
|
-
},
|
|
400
|
-
remainingBatchWeight: {
|
|
401
|
-
type: "number",
|
|
402
|
-
storage: {
|
|
403
|
-
virtual: true
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}),
|
|
407
|
-
mode: "replace"
|
|
408
|
-
}
|
|
409
|
-
},
|
|
410
|
-
create: {
|
|
411
|
-
body: {
|
|
412
|
-
schema: createSchema({
|
|
413
|
-
firstName: {
|
|
414
|
-
type: "string"
|
|
415
|
-
}
|
|
416
|
-
}),
|
|
417
|
-
mode: "create"
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
test("createCrudResourceRuntime requires table metadata from resource", () => {
|
|
425
|
-
const { knex } = createKnexDouble();
|
|
426
|
-
assert.throws(
|
|
427
|
-
() =>
|
|
428
|
-
createCrudResourceRuntime({
|
|
429
|
-
operations: {
|
|
430
|
-
view: {
|
|
431
|
-
output: {
|
|
432
|
-
schema: createSchema({
|
|
433
|
-
id: {
|
|
434
|
-
...recordIdSchema,
|
|
435
|
-
required: true
|
|
436
|
-
}
|
|
437
|
-
}),
|
|
438
|
-
mode: "replace"
|
|
439
|
-
}
|
|
440
|
-
},
|
|
441
|
-
create: {
|
|
442
|
-
body: {
|
|
443
|
-
schema: createSchema({}),
|
|
444
|
-
mode: "create"
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}, knex),
|
|
449
|
-
/requires resource\.tableName or resource\.namespace/
|
|
450
|
-
);
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
test("createCrudResourceRuntime binds methods directly and exposes withTransaction", async () => {
|
|
454
|
-
const { knex } = createKnexDouble([
|
|
455
|
-
{
|
|
456
|
-
contact_id: 3,
|
|
457
|
-
first_name: "Tony"
|
|
458
|
-
}
|
|
459
|
-
]);
|
|
460
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex);
|
|
461
|
-
|
|
462
|
-
assert.equal(typeof repository.list, "function");
|
|
463
|
-
assert.equal(typeof repository.findById, "function");
|
|
464
|
-
assert.equal(typeof repository.listByIds, "function");
|
|
465
|
-
assert.equal(typeof repository.listByForeignIds, "function");
|
|
466
|
-
assert.equal(typeof repository.create, "function");
|
|
467
|
-
assert.equal(typeof repository.updateById, "function");
|
|
468
|
-
assert.equal(typeof repository.deleteById, "function");
|
|
469
|
-
assert.equal(typeof repository.withTransaction, "function");
|
|
470
|
-
|
|
471
|
-
const transactionResult = await repository.withTransaction(async (trx) => ({ id: trx.trxId }));
|
|
472
|
-
assert.deepEqual(transactionResult, { id: "trx-1" });
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
test("list uses resource table and id defaults", async () => {
|
|
476
|
-
const { knex, calls } = createKnexDouble([
|
|
477
|
-
{
|
|
478
|
-
contact_id: 3,
|
|
479
|
-
first_name: "Tony"
|
|
480
|
-
}
|
|
481
|
-
]);
|
|
482
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex);
|
|
483
|
-
|
|
484
|
-
const result = await repository.list({
|
|
485
|
-
cursor: "2",
|
|
486
|
-
q: "to"
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
assert.deepEqual(result, {
|
|
490
|
-
items: [
|
|
491
|
-
{
|
|
492
|
-
id: "3",
|
|
493
|
-
firstName: "Tony"
|
|
494
|
-
}
|
|
495
|
-
],
|
|
496
|
-
nextCursor: null
|
|
497
|
-
});
|
|
498
|
-
assert.equal(calls[0][1], "contacts_table");
|
|
499
|
-
assert.ok(calls.some((call) => call[0] === "where" && call[1] === "contact_id" && call[2] === ">" && call[3] === "2"));
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
test("list converts database temporal values before strict resource validation", async () => {
|
|
503
|
-
const resource = {
|
|
504
|
-
namespace: "appointments",
|
|
505
|
-
tableName: "appointments",
|
|
506
|
-
operations: {
|
|
507
|
-
view: {
|
|
508
|
-
output: {
|
|
509
|
-
schema: createSchema({
|
|
510
|
-
id: {
|
|
511
|
-
...recordIdSchema,
|
|
512
|
-
required: true
|
|
513
|
-
},
|
|
514
|
-
serviceDate: {
|
|
515
|
-
type: "date",
|
|
516
|
-
required: true
|
|
517
|
-
},
|
|
518
|
-
openingTime: {
|
|
519
|
-
type: "time",
|
|
520
|
-
required: true
|
|
521
|
-
},
|
|
522
|
-
scheduledAt: {
|
|
523
|
-
type: "dateTime",
|
|
524
|
-
required: true
|
|
525
|
-
}
|
|
526
|
-
}),
|
|
527
|
-
mode: "replace"
|
|
528
|
-
}
|
|
529
|
-
},
|
|
530
|
-
create: {
|
|
531
|
-
body: {
|
|
532
|
-
schema: createSchema({}),
|
|
533
|
-
mode: "create"
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
const { knex } = createKnexDouble([
|
|
539
|
-
{
|
|
540
|
-
id: 3,
|
|
541
|
-
service_date: new Date("2026-08-13T23:59:58.123Z"),
|
|
542
|
-
opening_time: "07:08:09.123456",
|
|
543
|
-
scheduled_at: "2026-08-13 07:08:09.123456"
|
|
544
|
-
}
|
|
545
|
-
]);
|
|
546
|
-
const repository = createCrudResourceRuntime(resource, knex);
|
|
547
|
-
|
|
548
|
-
const result = await repository.list();
|
|
549
|
-
|
|
550
|
-
assert.deepEqual(result.items, [
|
|
551
|
-
{
|
|
552
|
-
id: "3",
|
|
553
|
-
serviceDate: "2026-08-13",
|
|
554
|
-
openingTime: "07:08:09.123456",
|
|
555
|
-
scheduledAt: "2026-08-13T07:08:09.123456Z"
|
|
556
|
-
}
|
|
557
|
-
]);
|
|
558
|
-
});
|
|
559
|
-
|
|
560
|
-
test("list respects bound list config", async () => {
|
|
561
|
-
const { knex, calls } = createKnexDouble([
|
|
562
|
-
{ contact_id: 3, first_name: "Tony" },
|
|
563
|
-
{ contact_id: 4, first_name: "Tom" },
|
|
564
|
-
{ contact_id: 5, first_name: "Toby" }
|
|
565
|
-
]);
|
|
566
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex, {
|
|
567
|
-
list: {
|
|
568
|
-
defaultLimit: 1,
|
|
569
|
-
maxLimit: 2,
|
|
570
|
-
searchColumns: ["first_name"]
|
|
571
|
-
}
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
await repository.list({
|
|
575
|
-
q: "to",
|
|
576
|
-
limit: 99
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
assert.ok(calls.some((call) => call[0] === "where" && call[1] === "first_name" && call[2] === "like" && call[3] === "%to%"));
|
|
580
|
-
assert.ok(calls.some((call) => call[0] === "limit" && call[1] === 3));
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
test("list supports configured virtual projections", async () => {
|
|
584
|
-
const { knex, calls } = createKnexDouble([
|
|
585
|
-
{
|
|
586
|
-
receival_id: 3,
|
|
587
|
-
first_name: "Tony",
|
|
588
|
-
remaining_batch_weight: 12
|
|
589
|
-
}
|
|
590
|
-
]);
|
|
591
|
-
const repository = createCrudResourceRuntime(createVirtualProjectionResourceFixture(), knex, {
|
|
592
|
-
virtualFields: {
|
|
593
|
-
remainingBatchWeight: {
|
|
594
|
-
applyProjection(dbQuery) {
|
|
595
|
-
dbQuery.select("remaining_batch_weight");
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
const result = await repository.list();
|
|
602
|
-
assert.equal(result.items[0].remainingBatchWeight, 12);
|
|
603
|
-
assert.ok(calls.some((call) => call[0] === "select" && call.includes("remaining_batch_weight")));
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
test("list hydrates lookups from callOptions.include", async () => {
|
|
607
|
-
const { knex } = createKnexDouble([
|
|
608
|
-
{
|
|
609
|
-
contact_id: 3,
|
|
610
|
-
first_name: "Tony",
|
|
611
|
-
primary_vet_id: 10,
|
|
612
|
-
secondary_vet_id: 12
|
|
613
|
-
}
|
|
614
|
-
]);
|
|
615
|
-
|
|
616
|
-
const lookupCalls = [];
|
|
617
|
-
const repository = createCrudResourceRuntime(createLookupResourceFixture(), knex, {
|
|
618
|
-
resolveLookup() {
|
|
619
|
-
return {
|
|
620
|
-
async listByIds(ids = [], options = {}) {
|
|
621
|
-
lookupCalls.push({
|
|
622
|
-
ids,
|
|
623
|
-
options
|
|
624
|
-
});
|
|
625
|
-
return [
|
|
626
|
-
{ id: "10", name: "Vet A" },
|
|
627
|
-
{ id: "12", name: "Vet B" }
|
|
628
|
-
];
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
}
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
const result = await repository.list({}, {
|
|
635
|
-
include: "*"
|
|
636
|
-
});
|
|
637
|
-
|
|
638
|
-
assert.equal(lookupCalls.length, 1);
|
|
639
|
-
assert.deepEqual(lookupCalls[0].ids, ["10", "12"]);
|
|
640
|
-
assert.equal(lookupCalls[0].options.include, "*");
|
|
641
|
-
assert.deepEqual(result.items[0].lookups, {
|
|
642
|
-
primaryVetId: { id: "10", name: "Vet A" },
|
|
643
|
-
secondaryVetId: { id: "12", name: "Vet B" }
|
|
644
|
-
});
|
|
645
|
-
});
|
|
646
|
-
|
|
647
|
-
test("list forwards nested include paths to child lookup repositories", async () => {
|
|
648
|
-
const { knex } = createKnexDouble([
|
|
649
|
-
{
|
|
650
|
-
contact_id: 3,
|
|
651
|
-
first_name: "Tony",
|
|
652
|
-
primary_vet_id: 10,
|
|
653
|
-
secondary_vet_id: 12
|
|
654
|
-
}
|
|
655
|
-
]);
|
|
656
|
-
|
|
657
|
-
const lookupCalls = [];
|
|
658
|
-
const repository = createCrudResourceRuntime(createLookupResourceFixture(), knex, {
|
|
659
|
-
resolveLookup() {
|
|
660
|
-
return {
|
|
661
|
-
async listByIds(ids = [], options = {}) {
|
|
662
|
-
lookupCalls.push({
|
|
663
|
-
ids,
|
|
664
|
-
options
|
|
665
|
-
});
|
|
666
|
-
return [{ id: "10", name: "Vet A" }, { id: "12", name: "Vet B" }];
|
|
667
|
-
}
|
|
668
|
-
};
|
|
669
|
-
}
|
|
670
|
-
});
|
|
671
|
-
|
|
672
|
-
await repository.list({}, {
|
|
673
|
-
include: "primaryVetId.vetTypeId"
|
|
674
|
-
});
|
|
675
|
-
|
|
676
|
-
assert.equal(lookupCalls.length, 1);
|
|
677
|
-
assert.equal(lookupCalls[0].options.include, "vetTypeId");
|
|
678
|
-
});
|
|
679
|
-
|
|
680
|
-
test("list wildcard hydration does not fail when child lookup resources declare no nested lookups", async () => {
|
|
681
|
-
const { knex } = createKnexDouble([
|
|
682
|
-
{
|
|
683
|
-
contact_id: 3,
|
|
684
|
-
first_name: "Tony",
|
|
685
|
-
primary_vet_id: 10,
|
|
686
|
-
secondary_vet_id: 12
|
|
687
|
-
}
|
|
688
|
-
]);
|
|
689
|
-
const { knex: childKnex } = createKnexDouble([
|
|
690
|
-
{
|
|
691
|
-
user_id: 10,
|
|
692
|
-
display_name: "Vet A"
|
|
693
|
-
},
|
|
694
|
-
{
|
|
695
|
-
user_id: 12,
|
|
696
|
-
display_name: "Vet B"
|
|
697
|
-
}
|
|
698
|
-
]);
|
|
699
|
-
|
|
700
|
-
const childRepository = createCrudResourceRuntime(createLeafLookupResourceFixture(), childKnex, {
|
|
701
|
-
context: "users repository"
|
|
702
|
-
});
|
|
703
|
-
const repository = createCrudResourceRuntime(createLookupResourceFixture(), knex, {
|
|
704
|
-
resolveLookup() {
|
|
705
|
-
return {
|
|
706
|
-
async listByIds(ids = [], options = {}) {
|
|
707
|
-
return childRepository.listByIds(ids, options);
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
const result = await repository.list({}, {
|
|
714
|
-
include: "*"
|
|
715
|
-
});
|
|
716
|
-
|
|
717
|
-
assert.deepEqual(result.items[0].lookups, {
|
|
718
|
-
primaryVetId: { id: "10", name: "Vet A" },
|
|
719
|
-
secondaryVetId: { id: "12", name: "Vet B" }
|
|
720
|
-
});
|
|
721
|
-
});
|
|
722
|
-
|
|
723
|
-
test("operations.read.applyQuery and operations.list.applyQuery run before canonical filters", async () => {
|
|
724
|
-
const { knex, calls } = createKnexDouble([
|
|
725
|
-
{
|
|
726
|
-
contact_id: 7,
|
|
727
|
-
first_name: "Tony"
|
|
728
|
-
}
|
|
729
|
-
]);
|
|
730
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex, {
|
|
731
|
-
operations: {
|
|
732
|
-
read: {
|
|
733
|
-
applyQuery(dbQuery) {
|
|
734
|
-
dbQuery.whereNull("deleted_at");
|
|
735
|
-
return dbQuery;
|
|
736
|
-
}
|
|
737
|
-
},
|
|
738
|
-
list: {
|
|
739
|
-
applyQuery(dbQuery, { query = {} } = {}) {
|
|
740
|
-
if (query.onlyVip === true) {
|
|
741
|
-
dbQuery.where("vip", 1);
|
|
742
|
-
}
|
|
743
|
-
return dbQuery;
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
});
|
|
748
|
-
|
|
749
|
-
await repository.list({
|
|
750
|
-
onlyVip: true
|
|
751
|
-
}, {
|
|
752
|
-
visibilityContext: {
|
|
753
|
-
visibility: "workspace",
|
|
754
|
-
scopeOwnerId: "workspace-1"
|
|
755
|
-
}
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
assert.ok(calls.some((call) => call[0] === "whereNull" && call[1] === "deleted_at"));
|
|
759
|
-
assert.ok(calls.some((call) => call[0] === "where" && call[1] === "vip" && call[2] === 1));
|
|
760
|
-
assert.ok(calls.some((call) => call[0] === "where" && call[1] === "workspace_id" && call[2] === "workspace-1"));
|
|
761
|
-
});
|
|
762
|
-
|
|
763
|
-
test("findById supports forUpdate via callOptions", async () => {
|
|
764
|
-
const { knex, calls } = createKnexDouble([
|
|
765
|
-
{
|
|
766
|
-
contact_id: 7,
|
|
767
|
-
first_name: "Tony"
|
|
768
|
-
}
|
|
769
|
-
]);
|
|
770
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex);
|
|
771
|
-
|
|
772
|
-
await repository.findById("7", {
|
|
773
|
-
trx: knex,
|
|
774
|
-
forUpdate: true
|
|
775
|
-
});
|
|
776
|
-
|
|
777
|
-
assert.ok(calls.some((call) => call[0] === "forUpdate"));
|
|
778
|
-
});
|
|
779
|
-
|
|
780
|
-
test("listByIds supports alternate valueKey and listByForeignIds delegates to it", async () => {
|
|
781
|
-
const resource = {
|
|
782
|
-
...createResourceFixture(),
|
|
783
|
-
operations: {
|
|
784
|
-
...createResourceFixture().operations,
|
|
785
|
-
view: {
|
|
786
|
-
output: {
|
|
787
|
-
schema: createSchema({
|
|
788
|
-
id: {
|
|
789
|
-
...recordIdSchema,
|
|
790
|
-
required: true,
|
|
791
|
-
actualField: "contact_id"
|
|
792
|
-
},
|
|
793
|
-
foreignId: {
|
|
794
|
-
...recordIdSchema,
|
|
795
|
-
actualField: "foreign_id"
|
|
796
|
-
},
|
|
797
|
-
firstName: {
|
|
798
|
-
type: "string",
|
|
799
|
-
required: true
|
|
800
|
-
}
|
|
801
|
-
}),
|
|
802
|
-
mode: "replace"
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
const { knex, calls } = createKnexDouble([
|
|
808
|
-
{
|
|
809
|
-
contact_id: 7,
|
|
810
|
-
foreign_id: 12,
|
|
811
|
-
first_name: "Tony"
|
|
812
|
-
}
|
|
813
|
-
]);
|
|
814
|
-
const repository = createCrudResourceRuntime(resource, knex);
|
|
815
|
-
|
|
816
|
-
const direct = await repository.listByIds(["12"], {
|
|
817
|
-
valueKey: "foreignId"
|
|
818
|
-
});
|
|
819
|
-
const delegated = await repository.listByForeignIds(["12"], "foreignId");
|
|
820
|
-
|
|
821
|
-
assert.equal(direct.length, 1);
|
|
822
|
-
assert.equal(delegated.length, 1);
|
|
823
|
-
assert.ok(calls.some((call) => call[0] === "whereIn" && call[1] === "foreign_id"));
|
|
824
|
-
});
|
|
825
|
-
|
|
826
|
-
test("create uses operations.create.prepareInsertPayload before insert", async () => {
|
|
827
|
-
const { knex, state } = createKnexDouble([
|
|
828
|
-
{
|
|
829
|
-
contact_id: 11,
|
|
830
|
-
first_name: "Tony",
|
|
831
|
-
created_at: "2026-01-01T00:00:00.000Z",
|
|
832
|
-
updated_at: "2026-01-01T00:00:00.000Z"
|
|
833
|
-
}
|
|
834
|
-
], {
|
|
835
|
-
insertResult: [11]
|
|
836
|
-
});
|
|
837
|
-
const resource = {
|
|
838
|
-
namespace: "contacts",
|
|
839
|
-
tableName: "contacts_table",
|
|
840
|
-
idColumn: "contact_id",
|
|
841
|
-
operations: {
|
|
842
|
-
view: {
|
|
843
|
-
output: {
|
|
844
|
-
schema: createSchema({
|
|
845
|
-
id: {
|
|
846
|
-
...recordIdSchema,
|
|
847
|
-
required: true,
|
|
848
|
-
actualField: "contact_id"
|
|
849
|
-
},
|
|
850
|
-
firstName: {
|
|
851
|
-
type: "string",
|
|
852
|
-
required: true
|
|
853
|
-
},
|
|
854
|
-
createdAt: {
|
|
855
|
-
type: "string",
|
|
856
|
-
required: true,
|
|
857
|
-
actualField: "created_at"
|
|
858
|
-
},
|
|
859
|
-
updatedAt: {
|
|
860
|
-
type: "string",
|
|
861
|
-
required: true,
|
|
862
|
-
actualField: "updated_at"
|
|
863
|
-
}
|
|
864
|
-
}),
|
|
865
|
-
mode: "replace"
|
|
866
|
-
}
|
|
867
|
-
},
|
|
868
|
-
create: {
|
|
869
|
-
body: {
|
|
870
|
-
schema: createSchema({
|
|
871
|
-
firstName: {
|
|
872
|
-
type: "string"
|
|
873
|
-
}
|
|
874
|
-
}),
|
|
875
|
-
mode: "create"
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
};
|
|
880
|
-
const repository = createCrudResourceRuntime(resource, knex, {
|
|
881
|
-
operations: {
|
|
882
|
-
create: {
|
|
883
|
-
prepareInsertPayload(insertPayload = {}, context = {}) {
|
|
884
|
-
return {
|
|
885
|
-
...insertPayload,
|
|
886
|
-
first_name: context.payload.firstName.toUpperCase()
|
|
887
|
-
};
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
});
|
|
892
|
-
|
|
893
|
-
await repository.create({
|
|
894
|
-
firstName: "Tony"
|
|
895
|
-
});
|
|
896
|
-
|
|
897
|
-
assert.equal(state.insertPayloads[0].first_name, "TONY");
|
|
898
|
-
});
|
|
899
|
-
|
|
900
|
-
test("create normalizes resource body payloads before insert", async () => {
|
|
901
|
-
const rows = [
|
|
902
|
-
{
|
|
903
|
-
contact_id: 11,
|
|
904
|
-
first_name: "Tony"
|
|
905
|
-
}
|
|
906
|
-
];
|
|
907
|
-
const { knex, state } = createKnexDouble(rows, {
|
|
908
|
-
insertResult: [11]
|
|
909
|
-
});
|
|
910
|
-
const repository = createCrudResourceRuntime(createNormalizedWriteResourceFixture(), knex);
|
|
911
|
-
|
|
912
|
-
await repository.create({
|
|
913
|
-
firstName: " Tony "
|
|
914
|
-
});
|
|
915
|
-
|
|
916
|
-
assert.equal(state.insertPayloads[0].first_name, "Tony");
|
|
917
|
-
});
|
|
918
|
-
|
|
919
|
-
test("update and delete keep canonical by-id behavior", async () => {
|
|
920
|
-
const rows = [
|
|
921
|
-
{
|
|
922
|
-
contact_id: 11,
|
|
923
|
-
first_name: "Tony"
|
|
924
|
-
}
|
|
925
|
-
];
|
|
926
|
-
const { knex, state } = createKnexDouble(rows, {
|
|
927
|
-
insertResult: [11]
|
|
928
|
-
});
|
|
929
|
-
const repository = createCrudResourceRuntime(createResourceFixture(), knex);
|
|
930
|
-
|
|
931
|
-
const updated = await repository.updateById("11", {
|
|
932
|
-
firstName: "Tom"
|
|
933
|
-
});
|
|
934
|
-
const deleted = await repository.deleteById("11");
|
|
935
|
-
|
|
936
|
-
assert.equal(state.updatePayloads.length, 1);
|
|
937
|
-
assert.equal(state.deleteCount, 1);
|
|
938
|
-
assert.equal(updated.id, "11");
|
|
939
|
-
assert.deepEqual(deleted, {
|
|
940
|
-
id: "11",
|
|
941
|
-
deleted: true
|
|
942
|
-
});
|
|
943
|
-
});
|
|
944
|
-
|
|
945
|
-
test("update normalizes resource patch payloads before persistence", async () => {
|
|
946
|
-
const rows = [
|
|
947
|
-
{
|
|
948
|
-
contact_id: 11,
|
|
949
|
-
first_name: "Tony",
|
|
950
|
-
last_seen_at: null
|
|
951
|
-
}
|
|
952
|
-
];
|
|
953
|
-
const { knex, state } = createKnexDouble(rows, {
|
|
954
|
-
insertResult: [11]
|
|
955
|
-
});
|
|
956
|
-
const repository = createCrudResourceRuntime(createNormalizedWriteResourceFixture(), knex);
|
|
957
|
-
|
|
958
|
-
await repository.updateById("11", {
|
|
959
|
-
firstName: " Tom "
|
|
960
|
-
});
|
|
961
|
-
|
|
962
|
-
assert.equal(state.updatePayloads[0].first_name, "Tom");
|
|
963
|
-
});
|
|
964
|
-
|
|
965
|
-
test("update maps patch-only resource fields into the DB payload", async () => {
|
|
966
|
-
const rows = [
|
|
967
|
-
{
|
|
968
|
-
contact_id: 11,
|
|
969
|
-
first_name: "Tony",
|
|
970
|
-
last_seen_at: null
|
|
971
|
-
}
|
|
972
|
-
];
|
|
973
|
-
const { knex, state } = createKnexDouble(rows, {
|
|
974
|
-
insertResult: [11]
|
|
975
|
-
});
|
|
976
|
-
const repository = createCrudResourceRuntime(createNormalizedWriteResourceFixture(), knex);
|
|
977
|
-
|
|
978
|
-
await repository.updateById("11", {
|
|
979
|
-
lastSeenAt: " 2026-01-01T00:00:00.000Z "
|
|
980
|
-
});
|
|
981
|
-
|
|
982
|
-
assert.equal(state.updatePayloads[0].last_seen_at, "2026-01-01T00:00:00.000Z");
|
|
983
|
-
});
|
|
984
|
-
|
|
985
|
-
test("resourceRuntime maps schema field errors for create and update", async () => {
|
|
986
|
-
const rows = [
|
|
987
|
-
{
|
|
988
|
-
contact_id: 11,
|
|
989
|
-
first_name: "Tony"
|
|
990
|
-
}
|
|
991
|
-
];
|
|
992
|
-
const { knex } = createKnexDouble(rows, {
|
|
993
|
-
insertResult: [11]
|
|
994
|
-
});
|
|
995
|
-
const repository = createCrudResourceRuntime(createNormalizedWriteResourceFixture(), knex);
|
|
996
|
-
|
|
997
|
-
await assert.rejects(
|
|
998
|
-
() => repository.create({ firstName: " " }),
|
|
999
|
-
(error) => (
|
|
1000
|
-
error?.status === 400 &&
|
|
1001
|
-
error?.code === "validation_failed" &&
|
|
1002
|
-
typeof error?.details?.fieldErrors?.firstName === "string" &&
|
|
1003
|
-
error.details.fieldErrorCodes?.firstName?.code?.startsWith("kernel.validation.field.")
|
|
1004
|
-
)
|
|
1005
|
-
);
|
|
1006
|
-
|
|
1007
|
-
await assert.rejects(
|
|
1008
|
-
() => repository.updateById("11", { firstName: " " }),
|
|
1009
|
-
(error) => (
|
|
1010
|
-
error?.status === 400 &&
|
|
1011
|
-
typeof error?.details?.fieldErrors?.firstName === "string"
|
|
1012
|
-
)
|
|
1013
|
-
);
|
|
1014
|
-
});
|