@jskit-ai/crud-core 0.2.14 → 0.2.16
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 +21 -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 +538 -0
- package/src/server/jsonApiModule/actions.js +117 -59
- package/src/server/jsonApiModule/routes.js +35 -11
- package/src/server/jsonApiModule/service.js +227 -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 +51 -0
- package/test/crudOnHandles.test.js +451 -0
- package/test/{defineCrudJsonApiFeature.test.js → defineCrudFeature.test.js} +451 -141
- package/test/errorCodes.test.js +11 -52
- package/test/hiddenFields.test.js +63 -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,528 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
normalizeDbRecordId,
|
|
3
|
-
toDatabaseDateTimeUtc,
|
|
4
|
-
toJsonDate,
|
|
5
|
-
toJsonTime,
|
|
6
|
-
toJsonDateTime
|
|
7
|
-
} from "@jskit-ai/database-runtime/shared";
|
|
8
|
-
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
9
|
-
import { RECORD_ID_PATTERN } from "@jskit-ai/kernel/shared/validators";
|
|
10
|
-
import { normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
11
|
-
import { normalizeObjectInput } from "@jskit-ai/kernel/shared/validators/inputNormalization";
|
|
12
|
-
import { normalizeSingleSchemaDefinition } from "@jskit-ai/kernel/shared/validators";
|
|
13
|
-
import {
|
|
14
|
-
buildCrudFieldContractMap,
|
|
15
|
-
resolveCrudFieldSchemaProperties,
|
|
16
|
-
CRUD_FIELD_STORAGE_COLUMN,
|
|
17
|
-
CRUD_FIELD_STORAGE_VIRTUAL,
|
|
18
|
-
CRUD_FIELD_WRITE_SERIALIZER_DATETIME_UTC
|
|
19
|
-
} from "@jskit-ai/resource-crud-core/shared/crudFieldContract";
|
|
20
|
-
import { toSnakeCase } from "@jskit-ai/kernel/shared/support/stringCase";
|
|
21
|
-
import {
|
|
22
|
-
isCrudRuntimeOutputOnlyFieldKey,
|
|
23
|
-
resolveCrudLookupContainerKey,
|
|
24
|
-
resolveCrudLookupFieldKeys
|
|
25
|
-
} from "@jskit-ai/resource-crud-core/shared/crudLookup";
|
|
26
|
-
|
|
27
|
-
const DEFAULT_LIST_LIMIT = 20;
|
|
28
|
-
const MAX_LIST_LIMIT = 100;
|
|
29
|
-
const CRUD_WRITE_SERIALIZERS = Object.freeze({
|
|
30
|
-
[CRUD_FIELD_WRITE_SERIALIZER_DATETIME_UTC]: (value) => toDatabaseDateTimeUtc(value)
|
|
31
|
-
});
|
|
32
|
-
const CRUD_OUTPUT_SERIALIZERS = Object.freeze({
|
|
33
|
-
date: toJsonDate,
|
|
34
|
-
time: toJsonTime,
|
|
35
|
-
datetime: toJsonDateTime
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
function normalizeCrudListCursor(cursor = null, { allowEmpty = true } = {}) {
|
|
39
|
-
if (cursor === undefined || cursor === null) {
|
|
40
|
-
return allowEmpty === true ? "" : null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const normalizedCursor = typeof cursor === "string"
|
|
44
|
-
? cursor.trim()
|
|
45
|
-
: cursor;
|
|
46
|
-
if (normalizedCursor === "" || normalizedCursor === 0 || normalizedCursor === "0") {
|
|
47
|
-
return allowEmpty === true ? "" : null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const recordId = normalizeRecordId(normalizedCursor, { fallback: null });
|
|
51
|
-
if (!recordId) {
|
|
52
|
-
throw new AppError(400, "Invalid cursor.", {
|
|
53
|
-
code: "INVALID_CURSOR"
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return recordId;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function normalizeCrudListLimit(value, { fallback = DEFAULT_LIST_LIMIT, max = MAX_LIST_LIMIT } = {}) {
|
|
61
|
-
const parsed = Number(value);
|
|
62
|
-
if (!Number.isInteger(parsed) || parsed < 1) {
|
|
63
|
-
return fallback;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return Math.min(parsed, max);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function requireCrudTableName(tableName, { context = "crudRepository" } = {}) {
|
|
70
|
-
const normalizedTableName = normalizeText(tableName);
|
|
71
|
-
if (!normalizedTableName) {
|
|
72
|
-
throw new TypeError(`${context} requires tableName.`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return normalizedTableName;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function resolveColumnName(fieldKey, overrides = {}) {
|
|
79
|
-
const normalizedKey = String(fieldKey || "").trim();
|
|
80
|
-
if (!normalizedKey) {
|
|
81
|
-
return "";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const overrideValue = String(overrides[normalizedKey] || "").trim();
|
|
85
|
-
if (overrideValue) {
|
|
86
|
-
return overrideValue;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return toSnakeCase(normalizedKey);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function buildRepositoryColumnMetadata({
|
|
93
|
-
outputKeys = [],
|
|
94
|
-
writeKeys = [],
|
|
95
|
-
columnOverrides = {},
|
|
96
|
-
fieldStorageByKey = {}
|
|
97
|
-
} = {}) {
|
|
98
|
-
const normalizedOutputKeys = (Array.isArray(outputKeys) ? outputKeys : [])
|
|
99
|
-
.map((key) => String(key || "").trim())
|
|
100
|
-
.filter(Boolean);
|
|
101
|
-
const normalizedWriteKeys = (Array.isArray(writeKeys) ? writeKeys : [])
|
|
102
|
-
.map((key) => String(key || "").trim())
|
|
103
|
-
.filter(Boolean);
|
|
104
|
-
|
|
105
|
-
const deriveMapping = (key) => {
|
|
106
|
-
if (fieldStorageByKey[key] !== CRUD_FIELD_STORAGE_COLUMN) {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
const column = resolveColumnName(key, columnOverrides);
|
|
110
|
-
if (!column) {
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
return { key, column };
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const outputMappings = normalizedOutputKeys.map(deriveMapping).filter(Boolean);
|
|
117
|
-
const writeMappings = normalizedWriteKeys.map(deriveMapping).filter(Boolean);
|
|
118
|
-
const selectColumns = Object.freeze(
|
|
119
|
-
[...new Set(outputMappings.map((mapping) => mapping.column))]
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
return Object.freeze({
|
|
123
|
-
selectColumns,
|
|
124
|
-
outputMappings: Object.freeze(outputMappings),
|
|
125
|
-
writeMappings: Object.freeze(writeMappings)
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function resolveOptionalFieldDefinitions(definition, options = {}) {
|
|
130
|
-
if (!definition) {
|
|
131
|
-
return {};
|
|
132
|
-
}
|
|
133
|
-
return requireFieldDefinitions(definition, options);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function requireFieldDefinitions(definition, {
|
|
137
|
-
context = "crudRepository",
|
|
138
|
-
schemaLabel = "schema definition",
|
|
139
|
-
defaultMode = "patch"
|
|
140
|
-
} = {}) {
|
|
141
|
-
const normalized = normalizeSingleSchemaDefinition(definition, {
|
|
142
|
-
context: `${context} ${schemaLabel}`,
|
|
143
|
-
defaultMode
|
|
144
|
-
});
|
|
145
|
-
if (!normalized) {
|
|
146
|
-
throw new TypeError(`${context} requires ${schemaLabel}.`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const properties = resolveCrudFieldSchemaProperties(normalized, {
|
|
150
|
-
context: `${context} ${schemaLabel}`
|
|
151
|
-
});
|
|
152
|
-
if (!properties || typeof properties !== "object" || Array.isArray(properties)) {
|
|
153
|
-
throw new TypeError(`${context} requires ${schemaLabel} field definitions.`);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return properties;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function resolveFieldDefinitionType(definition) {
|
|
160
|
-
return normalizeText(definition?.type).toLowerCase();
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function definitionIncludesStringType(definition = {}) {
|
|
164
|
-
return resolveFieldDefinitionType(definition) === "string";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function definitionIncludesDateTimeType(definition = {}) {
|
|
168
|
-
return resolveFieldDefinitionType(definition) === "datetime";
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function definitionIncludesRecordIdType(definition = {}) {
|
|
172
|
-
const type = resolveFieldDefinitionType(definition);
|
|
173
|
-
if (type === "id") {
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return type === "string" && normalizeText(definition.pattern) === RECORD_ID_PATTERN;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function deriveRepositoryMappingFromResource(resource = {}, { context = "crudRepository" } = {}) {
|
|
181
|
-
if (!resource || typeof resource !== "object" || Array.isArray(resource)) {
|
|
182
|
-
throw new TypeError(`${context} requires resource object.`);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const operations = resource.operations;
|
|
186
|
-
if (!operations || typeof operations !== "object" || Array.isArray(operations)) {
|
|
187
|
-
throw new TypeError(`${context} requires resource.operations.`);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const outputProperties = requireFieldDefinitions(operations.view?.output, {
|
|
191
|
-
context,
|
|
192
|
-
schemaLabel: "operations.view.output",
|
|
193
|
-
defaultMode: "replace"
|
|
194
|
-
});
|
|
195
|
-
const writeProperties = requireFieldDefinitions(operations.create?.body, {
|
|
196
|
-
context,
|
|
197
|
-
schemaLabel: "operations.create.body",
|
|
198
|
-
defaultMode: "create"
|
|
199
|
-
});
|
|
200
|
-
const patchProperties = resolveOptionalFieldDefinitions(operations.patch?.body, {
|
|
201
|
-
context,
|
|
202
|
-
schemaLabel: "operations.patch.body",
|
|
203
|
-
defaultMode: "patch"
|
|
204
|
-
});
|
|
205
|
-
const lookupContainerKey = resolveCrudLookupContainerKey(resource, {
|
|
206
|
-
context: `${context} resource.contract.lookup.containerKey`
|
|
207
|
-
});
|
|
208
|
-
const outputKeys = Object.freeze(
|
|
209
|
-
Object.keys(outputProperties).filter(
|
|
210
|
-
(key) => !isCrudRuntimeOutputOnlyFieldKey(key, { lookupContainerKey })
|
|
211
|
-
)
|
|
212
|
-
);
|
|
213
|
-
const writeKeys = Object.freeze([
|
|
214
|
-
...Object.keys(writeProperties),
|
|
215
|
-
...Object.keys(patchProperties).filter((key) => !Object.hasOwn(writeProperties, key))
|
|
216
|
-
]);
|
|
217
|
-
|
|
218
|
-
const fieldStorageByKey = {};
|
|
219
|
-
const columnOverrides = {};
|
|
220
|
-
const writeSerializerByKey = {};
|
|
221
|
-
for (const entry of Object.values(buildCrudFieldContractMap(resource, {
|
|
222
|
-
context: `${context} resource field contract`
|
|
223
|
-
}))) {
|
|
224
|
-
const key = normalizeText(entry.key);
|
|
225
|
-
if (!key) {
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
fieldStorageByKey[key] = entry.storage.mode || CRUD_FIELD_STORAGE_COLUMN;
|
|
229
|
-
if (entry.storage.column) {
|
|
230
|
-
columnOverrides[key] = entry.storage.column;
|
|
231
|
-
}
|
|
232
|
-
if (entry.storage.writeSerializer) {
|
|
233
|
-
writeSerializerByKey[key] = entry.storage.writeSerializer;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
for (const key of [...outputKeys, ...writeKeys]) {
|
|
238
|
-
if (!fieldStorageByKey[key]) {
|
|
239
|
-
fieldStorageByKey[key] = CRUD_FIELD_STORAGE_COLUMN;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const virtualOutputKeys = [];
|
|
244
|
-
const columnBackedOutputKeys = [];
|
|
245
|
-
for (const key of outputKeys) {
|
|
246
|
-
const storage = fieldStorageByKey[key] || CRUD_FIELD_STORAGE_COLUMN;
|
|
247
|
-
if (storage === CRUD_FIELD_STORAGE_VIRTUAL) {
|
|
248
|
-
virtualOutputKeys.push(key);
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
columnBackedOutputKeys.push(key);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
for (const key of virtualOutputKeys) {
|
|
255
|
-
if (Object.hasOwn(writeProperties, key)) {
|
|
256
|
-
throw new Error(
|
|
257
|
-
`${context} resource create schema field "${key}" cannot use storage.virtual.`
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
if (Object.hasOwn(patchProperties, key)) {
|
|
261
|
-
throw new Error(
|
|
262
|
-
`${context} resource patch schema field "${key}" cannot use storage.virtual.`
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const listSearchColumns = [];
|
|
268
|
-
for (const [key, definition] of Object.entries(outputProperties)) {
|
|
269
|
-
if ((fieldStorageByKey[key] || CRUD_FIELD_STORAGE_COLUMN) !== CRUD_FIELD_STORAGE_COLUMN) {
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
if (!definitionIncludesStringType(definition)) {
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const columnName = resolveColumnName(key, columnOverrides);
|
|
277
|
-
if (!columnName || listSearchColumns.includes(columnName)) {
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
listSearchColumns.push(columnName);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const parentFilterColumns = {};
|
|
284
|
-
for (const key of resolveCrudLookupFieldKeys(resource, { allowKeys: writeKeys })) {
|
|
285
|
-
if ((fieldStorageByKey[key] || CRUD_FIELD_STORAGE_COLUMN) !== CRUD_FIELD_STORAGE_COLUMN) {
|
|
286
|
-
continue;
|
|
287
|
-
}
|
|
288
|
-
const columnName = resolveColumnName(key, columnOverrides);
|
|
289
|
-
if (!columnName) {
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
parentFilterColumns[key] = columnName;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const outputRecordIdKeys = [];
|
|
296
|
-
const outputSerializerByKey = {};
|
|
297
|
-
for (const [key, definition] of Object.entries(outputProperties)) {
|
|
298
|
-
if (definitionIncludesRecordIdType(definition)) {
|
|
299
|
-
outputRecordIdKeys.push(key);
|
|
300
|
-
}
|
|
301
|
-
const fieldType = resolveFieldDefinitionType(definition);
|
|
302
|
-
if (Object.hasOwn(CRUD_OUTPUT_SERIALIZERS, fieldType)) {
|
|
303
|
-
outputSerializerByKey[key] = Object.freeze({
|
|
304
|
-
type: fieldType,
|
|
305
|
-
...(Number.isInteger(definition?.temporalPrecision)
|
|
306
|
-
? { temporalPrecision: definition.temporalPrecision }
|
|
307
|
-
: {})
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
for (const key of writeKeys) {
|
|
313
|
-
if ((fieldStorageByKey[key] || CRUD_FIELD_STORAGE_COLUMN) !== CRUD_FIELD_STORAGE_COLUMN) {
|
|
314
|
-
continue;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (writeSerializerByKey[key]) {
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const definition = writeProperties[key] || patchProperties[key];
|
|
322
|
-
if (!definitionIncludesDateTimeType(definition)) {
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
writeSerializerByKey[key] = CRUD_FIELD_WRITE_SERIALIZER_DATETIME_UTC;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return Object.freeze({
|
|
330
|
-
outputKeys,
|
|
331
|
-
writeKeys,
|
|
332
|
-
writeSerializerByKey: Object.freeze(writeSerializerByKey),
|
|
333
|
-
fieldStorageByKey: Object.freeze(fieldStorageByKey),
|
|
334
|
-
columnOverrides: Object.freeze(columnOverrides),
|
|
335
|
-
columnBackedOutputKeys: Object.freeze(columnBackedOutputKeys),
|
|
336
|
-
virtualOutputKeys: Object.freeze(virtualOutputKeys),
|
|
337
|
-
listSearchColumns: Object.freeze(listSearchColumns),
|
|
338
|
-
parentFilterColumns: Object.freeze(parentFilterColumns),
|
|
339
|
-
outputRecordIdKeys: Object.freeze(outputRecordIdKeys),
|
|
340
|
-
outputSerializerByKey: Object.freeze(outputSerializerByKey)
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function mapRecordRow(
|
|
345
|
-
row,
|
|
346
|
-
fieldKeys = [],
|
|
347
|
-
overrides = {},
|
|
348
|
-
{ recordIdKeys = [], serializerByKey = {} } = {}
|
|
349
|
-
) {
|
|
350
|
-
if (!row) {
|
|
351
|
-
return null;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const recordIdKeySet = new Set(
|
|
355
|
-
(Array.isArray(recordIdKeys) ? recordIdKeys : [])
|
|
356
|
-
.map((key) => String(key || "").trim())
|
|
357
|
-
.filter(Boolean)
|
|
358
|
-
);
|
|
359
|
-
const mapped = {};
|
|
360
|
-
const normalizedSerializerByKey = normalizeObjectInput(serializerByKey);
|
|
361
|
-
for (const key of fieldKeys) {
|
|
362
|
-
const normalizedKey = String(key || "").trim();
|
|
363
|
-
const columnName = resolveColumnName(normalizedKey, overrides);
|
|
364
|
-
if (!normalizedKey || !columnName) {
|
|
365
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const rawValue = row[columnName];
|
|
369
|
-
if (rawValue === undefined) {
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (recordIdKeySet.has(normalizedKey)) {
|
|
374
|
-
const normalizedIdValue = normalizeDbRecordId(rawValue, { fallback: null });
|
|
375
|
-
mapped[normalizedKey] = normalizedIdValue || rawValue;
|
|
376
|
-
continue;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const serializerConfig = normalizedSerializerByKey[normalizedKey];
|
|
380
|
-
const serializerId = normalizeText(
|
|
381
|
-
typeof serializerConfig === "string" ? serializerConfig : serializerConfig?.type
|
|
382
|
-
).toLowerCase();
|
|
383
|
-
if (serializerId && rawValue != null) {
|
|
384
|
-
const serializer = CRUD_OUTPUT_SERIALIZERS[serializerId];
|
|
385
|
-
if (typeof serializer !== "function") {
|
|
386
|
-
throw new Error(`crudRepository output serializer "${serializerId}" is not supported.`);
|
|
387
|
-
}
|
|
388
|
-
mapped[normalizedKey] = serializer(rawValue, {
|
|
389
|
-
temporalPrecision: serializerConfig?.temporalPrecision
|
|
390
|
-
});
|
|
391
|
-
continue;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
mapped[normalizedKey] = rawValue;
|
|
395
|
-
}
|
|
396
|
-
return mapped;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function applyCrudListQueryFilters(
|
|
400
|
-
query,
|
|
401
|
-
{
|
|
402
|
-
idColumn = "id",
|
|
403
|
-
cursor = "",
|
|
404
|
-
applyCursor = true,
|
|
405
|
-
q = "",
|
|
406
|
-
searchColumns = [],
|
|
407
|
-
parentFilters = {},
|
|
408
|
-
parentFilterColumns = {}
|
|
409
|
-
} = {}
|
|
410
|
-
) {
|
|
411
|
-
// readable-check: allow method-probe - safety invariant: a value that is not a query builder fails inside the filter callbacks with an unclear error
|
|
412
|
-
if (!query || typeof query.modify !== "function" || typeof query.where !== "function") {
|
|
413
|
-
throw new TypeError("applyCrudListQueryFilters requires query builder.");
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
const normalizedSearch = String(q || "").trim();
|
|
417
|
-
const normalizedSearchColumns = (Array.isArray(searchColumns) ? searchColumns : [])
|
|
418
|
-
.map((columnName) => String(columnName || "").trim())
|
|
419
|
-
.filter(Boolean);
|
|
420
|
-
|
|
421
|
-
let nextQuery = query;
|
|
422
|
-
|
|
423
|
-
const sourceParentFilters = normalizeObjectInput(parentFilters);
|
|
424
|
-
const normalizedParentFilterColumns = parentFilterColumns && typeof parentFilterColumns === "object" && !Array.isArray(parentFilterColumns)
|
|
425
|
-
? parentFilterColumns
|
|
426
|
-
: {};
|
|
427
|
-
|
|
428
|
-
for (const [fieldKey, columnNameRaw] of Object.entries(normalizedParentFilterColumns)) {
|
|
429
|
-
if (!Object.hasOwn(sourceParentFilters, fieldKey)) {
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
const columnName = String(columnNameRaw || "").trim();
|
|
434
|
-
if (!columnName) {
|
|
435
|
-
continue;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
const sourceValue = sourceParentFilters[fieldKey];
|
|
439
|
-
const normalizedValue = typeof sourceValue === "string"
|
|
440
|
-
? sourceValue.trim()
|
|
441
|
-
: sourceValue;
|
|
442
|
-
if (normalizedValue === "" || normalizedValue === undefined || normalizedValue === null) {
|
|
443
|
-
continue;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
nextQuery = nextQuery.where(columnName, normalizedValue);
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
if (normalizedSearch && normalizedSearchColumns.length > 0) {
|
|
450
|
-
const searchPattern = `%${normalizedSearch}%`;
|
|
451
|
-
nextQuery = nextQuery.modify((searchQuery) => {
|
|
452
|
-
searchQuery.where((whereQuery) => {
|
|
453
|
-
for (const [index, columnName] of normalizedSearchColumns.entries()) {
|
|
454
|
-
if (index === 0) {
|
|
455
|
-
whereQuery.where(columnName, "like", searchPattern);
|
|
456
|
-
continue;
|
|
457
|
-
}
|
|
458
|
-
whereQuery.orWhere(columnName, "like", searchPattern);
|
|
459
|
-
}
|
|
460
|
-
});
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const normalizedIdColumn = String(idColumn || "").trim() || "id";
|
|
465
|
-
if (applyCursor !== false) {
|
|
466
|
-
const normalizedCursor = normalizeCrudListCursor(cursor);
|
|
467
|
-
if (normalizedCursor) {
|
|
468
|
-
nextQuery = nextQuery.where(normalizedIdColumn, ">", normalizedCursor);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
return nextQuery;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function buildWritePayload(sourcePayload = {}, fieldKeys = [], overrides = {}, { serializerByKey = {} } = {}) {
|
|
476
|
-
const source = normalizeObjectInput(sourcePayload);
|
|
477
|
-
const normalizedSerializerByKey = serializerByKey && typeof serializerByKey === "object" && !Array.isArray(serializerByKey)
|
|
478
|
-
? serializerByKey
|
|
479
|
-
: {};
|
|
480
|
-
const payload = {};
|
|
481
|
-
for (const key of fieldKeys) {
|
|
482
|
-
const normalizedKey = String(key || "").trim();
|
|
483
|
-
const columnName = resolveColumnName(normalizedKey, overrides);
|
|
484
|
-
if (!normalizedKey || !columnName) {
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
if (!Object.hasOwn(source, normalizedKey)) {
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
const value = source[normalizedKey];
|
|
491
|
-
const serializerId = normalizeText(normalizedSerializerByKey[normalizedKey]).toLowerCase();
|
|
492
|
-
if (value === null || value === undefined || !serializerId) {
|
|
493
|
-
payload[columnName] = value;
|
|
494
|
-
continue;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const serializer = CRUD_WRITE_SERIALIZERS[serializerId];
|
|
498
|
-
if (typeof serializer !== "function") {
|
|
499
|
-
throw new Error(`crudRepository write serializer "${serializerId}" is not supported.`);
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
payload[columnName] = serializer(value);
|
|
503
|
-
}
|
|
504
|
-
return payload;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
function resolveCrudIdColumn(idColumn, { fallback = "id" } = {}) {
|
|
508
|
-
const normalized = String(idColumn ?? fallback ?? "").trim();
|
|
509
|
-
if (!normalized) {
|
|
510
|
-
throw new TypeError("crudRepository requires idColumn.");
|
|
511
|
-
}
|
|
512
|
-
return normalized;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
export {
|
|
516
|
-
DEFAULT_LIST_LIMIT,
|
|
517
|
-
MAX_LIST_LIMIT,
|
|
518
|
-
normalizeCrudListLimit,
|
|
519
|
-
normalizeCrudListCursor,
|
|
520
|
-
requireCrudTableName,
|
|
521
|
-
deriveRepositoryMappingFromResource,
|
|
522
|
-
applyCrudListQueryFilters,
|
|
523
|
-
mapRecordRow,
|
|
524
|
-
buildWritePayload,
|
|
525
|
-
resolveColumnName,
|
|
526
|
-
resolveCrudIdColumn,
|
|
527
|
-
buildRepositoryColumnMetadata
|
|
528
|
-
};
|