@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,767 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createSchema } from "json-rest-schema";
|
|
4
|
-
import {
|
|
5
|
-
DEFAULT_LIST_LIMIT,
|
|
6
|
-
normalizeCrudListLimit,
|
|
7
|
-
normalizeCrudListCursor,
|
|
8
|
-
requireCrudTableName,
|
|
9
|
-
buildWritePayload,
|
|
10
|
-
mapRecordRow,
|
|
11
|
-
applyCrudListQueryFilters,
|
|
12
|
-
resolveCrudIdColumn,
|
|
13
|
-
buildRepositoryColumnMetadata,
|
|
14
|
-
deriveRepositoryMappingFromResource
|
|
15
|
-
} from "../src/server/repositorySupport.js";
|
|
16
|
-
|
|
17
|
-
function createQueryDouble() {
|
|
18
|
-
const calls = [];
|
|
19
|
-
const whereQuery = {
|
|
20
|
-
where(...args) {
|
|
21
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
22
|
-
calls.push(["innerWhereCallback"]);
|
|
23
|
-
args[0](whereQuery);
|
|
24
|
-
return whereQuery;
|
|
25
|
-
}
|
|
26
|
-
calls.push(["innerWhere", ...args]);
|
|
27
|
-
return whereQuery;
|
|
28
|
-
},
|
|
29
|
-
orWhere(...args) {
|
|
30
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
31
|
-
calls.push(["innerOrWhereCallback"]);
|
|
32
|
-
args[0](whereQuery);
|
|
33
|
-
return whereQuery;
|
|
34
|
-
}
|
|
35
|
-
calls.push(["innerOrWhere", ...args]);
|
|
36
|
-
return whereQuery;
|
|
37
|
-
},
|
|
38
|
-
whereNull(...args) {
|
|
39
|
-
calls.push(["innerWhereNull", ...args]);
|
|
40
|
-
return whereQuery;
|
|
41
|
-
},
|
|
42
|
-
orWhereNull(...args) {
|
|
43
|
-
calls.push(["innerOrWhereNull", ...args]);
|
|
44
|
-
return whereQuery;
|
|
45
|
-
},
|
|
46
|
-
whereNotNull(...args) {
|
|
47
|
-
calls.push(["innerWhereNotNull", ...args]);
|
|
48
|
-
return whereQuery;
|
|
49
|
-
},
|
|
50
|
-
orWhereNotNull(...args) {
|
|
51
|
-
calls.push(["innerOrWhereNotNull", ...args]);
|
|
52
|
-
return whereQuery;
|
|
53
|
-
},
|
|
54
|
-
whereRaw(...args) {
|
|
55
|
-
calls.push(["innerWhereRaw", ...args]);
|
|
56
|
-
return whereQuery;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const query = {
|
|
61
|
-
modify(callback) {
|
|
62
|
-
calls.push(["modify"]);
|
|
63
|
-
callback(query);
|
|
64
|
-
return query;
|
|
65
|
-
},
|
|
66
|
-
where(...args) {
|
|
67
|
-
if (args.length === 1 && typeof args[0] === "function") {
|
|
68
|
-
calls.push(["whereGroup"]);
|
|
69
|
-
args[0](whereQuery);
|
|
70
|
-
return query;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
calls.push(["where", ...args]);
|
|
74
|
-
return query;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
query,
|
|
80
|
-
calls
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function createOperationSchemaDefinition(structure = {}, mode = "replace") {
|
|
85
|
-
return {
|
|
86
|
-
schema: createSchema(structure),
|
|
87
|
-
mode
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
test("normalizeCrudListLimit enforces fallback and max", () => {
|
|
92
|
-
assert.equal(normalizeCrudListLimit(null), DEFAULT_LIST_LIMIT);
|
|
93
|
-
assert.equal(normalizeCrudListLimit("abc"), DEFAULT_LIST_LIMIT);
|
|
94
|
-
assert.equal(normalizeCrudListLimit(0), DEFAULT_LIST_LIMIT);
|
|
95
|
-
assert.equal(normalizeCrudListLimit(5), 5);
|
|
96
|
-
assert.equal(normalizeCrudListLimit(200), 100);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("normalizeCrudListCursor rejects malformed id cursors", () => {
|
|
100
|
-
assert.equal(normalizeCrudListCursor("7"), "7");
|
|
101
|
-
assert.equal(normalizeCrudListCursor(""), "");
|
|
102
|
-
assert.throws(
|
|
103
|
-
() => normalizeCrudListCursor("abc"),
|
|
104
|
-
/Invalid cursor/
|
|
105
|
-
);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test("requireCrudTableName trims and rejects empty values", () => {
|
|
109
|
-
assert.equal(requireCrudTableName(" crud_customers "), "crud_customers");
|
|
110
|
-
|
|
111
|
-
assert.throws(
|
|
112
|
-
() => requireCrudTableName(" "),
|
|
113
|
-
/requires tableName/
|
|
114
|
-
);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("mapRecordRow remaps rows by key/column pairs", () => {
|
|
118
|
-
const row = { some_column: 1, other_column: 2 };
|
|
119
|
-
const mapped = mapRecordRow(row, ["someKey", "otherKey"], {
|
|
120
|
-
someKey: "some_column",
|
|
121
|
-
otherKey: "other_column"
|
|
122
|
-
});
|
|
123
|
-
assert.deepEqual(mapped, {
|
|
124
|
-
someKey: 1,
|
|
125
|
-
otherKey: 2
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test("mapRecordRow omits keys whose source column is absent", () => {
|
|
130
|
-
const row = { some_column: 1 };
|
|
131
|
-
const mapped = mapRecordRow(row, ["someKey", "virtualField"], {
|
|
132
|
-
someKey: "some_column",
|
|
133
|
-
virtualField: "virtual_field"
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
assert.deepEqual(mapped, {
|
|
137
|
-
someKey: 1
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test("mapRecordRow serializes database temporal values for strict JSON resource output", () => {
|
|
142
|
-
const row = {
|
|
143
|
-
service_date: new Date("2026-08-13T23:59:58.123Z"),
|
|
144
|
-
opening_time: "07:08:09.123456",
|
|
145
|
-
scheduled_at: "2026-08-13 07:08:09.123456"
|
|
146
|
-
};
|
|
147
|
-
const mapped = mapRecordRow(
|
|
148
|
-
row,
|
|
149
|
-
["serviceDate", "openingTime", "scheduledAt"],
|
|
150
|
-
{
|
|
151
|
-
serviceDate: "service_date",
|
|
152
|
-
openingTime: "opening_time",
|
|
153
|
-
scheduledAt: "scheduled_at"
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
serializerByKey: {
|
|
157
|
-
serviceDate: "date",
|
|
158
|
-
openingTime: "time",
|
|
159
|
-
scheduledAt: "datetime"
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
assert.deepEqual(mapped, {
|
|
165
|
-
serviceDate: "2026-08-13",
|
|
166
|
-
openingTime: "07:08:09.123456",
|
|
167
|
-
scheduledAt: "2026-08-13T07:08:09.123456Z"
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test("buildWritePayload respects defined keys", () => {
|
|
172
|
-
const payload = buildWritePayload(
|
|
173
|
-
{ foo: "bar", missing: true },
|
|
174
|
-
["foo", "notPresent"],
|
|
175
|
-
{
|
|
176
|
-
foo: "foo_column",
|
|
177
|
-
notPresent: "not_present_column"
|
|
178
|
-
}
|
|
179
|
-
);
|
|
180
|
-
assert.deepEqual(payload, {
|
|
181
|
-
foo_column: "bar"
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
test("buildWritePayload serializes configured date-time keys to database shape", () => {
|
|
186
|
-
const payload = buildWritePayload(
|
|
187
|
-
{
|
|
188
|
-
scheduledAt: "2026-04-23T10:11:12.000Z",
|
|
189
|
-
archivedAt: null,
|
|
190
|
-
title: "Example"
|
|
191
|
-
},
|
|
192
|
-
["scheduledAt", "archivedAt", "title"],
|
|
193
|
-
{
|
|
194
|
-
scheduledAt: "scheduled_at",
|
|
195
|
-
archivedAt: "archived_at",
|
|
196
|
-
title: "title"
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
serializerByKey: {
|
|
200
|
-
scheduledAt: "datetime-utc",
|
|
201
|
-
archivedAt: "datetime-utc"
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
assert.deepEqual(payload, {
|
|
207
|
-
scheduled_at: "2026-04-23 10:11:12.000",
|
|
208
|
-
archived_at: null,
|
|
209
|
-
title: "Example"
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test("applyCrudListQueryFilters applies search and cursor filters", () => {
|
|
214
|
-
const { query, calls } = createQueryDouble();
|
|
215
|
-
const result = applyCrudListQueryFilters(query, {
|
|
216
|
-
idColumn: "id",
|
|
217
|
-
cursor: "3",
|
|
218
|
-
q: "ani",
|
|
219
|
-
searchColumns: ["first_name", "last_name"]
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
assert.equal(result, query);
|
|
223
|
-
assert.deepEqual(calls, [
|
|
224
|
-
["modify"],
|
|
225
|
-
["whereGroup"],
|
|
226
|
-
["innerWhere", "first_name", "like", "%ani%"],
|
|
227
|
-
["innerOrWhere", "last_name", "like", "%ani%"],
|
|
228
|
-
["where", "id", ">", "3"]
|
|
229
|
-
]);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
test("applyCrudListQueryFilters skips search and cursor when inputs are empty", () => {
|
|
233
|
-
const { query, calls } = createQueryDouble();
|
|
234
|
-
const result = applyCrudListQueryFilters(query, {
|
|
235
|
-
cursor: 0,
|
|
236
|
-
q: " ",
|
|
237
|
-
searchColumns: []
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
assert.equal(result, query);
|
|
241
|
-
assert.deepEqual(calls, []);
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
test("applyCrudListQueryFilters can skip id cursor filtering for ordered lists", () => {
|
|
245
|
-
const { query, calls } = createQueryDouble();
|
|
246
|
-
const result = applyCrudListQueryFilters(query, {
|
|
247
|
-
cursor: "9",
|
|
248
|
-
applyCursor: false
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
assert.equal(result, query);
|
|
252
|
-
assert.deepEqual(calls, []);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
test("applyCrudListQueryFilters rejects malformed id cursors", () => {
|
|
256
|
-
const { query } = createQueryDouble();
|
|
257
|
-
assert.throws(
|
|
258
|
-
() => applyCrudListQueryFilters(query, { cursor: "abc" }),
|
|
259
|
-
/Invalid cursor/
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test("applyCrudListQueryFilters applies parent FK filters from allowed columns", () => {
|
|
264
|
-
const { query, calls } = createQueryDouble();
|
|
265
|
-
const result = applyCrudListQueryFilters(query, {
|
|
266
|
-
parentFilters: {
|
|
267
|
-
contactId: " 7 ",
|
|
268
|
-
ignored: "x"
|
|
269
|
-
},
|
|
270
|
-
parentFilterColumns: {
|
|
271
|
-
contactId: "contact_id"
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
assert.equal(result, query);
|
|
276
|
-
assert.deepEqual(calls, [
|
|
277
|
-
["where", "contact_id", "7"]
|
|
278
|
-
]);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
test("applyCrudListQueryFilters throws for invalid query builders", () => {
|
|
282
|
-
assert.throws(
|
|
283
|
-
() => applyCrudListQueryFilters({}),
|
|
284
|
-
/requires query builder/
|
|
285
|
-
);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
test("resolveCrudIdColumn falls back and rejects empties", () => {
|
|
289
|
-
assert.equal(resolveCrudIdColumn(" custom_id "), "custom_id");
|
|
290
|
-
assert.equal(resolveCrudIdColumn(undefined, { fallback: "fallback_id" }), "fallback_id");
|
|
291
|
-
assert.throws(() => resolveCrudIdColumn("", { fallback: "" }), /requires idColumn/);
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
test("buildRepositoryColumnMetadata normalizes columns and applies overrides", () => {
|
|
295
|
-
const metadata = buildRepositoryColumnMetadata({
|
|
296
|
-
outputKeys: ["firstName", "lastName"],
|
|
297
|
-
writeKeys: ["firstName"],
|
|
298
|
-
columnOverrides: { lastName: "surname" },
|
|
299
|
-
fieldStorageByKey: {
|
|
300
|
-
firstName: "column",
|
|
301
|
-
lastName: "column"
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
assert.deepEqual(metadata.selectColumns, Object.freeze(["first_name", "surname"]));
|
|
306
|
-
assert.equal(metadata.outputMappings.length, 2);
|
|
307
|
-
assert.equal(metadata.writeMappings.length, 1);
|
|
308
|
-
assert.equal(metadata.outputMappings[1].column, "surname");
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
test("deriveRepositoryMappingFromResource reads schema keys and repository column overrides", () => {
|
|
312
|
-
const resource = {
|
|
313
|
-
operations: {
|
|
314
|
-
view: {
|
|
315
|
-
output: createOperationSchemaDefinition({
|
|
316
|
-
id: { type: "integer", required: true },
|
|
317
|
-
firstName: { type: "string", required: true },
|
|
318
|
-
createdAt: {
|
|
319
|
-
type: "string",
|
|
320
|
-
required: true,
|
|
321
|
-
actualField: "created_at"
|
|
322
|
-
}
|
|
323
|
-
})
|
|
324
|
-
},
|
|
325
|
-
create: {
|
|
326
|
-
body: createOperationSchemaDefinition({
|
|
327
|
-
firstName: { type: "string" },
|
|
328
|
-
vetId: {
|
|
329
|
-
type: "integer",
|
|
330
|
-
actualField: "vet_id",
|
|
331
|
-
relation: {
|
|
332
|
-
kind: "lookup",
|
|
333
|
-
namespace: "vets",
|
|
334
|
-
valueKey: "id"
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}, "create")
|
|
338
|
-
},
|
|
339
|
-
patch: {
|
|
340
|
-
body: createOperationSchemaDefinition({
|
|
341
|
-
archivedAt: {
|
|
342
|
-
type: "string",
|
|
343
|
-
actualField: "archived_at"
|
|
344
|
-
},
|
|
345
|
-
vetId: {
|
|
346
|
-
type: "integer",
|
|
347
|
-
actualField: "vet_id",
|
|
348
|
-
relation: {
|
|
349
|
-
kind: "lookup",
|
|
350
|
-
namespace: "vets",
|
|
351
|
-
valueKey: "id"
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}, "patch")
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
360
|
-
assert.deepEqual(mapping.outputKeys, ["id", "firstName", "createdAt"]);
|
|
361
|
-
assert.deepEqual(mapping.writeKeys, ["firstName", "vetId", "archivedAt"]);
|
|
362
|
-
assert.deepEqual(mapping.columnOverrides, {
|
|
363
|
-
createdAt: "created_at",
|
|
364
|
-
vetId: "vet_id",
|
|
365
|
-
archivedAt: "archived_at"
|
|
366
|
-
});
|
|
367
|
-
assert.deepEqual(mapping.listSearchColumns, ["first_name", "created_at"]);
|
|
368
|
-
assert.deepEqual(mapping.parentFilterColumns, {
|
|
369
|
-
vetId: "vet_id"
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
test("deriveRepositoryMappingFromResource treats virtual output fields as non-column projections", () => {
|
|
374
|
-
const resource = {
|
|
375
|
-
operations: {
|
|
376
|
-
view: {
|
|
377
|
-
output: createOperationSchemaDefinition({
|
|
378
|
-
id: { type: "integer", required: true },
|
|
379
|
-
firstName: { type: "string", required: true },
|
|
380
|
-
remainingBatchWeight: {
|
|
381
|
-
type: "number",
|
|
382
|
-
storage: {
|
|
383
|
-
virtual: true
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
})
|
|
387
|
-
},
|
|
388
|
-
create: {
|
|
389
|
-
body: createOperationSchemaDefinition({
|
|
390
|
-
firstName: { type: "string" }
|
|
391
|
-
}, "create")
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
397
|
-
assert.deepEqual(mapping.outputKeys, ["id", "firstName", "remainingBatchWeight"]);
|
|
398
|
-
assert.deepEqual(mapping.columnBackedOutputKeys, ["id", "firstName"]);
|
|
399
|
-
assert.deepEqual(mapping.virtualOutputKeys, ["remainingBatchWeight"]);
|
|
400
|
-
assert.equal(mapping.fieldStorageByKey.remainingBatchWeight, "virtual");
|
|
401
|
-
assert.deepEqual(mapping.listSearchColumns, ["first_name"]);
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
test("deriveRepositoryMappingFromResource rejects virtual fields in create schema", () => {
|
|
405
|
-
const resource = {
|
|
406
|
-
operations: {
|
|
407
|
-
view: {
|
|
408
|
-
output: createOperationSchemaDefinition({
|
|
409
|
-
id: { type: "integer", required: true },
|
|
410
|
-
remainingBatchWeight: {
|
|
411
|
-
type: "number",
|
|
412
|
-
storage: {
|
|
413
|
-
virtual: true
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
})
|
|
417
|
-
},
|
|
418
|
-
create: {
|
|
419
|
-
body: createOperationSchemaDefinition({
|
|
420
|
-
remainingBatchWeight: {
|
|
421
|
-
type: "number",
|
|
422
|
-
storage: {
|
|
423
|
-
virtual: true
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}, "create")
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
assert.throws(
|
|
432
|
-
() => deriveRepositoryMappingFromResource(resource),
|
|
433
|
-
/resource create schema field "remainingBatchWeight" cannot use storage\.virtual/
|
|
434
|
-
);
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
test("deriveRepositoryMappingFromResource rejects virtual fields in patch schema", () => {
|
|
438
|
-
const resource = {
|
|
439
|
-
operations: {
|
|
440
|
-
view: {
|
|
441
|
-
output: createOperationSchemaDefinition({
|
|
442
|
-
id: { type: "integer", required: true },
|
|
443
|
-
remainingBatchWeight: {
|
|
444
|
-
type: "number",
|
|
445
|
-
storage: {
|
|
446
|
-
virtual: true
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
})
|
|
450
|
-
},
|
|
451
|
-
create: {
|
|
452
|
-
body: createOperationSchemaDefinition({}, "create")
|
|
453
|
-
},
|
|
454
|
-
patch: {
|
|
455
|
-
body: createOperationSchemaDefinition({
|
|
456
|
-
remainingBatchWeight: {
|
|
457
|
-
type: "number",
|
|
458
|
-
storage: {
|
|
459
|
-
virtual: true
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}, "patch")
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
assert.throws(
|
|
468
|
-
() => deriveRepositoryMappingFromResource(resource),
|
|
469
|
-
/resource patch schema field "remainingBatchWeight" cannot use storage\.virtual/
|
|
470
|
-
);
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
test("deriveRepositoryMappingFromResource excludes runtime-only lookups output key from db mapping", () => {
|
|
474
|
-
const resource = {
|
|
475
|
-
operations: {
|
|
476
|
-
view: {
|
|
477
|
-
output: createOperationSchemaDefinition({
|
|
478
|
-
id: { type: "integer", required: true },
|
|
479
|
-
firstName: { type: "string", required: true },
|
|
480
|
-
lookups: { type: "object", opaque: true }
|
|
481
|
-
})
|
|
482
|
-
},
|
|
483
|
-
create: {
|
|
484
|
-
body: createOperationSchemaDefinition({
|
|
485
|
-
firstName: { type: "string" }
|
|
486
|
-
}, "create")
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
492
|
-
assert.deepEqual(mapping.outputKeys, ["id", "firstName"]);
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
test("deriveRepositoryMappingFromResource excludes custom lookup output container key", () => {
|
|
496
|
-
const resource = {
|
|
497
|
-
contract: {
|
|
498
|
-
lookup: {
|
|
499
|
-
containerKey: "lookupData"
|
|
500
|
-
}
|
|
501
|
-
},
|
|
502
|
-
operations: {
|
|
503
|
-
view: {
|
|
504
|
-
output: createOperationSchemaDefinition({
|
|
505
|
-
id: { type: "integer", required: true },
|
|
506
|
-
firstName: { type: "string", required: true },
|
|
507
|
-
lookupData: { type: "object", opaque: true }
|
|
508
|
-
})
|
|
509
|
-
},
|
|
510
|
-
create: {
|
|
511
|
-
body: createOperationSchemaDefinition({
|
|
512
|
-
firstName: { type: "string" }
|
|
513
|
-
}, "create")
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
519
|
-
assert.deepEqual(mapping.outputKeys, ["id", "firstName"]);
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
test("deriveRepositoryMappingFromResource rejects non-schema view output definitions", () => {
|
|
523
|
-
const resource = {
|
|
524
|
-
operations: {
|
|
525
|
-
view: {
|
|
526
|
-
output: {
|
|
527
|
-
schema: {
|
|
528
|
-
type: "object"
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
},
|
|
532
|
-
create: {
|
|
533
|
-
body: createOperationSchemaDefinition({
|
|
534
|
-
firstName: { type: "string" }
|
|
535
|
-
}, "create")
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
};
|
|
539
|
-
|
|
540
|
-
assert.throws(
|
|
541
|
-
() => deriveRepositoryMappingFromResource(resource),
|
|
542
|
-
/operations\.view\.output\.schema must be a json-rest-schema schema instance/
|
|
543
|
-
);
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
test("deriveRepositoryMappingFromResource rejects non-schema create body definitions", () => {
|
|
547
|
-
const resource = {
|
|
548
|
-
operations: {
|
|
549
|
-
view: {
|
|
550
|
-
output: createOperationSchemaDefinition({
|
|
551
|
-
id: { type: "integer", required: true }
|
|
552
|
-
})
|
|
553
|
-
},
|
|
554
|
-
create: {
|
|
555
|
-
body: {
|
|
556
|
-
schema: {
|
|
557
|
-
type: "object"
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
assert.throws(
|
|
565
|
-
() => deriveRepositoryMappingFromResource(resource),
|
|
566
|
-
/operations\.create\.body\.schema must be a json-rest-schema schema instance/
|
|
567
|
-
);
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
test("deriveRepositoryMappingFromResource tracks writable column-backed write serializers", () => {
|
|
571
|
-
const resource = {
|
|
572
|
-
operations: {
|
|
573
|
-
view: {
|
|
574
|
-
output: createOperationSchemaDefinition({
|
|
575
|
-
id: { type: "integer", required: true },
|
|
576
|
-
scheduledAt: { type: "dateTime", required: true },
|
|
577
|
-
archivedAt: { type: "dateTime", required: true },
|
|
578
|
-
remainingBatchWeight: {
|
|
579
|
-
type: "number",
|
|
580
|
-
storage: {
|
|
581
|
-
virtual: true
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
})
|
|
585
|
-
},
|
|
586
|
-
create: {
|
|
587
|
-
body: createOperationSchemaDefinition({
|
|
588
|
-
scheduledAt: { type: "dateTime" }
|
|
589
|
-
}, "create")
|
|
590
|
-
},
|
|
591
|
-
patch: {
|
|
592
|
-
body: createOperationSchemaDefinition({
|
|
593
|
-
archivedAt: {
|
|
594
|
-
type: "dateTime",
|
|
595
|
-
nullable: true
|
|
596
|
-
}
|
|
597
|
-
}, "patch")
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
|
|
602
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
603
|
-
assert.deepEqual(mapping.writeSerializerByKey, {
|
|
604
|
-
scheduledAt: "datetime-utc",
|
|
605
|
-
archivedAt: "datetime-utc"
|
|
606
|
-
});
|
|
607
|
-
assert.deepEqual(mapping.outputSerializerByKey, {
|
|
608
|
-
scheduledAt: { type: "datetime" },
|
|
609
|
-
archivedAt: { type: "datetime" }
|
|
610
|
-
});
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
test("deriveRepositoryMappingFromResource tracks JSON temporal output serializers", () => {
|
|
614
|
-
const resource = {
|
|
615
|
-
operations: {
|
|
616
|
-
view: {
|
|
617
|
-
output: createOperationSchemaDefinition({
|
|
618
|
-
id: { type: "integer", required: true },
|
|
619
|
-
serviceDate: { type: "date", required: true },
|
|
620
|
-
openingTime: { type: "time", required: true },
|
|
621
|
-
scheduledAt: { type: "dateTime", temporalPrecision: 3, required: true }
|
|
622
|
-
})
|
|
623
|
-
},
|
|
624
|
-
create: {
|
|
625
|
-
body: createOperationSchemaDefinition({}, "create")
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
};
|
|
629
|
-
|
|
630
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
631
|
-
|
|
632
|
-
assert.deepEqual(mapping.outputSerializerByKey, {
|
|
633
|
-
serviceDate: { type: "date" },
|
|
634
|
-
openingTime: { type: "time" },
|
|
635
|
-
scheduledAt: { type: "datetime", temporalPrecision: 3 }
|
|
636
|
-
});
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
test("deriveRepositoryMappingFromResource keeps explicit storage.writeSerializer metadata", () => {
|
|
640
|
-
const resource = {
|
|
641
|
-
operations: {
|
|
642
|
-
view: {
|
|
643
|
-
output: createOperationSchemaDefinition({
|
|
644
|
-
id: { type: "integer", required: true },
|
|
645
|
-
arrivalDatetime: {
|
|
646
|
-
type: "dateTime",
|
|
647
|
-
required: true,
|
|
648
|
-
storage: {
|
|
649
|
-
writeSerializer: "datetime-utc"
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
})
|
|
653
|
-
},
|
|
654
|
-
create: {
|
|
655
|
-
body: createOperationSchemaDefinition({
|
|
656
|
-
arrivalDatetime: {
|
|
657
|
-
type: "dateTime",
|
|
658
|
-
storage: {
|
|
659
|
-
writeSerializer: "datetime-utc"
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
}, "create")
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
};
|
|
666
|
-
|
|
667
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
668
|
-
assert.deepEqual(mapping.writeSerializerByKey, {
|
|
669
|
-
arrivalDatetime: "datetime-utc"
|
|
670
|
-
});
|
|
671
|
-
});
|
|
672
|
-
|
|
673
|
-
test("deriveRepositoryMappingFromResource ignores transport export shape and uses authored field definitions", () => {
|
|
674
|
-
const viewSchema = createSchema({
|
|
675
|
-
recordId: {
|
|
676
|
-
type: "id",
|
|
677
|
-
required: true,
|
|
678
|
-
actualField: "record_id"
|
|
679
|
-
},
|
|
680
|
-
title: {
|
|
681
|
-
type: "string",
|
|
682
|
-
required: true
|
|
683
|
-
},
|
|
684
|
-
scheduledAt: {
|
|
685
|
-
type: "dateTime",
|
|
686
|
-
required: true,
|
|
687
|
-
actualField: "scheduled_at"
|
|
688
|
-
}
|
|
689
|
-
});
|
|
690
|
-
const createInputSchema = createSchema({
|
|
691
|
-
title: {
|
|
692
|
-
type: "string",
|
|
693
|
-
required: true
|
|
694
|
-
},
|
|
695
|
-
scheduledAt: {
|
|
696
|
-
type: "dateTime",
|
|
697
|
-
required: true,
|
|
698
|
-
actualField: "scheduled_at"
|
|
699
|
-
}
|
|
700
|
-
});
|
|
701
|
-
const patchInputSchema = createSchema({
|
|
702
|
-
scheduledAt: {
|
|
703
|
-
type: "dateTime",
|
|
704
|
-
required: false,
|
|
705
|
-
actualField: "scheduled_at"
|
|
706
|
-
}
|
|
707
|
-
});
|
|
708
|
-
|
|
709
|
-
viewSchema.toJsonSchema = () => ({
|
|
710
|
-
type: "object",
|
|
711
|
-
properties: {
|
|
712
|
-
title: {
|
|
713
|
-
type: "number"
|
|
714
|
-
},
|
|
715
|
-
scheduledAt: {
|
|
716
|
-
type: "string"
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
});
|
|
720
|
-
createInputSchema.toJsonSchema = () => ({
|
|
721
|
-
type: "object",
|
|
722
|
-
properties: {
|
|
723
|
-
scheduledAt: {
|
|
724
|
-
type: "string"
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
});
|
|
728
|
-
patchInputSchema.toJsonSchema = () => ({
|
|
729
|
-
type: "object",
|
|
730
|
-
properties: {}
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
const resource = {
|
|
734
|
-
operations: {
|
|
735
|
-
view: {
|
|
736
|
-
output: {
|
|
737
|
-
schema: viewSchema,
|
|
738
|
-
mode: "replace"
|
|
739
|
-
}
|
|
740
|
-
},
|
|
741
|
-
create: {
|
|
742
|
-
body: {
|
|
743
|
-
schema: createInputSchema,
|
|
744
|
-
mode: "create"
|
|
745
|
-
}
|
|
746
|
-
},
|
|
747
|
-
patch: {
|
|
748
|
-
body: {
|
|
749
|
-
schema: patchInputSchema,
|
|
750
|
-
mode: "patch"
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
const mapping = deriveRepositoryMappingFromResource(resource);
|
|
757
|
-
assert.deepEqual(mapping.outputKeys, ["recordId", "title", "scheduledAt"]);
|
|
758
|
-
assert.deepEqual(mapping.outputRecordIdKeys, ["recordId"]);
|
|
759
|
-
assert.deepEqual(mapping.listSearchColumns, ["title"]);
|
|
760
|
-
assert.deepEqual(mapping.writeSerializerByKey, {
|
|
761
|
-
scheduledAt: "datetime-utc"
|
|
762
|
-
});
|
|
763
|
-
assert.deepEqual(mapping.columnOverrides, {
|
|
764
|
-
recordId: "record_id",
|
|
765
|
-
scheduledAt: "scheduled_at"
|
|
766
|
-
});
|
|
767
|
-
});
|