@jskit-ai/crud-core 0.2.3 → 0.2.5
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/locales/en.json +2 -1
- package/package.json +7 -7
- package/patterns/json-api-resource-package/PATTERN.md +102 -23
- package/patterns/json-api-resource-package/example/packages/books/package.json +3 -2
- package/patterns/json-api-resource-package/example/packages/books/src/server/types.js +5 -0
- package/src/server/createCrudServiceFromResource.js +14 -6
- package/src/server/createHooksToCollectChildren.js +123 -97
- package/src/server/defineCrudJsonApiFeature.js +40 -8
- package/src/server/fieldAccess.js +33 -25
- package/src/server/jsonApiModule/actions.js +151 -548
- package/src/server/jsonApiModule/assistantExtension.js +386 -0
- package/src/server/jsonApiModule/customActionId.js +12 -0
- package/src/server/jsonApiModule/repository.js +73 -21
- package/src/server/jsonApiModule/routes.js +44 -30
- package/src/server/jsonApiModule/service.js +37 -35
- package/src/server/jsonApiResourceContract.js +22 -18
- package/src/server/listFilterJsonRest.js +304 -0
- package/src/server/listFilterTypes.js +427 -0
- package/src/server/listFilters.js +26 -722
- package/src/server/listQueryValidators.js +26 -21
- package/src/server/lookupPathSupport.js +31 -18
- package/src/server/lookups.js +20 -34
- package/src/server/normalizeOptionalText.js +13 -0
- package/src/server/repositorySupport.js +11 -10
- package/src/server/resourceRuntime/compiledRuntime.js +284 -0
- package/src/server/resourceRuntime/index.js +167 -920
- package/src/server/resourceRuntime/listControls.js +333 -0
- package/src/server/resourceRuntime/lookupHydration.js +121 -186
- package/src/server/resourceRuntime/operationStages.js +140 -0
- package/src/server/resourceRuntime/recordValidation.js +65 -0
- package/src/server/routeContracts.js +44 -46
- package/src/server/serviceEvents.js +42 -118
- package/src/server/serviceMethods.js +34 -26
- package/src/server/types.js +156 -0
- package/test/assistantActionConformance.test.js +8 -5
- package/test/assistantPagination.integration.test.js +5 -3
- package/test/createCrudServiceFromResource.test.js +0 -18
- package/test/customActionId.test.js +12 -0
- package/test/defineCrudJsonApiFeature.test.js +193 -28
- package/test/errorCodes.test.js +18 -0
- package/test/jsonApiRepository.test.js +103 -3
- package/test/listFilters.test.js +7 -7
- package/test/listQueryValidators.test.js +1 -54
- package/test/serviceEvents.test.js +31 -81
- package/test/support/crudJsonApiActionOptions.js +37 -0
- package/src/server/crudModuleConfig.js +0 -181
- package/src/server/lookupHydration.js +0 -710
- package/test/crudModuleConfig.test.js +0 -33
|
@@ -1,429 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
RECORD_ID_PATTERN
|
|
4
|
-
} from "@jskit-ai/kernel/shared/validators";
|
|
5
|
-
import { createSchema } from "json-rest-schema";
|
|
6
|
-
import {
|
|
7
|
-
isRecord as isPlainObject,
|
|
8
|
-
normalizeObject,
|
|
9
|
-
normalizeText
|
|
10
|
-
} from "@jskit-ai/kernel/shared/support/normalize";
|
|
1
|
+
import { normalizeObjectInput } from "@jskit-ai/kernel/shared/validators";
|
|
2
|
+
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
11
3
|
import {
|
|
12
4
|
defineCrudListFilters,
|
|
13
5
|
CRUD_LIST_FILTER_INVALID_VALUES_REJECT,
|
|
14
6
|
CRUD_LIST_FILTER_INVALID_VALUES_DISCARD,
|
|
15
|
-
|
|
16
|
-
CRUD_LIST_FILTER_TYPE_ENUM,
|
|
17
|
-
CRUD_LIST_FILTER_TYPE_ENUM_MANY,
|
|
18
|
-
CRUD_LIST_FILTER_TYPE_RECORD_ID,
|
|
19
|
-
CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY,
|
|
20
|
-
CRUD_LIST_FILTER_TYPE_DATE,
|
|
21
|
-
CRUD_LIST_FILTER_TYPE_DATE_RANGE,
|
|
22
|
-
CRUD_LIST_FILTER_TYPE_NUMBER_RANGE,
|
|
23
|
-
CRUD_LIST_FILTER_TYPE_PRESENCE,
|
|
24
|
-
CRUD_LIST_FILTER_PRESENCE_PRESENT,
|
|
25
|
-
CRUD_LIST_FILTER_PRESENCE_MISSING,
|
|
26
|
-
INVALID_CRUD_LIST_FILTER_QUERY_VALUE,
|
|
27
|
-
normalizeCrudListFilterInvalidValues,
|
|
28
|
-
parseCrudListFilterQueryValue
|
|
7
|
+
normalizeCrudListFilterInvalidValues
|
|
29
8
|
} from "@jskit-ai/resource-crud-core/shared/crudListFilters";
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const looseTextTransportSchema = Object.freeze({
|
|
42
|
-
type: "string",
|
|
43
|
-
minLength: 0
|
|
44
|
-
});
|
|
45
|
-
const strictNumberRangeTransportSchema = Object.freeze({
|
|
46
|
-
anyOf: [
|
|
47
|
-
{
|
|
48
|
-
type: "string",
|
|
49
|
-
pattern: NUMBER_RANGE_FILTER_PATTERN_SOURCE
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
type: "number"
|
|
53
|
-
}
|
|
54
|
-
]
|
|
55
|
-
});
|
|
56
|
-
const looseStringOrNumberTransportSchema = Object.freeze({
|
|
57
|
-
anyOf: [
|
|
58
|
-
looseTextTransportSchema,
|
|
59
|
-
{
|
|
60
|
-
type: "number"
|
|
61
|
-
}
|
|
62
|
-
]
|
|
63
|
-
});
|
|
64
|
-
const recordIdTransportSchema = Object.freeze({
|
|
65
|
-
anyOf: [
|
|
66
|
-
{
|
|
67
|
-
type: "string",
|
|
68
|
-
pattern: RECORD_ID_PATTERN
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
type: "number",
|
|
72
|
-
minimum: 1
|
|
73
|
-
}
|
|
74
|
-
]
|
|
75
|
-
});
|
|
76
|
-
const flagTransportSchema = Object.freeze({
|
|
77
|
-
anyOf: [
|
|
78
|
-
{
|
|
79
|
-
type: "string",
|
|
80
|
-
minLength: 0
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
type: "boolean"
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
type: "number"
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
function cloneTransportSchema(value) {
|
|
92
|
-
if (Array.isArray(value)) {
|
|
93
|
-
return value.map((entry) => cloneTransportSchema(entry));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!value || typeof value !== "object") {
|
|
97
|
-
return value;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return Object.fromEntries(
|
|
101
|
-
Object.entries(value).map(([key, entry]) => [key, cloneTransportSchema(entry)])
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function buildSingleOrMultiTransportSchema(itemSchema) {
|
|
106
|
-
return {
|
|
107
|
-
anyOf: [
|
|
108
|
-
cloneTransportSchema(itemSchema),
|
|
109
|
-
{
|
|
110
|
-
type: "array",
|
|
111
|
-
items: cloneTransportSchema(itemSchema),
|
|
112
|
-
minItems: 1
|
|
113
|
-
}
|
|
114
|
-
]
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function addDaysToDateFilterValue(value = "", days = 0) {
|
|
119
|
-
const normalizedValue = parseCrudListFilterQueryValue(
|
|
120
|
-
{ type: CRUD_LIST_FILTER_TYPE_DATE },
|
|
121
|
-
value,
|
|
122
|
-
{ invalidValues: CRUD_LIST_FILTER_INVALID_VALUES_REJECT }
|
|
123
|
-
);
|
|
124
|
-
if (
|
|
125
|
-
normalizedValue === INVALID_CRUD_LIST_FILTER_QUERY_VALUE ||
|
|
126
|
-
!normalizedValue ||
|
|
127
|
-
!Number.isInteger(days)
|
|
128
|
-
) {
|
|
129
|
-
return "";
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const date = new Date(`${normalizedValue}T00:00:00.000Z`);
|
|
133
|
-
if (Number.isNaN(date.getTime())) {
|
|
134
|
-
return "";
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
date.setUTCDate(date.getUTCDate() + days);
|
|
138
|
-
const year = date.getUTCFullYear();
|
|
139
|
-
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
|
|
140
|
-
const day = String(date.getUTCDate()).padStart(2, "0");
|
|
141
|
-
return `${year}-${month}-${day}`;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const FILTER_TYPE_SERVER_HANDLERS = Object.freeze({
|
|
145
|
-
[CRUD_LIST_FILTER_TYPE_FLAG]: Object.freeze({
|
|
146
|
-
buildTransportSchema() {
|
|
147
|
-
return cloneTransportSchema(flagTransportSchema);
|
|
148
|
-
},
|
|
149
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
150
|
-
if (column && value === true) {
|
|
151
|
-
queryBuilder.where(column, true);
|
|
152
|
-
}
|
|
153
|
-
return queryBuilder;
|
|
154
|
-
}
|
|
155
|
-
}),
|
|
156
|
-
[CRUD_LIST_FILTER_TYPE_ENUM]: Object.freeze({
|
|
157
|
-
buildTransportSchema({ discardInvalidValues, allowedValues }) {
|
|
158
|
-
return discardInvalidValues
|
|
159
|
-
? cloneTransportSchema(looseTextTransportSchema)
|
|
160
|
-
: {
|
|
161
|
-
type: "string",
|
|
162
|
-
enum: allowedValues
|
|
163
|
-
};
|
|
164
|
-
},
|
|
165
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
166
|
-
if (column && value !== undefined) {
|
|
167
|
-
queryBuilder.where(column, value);
|
|
168
|
-
}
|
|
169
|
-
return queryBuilder;
|
|
170
|
-
}
|
|
171
|
-
}),
|
|
172
|
-
[CRUD_LIST_FILTER_TYPE_ENUM_MANY]: Object.freeze({
|
|
173
|
-
buildTransportSchema({ discardInvalidValues, allowedValues }) {
|
|
174
|
-
return buildSingleOrMultiTransportSchema(
|
|
175
|
-
discardInvalidValues
|
|
176
|
-
? looseTextTransportSchema
|
|
177
|
-
: {
|
|
178
|
-
type: "string",
|
|
179
|
-
enum: allowedValues
|
|
180
|
-
}
|
|
181
|
-
);
|
|
182
|
-
},
|
|
183
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
184
|
-
if (column && Array.isArray(value) && value.length > 0) {
|
|
185
|
-
queryBuilder.whereIn(column, value);
|
|
186
|
-
}
|
|
187
|
-
return queryBuilder;
|
|
188
|
-
}
|
|
189
|
-
}),
|
|
190
|
-
[CRUD_LIST_FILTER_TYPE_RECORD_ID]: Object.freeze({
|
|
191
|
-
buildTransportSchema({ discardInvalidValues }) {
|
|
192
|
-
return discardInvalidValues
|
|
193
|
-
? cloneTransportSchema(looseStringOrNumberTransportSchema)
|
|
194
|
-
: cloneTransportSchema(recordIdTransportSchema);
|
|
195
|
-
},
|
|
196
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
197
|
-
if (column && value !== undefined) {
|
|
198
|
-
queryBuilder.where(column, value);
|
|
199
|
-
}
|
|
200
|
-
return queryBuilder;
|
|
201
|
-
}
|
|
202
|
-
}),
|
|
203
|
-
[CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY]: Object.freeze({
|
|
204
|
-
buildTransportSchema({ discardInvalidValues }) {
|
|
205
|
-
return buildSingleOrMultiTransportSchema(
|
|
206
|
-
discardInvalidValues
|
|
207
|
-
? looseStringOrNumberTransportSchema
|
|
208
|
-
: recordIdTransportSchema
|
|
209
|
-
);
|
|
210
|
-
},
|
|
211
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
212
|
-
if (column && Array.isArray(value) && value.length > 0) {
|
|
213
|
-
queryBuilder.whereIn(column, value);
|
|
214
|
-
}
|
|
215
|
-
return queryBuilder;
|
|
216
|
-
}
|
|
217
|
-
}),
|
|
218
|
-
[CRUD_LIST_FILTER_TYPE_DATE]: Object.freeze({
|
|
219
|
-
buildTransportSchema({ discardInvalidValues }) {
|
|
220
|
-
return discardInvalidValues
|
|
221
|
-
? cloneTransportSchema(looseTextTransportSchema)
|
|
222
|
-
: {
|
|
223
|
-
type: "string",
|
|
224
|
-
pattern: DATE_FILTER_PATTERN_SOURCE
|
|
225
|
-
};
|
|
226
|
-
},
|
|
227
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
228
|
-
if (!column || !value) {
|
|
229
|
-
return queryBuilder;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const nextDate = addDaysToDateFilterValue(value, 1);
|
|
233
|
-
queryBuilder.where(column, ">=", `${value} 00:00:00`);
|
|
234
|
-
if (nextDate) {
|
|
235
|
-
queryBuilder.where(column, "<", `${nextDate} 00:00:00`);
|
|
236
|
-
}
|
|
237
|
-
return queryBuilder;
|
|
238
|
-
}
|
|
239
|
-
}),
|
|
240
|
-
[CRUD_LIST_FILTER_TYPE_DATE_RANGE]: Object.freeze({
|
|
241
|
-
buildTransportSchema({ discardInvalidValues }) {
|
|
242
|
-
return discardInvalidValues
|
|
243
|
-
? cloneTransportSchema(looseTextTransportSchema)
|
|
244
|
-
: {
|
|
245
|
-
type: "string",
|
|
246
|
-
pattern: DATE_RANGE_FILTER_PATTERN_SOURCE
|
|
247
|
-
};
|
|
248
|
-
},
|
|
249
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
250
|
-
if (!column) {
|
|
251
|
-
return queryBuilder;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (value?.from) {
|
|
255
|
-
queryBuilder.where(column, ">=", `${value.from} 00:00:00`);
|
|
256
|
-
}
|
|
257
|
-
if (value?.to) {
|
|
258
|
-
const nextDate = addDaysToDateFilterValue(value.to, 1);
|
|
259
|
-
if (nextDate) {
|
|
260
|
-
queryBuilder.where(column, "<", `${nextDate} 00:00:00`);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return queryBuilder;
|
|
264
|
-
}
|
|
265
|
-
}),
|
|
266
|
-
[CRUD_LIST_FILTER_TYPE_NUMBER_RANGE]: Object.freeze({
|
|
267
|
-
buildTransportSchema({ discardInvalidValues }) {
|
|
268
|
-
return discardInvalidValues
|
|
269
|
-
? cloneTransportSchema(looseStringOrNumberTransportSchema)
|
|
270
|
-
: cloneTransportSchema(strictNumberRangeTransportSchema);
|
|
271
|
-
},
|
|
272
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
273
|
-
if (!column) {
|
|
274
|
-
return queryBuilder;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (value?.min != null) {
|
|
278
|
-
queryBuilder.where(column, ">=", value.min);
|
|
279
|
-
}
|
|
280
|
-
if (value?.max != null) {
|
|
281
|
-
queryBuilder.where(column, "<=", value.max);
|
|
282
|
-
}
|
|
283
|
-
return queryBuilder;
|
|
284
|
-
}
|
|
285
|
-
}),
|
|
286
|
-
[CRUD_LIST_FILTER_TYPE_PRESENCE]: Object.freeze({
|
|
287
|
-
buildTransportSchema({ discardInvalidValues, allowedValues }) {
|
|
288
|
-
return discardInvalidValues
|
|
289
|
-
? cloneTransportSchema(looseTextTransportSchema)
|
|
290
|
-
: {
|
|
291
|
-
type: "string",
|
|
292
|
-
enum: allowedValues
|
|
293
|
-
};
|
|
294
|
-
},
|
|
295
|
-
applyQuery(queryBuilder, value, column = "") {
|
|
296
|
-
if (!column) {
|
|
297
|
-
return queryBuilder;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
if (value === "present") {
|
|
301
|
-
queryBuilder.whereNotNull(column);
|
|
302
|
-
}
|
|
303
|
-
if (value === "missing") {
|
|
304
|
-
queryBuilder.whereNull(column);
|
|
305
|
-
}
|
|
306
|
-
return queryBuilder;
|
|
307
|
-
}
|
|
308
|
-
})
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
function normalizeCrudListFilterQueryFieldInput(filterDefinition = null) {
|
|
312
|
-
if (!filterDefinition || typeof filterDefinition !== "object" || Array.isArray(filterDefinition)) {
|
|
313
|
-
throw new TypeError("createCrudListFilterQueryField requires a normalized filter definition object.");
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const key = normalizeText(filterDefinition.key);
|
|
317
|
-
const type = normalizeText(filterDefinition.type);
|
|
318
|
-
const queryKey = normalizeText(filterDefinition.queryKey);
|
|
319
|
-
if (!key || !type || !queryKey) {
|
|
320
|
-
throw new TypeError(
|
|
321
|
-
"createCrudListFilterQueryField requires a normalized filter definition from defineCrudListFilters(...)."
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
if (!FILTER_TYPE_SERVER_HANDLERS[type]) {
|
|
325
|
-
throw new TypeError(`Unsupported CRUD list filter type "${type}".`);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
return filterDefinition;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function createCrudListFilterQueryField(filterDefinition = {}, {
|
|
332
|
-
invalidValues = CRUD_LIST_FILTER_INVALID_VALUES_REJECT
|
|
333
|
-
} = {}) {
|
|
334
|
-
const invalidValueMode = normalizeCrudListFilterInvalidValues(invalidValues);
|
|
335
|
-
const filter = normalizeCrudListFilterQueryFieldInput(filterDefinition);
|
|
336
|
-
const filterContract = Object.freeze({
|
|
337
|
-
filter,
|
|
338
|
-
invalidValues: invalidValueMode
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
return Object.freeze({
|
|
342
|
-
type: CRUD_LIST_FILTER_QUERY_TYPE,
|
|
343
|
-
filterContract
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
function buildFilterQueryFieldDefinition(filterDefinition = {}, { invalidValues = CRUD_LIST_FILTER_INVALID_VALUES_REJECT } = {}) {
|
|
348
|
-
const filter = normalizeCrudListFilterQueryFieldInput(filterDefinition);
|
|
349
|
-
return {
|
|
350
|
-
[filter.queryKey]: createCrudListFilterQueryField(filter, {
|
|
351
|
-
invalidValues
|
|
352
|
-
})
|
|
353
|
-
};
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function buildFilterQueryTransportSchema(filter = {}, { invalidValues = CRUD_LIST_FILTER_INVALID_VALUES_REJECT } = {}) {
|
|
357
|
-
const invalidValueMode = normalizeCrudListFilterInvalidValues(invalidValues);
|
|
358
|
-
const discardInvalidValues = invalidValueMode === CRUD_LIST_FILTER_INVALID_VALUES_DISCARD;
|
|
359
|
-
const allowedValues = (Array.isArray(filter.options) ? filter.options : []).map((entry) => entry.value);
|
|
360
|
-
const handler = FILTER_TYPE_SERVER_HANDLERS[filter.type];
|
|
361
|
-
return handler
|
|
362
|
-
? handler.buildTransportSchema({ discardInvalidValues, allowedValues })
|
|
363
|
-
: {};
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
crudListFilterSchemaFactory.addType(CRUD_LIST_FILTER_QUERY_TYPE, Object.assign(
|
|
367
|
-
(context) => {
|
|
368
|
-
const contract = isPlainObject(context.definition.filterContract)
|
|
369
|
-
? context.definition.filterContract
|
|
370
|
-
: null;
|
|
371
|
-
const filter = contract?.filter;
|
|
372
|
-
const invalidValues = contract?.invalidValues;
|
|
373
|
-
|
|
374
|
-
if (!filter || !invalidValues) {
|
|
375
|
-
context.throwTypeError();
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const parsedValue = parseCrudListFilterQueryValue(filter, context.value, {
|
|
379
|
-
invalidValues
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
if (parsedValue === INVALID_CRUD_LIST_FILTER_QUERY_VALUE) {
|
|
383
|
-
context.throwTypeError();
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
return parsedValue;
|
|
387
|
-
},
|
|
388
|
-
{
|
|
389
|
-
toJsonSchema({ definition }) {
|
|
390
|
-
const contract = isPlainObject(definition?.filterContract)
|
|
391
|
-
? definition.filterContract
|
|
392
|
-
: null;
|
|
393
|
-
const filter = contract?.filter;
|
|
394
|
-
const invalidValues = contract?.invalidValues;
|
|
395
|
-
|
|
396
|
-
if (!filter || !invalidValues) {
|
|
397
|
-
throw new Error(`Type "${CRUD_LIST_FILTER_QUERY_TYPE}" requires definition.filterContract for transport export.`);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
return buildFilterQueryTransportSchema(filter, {
|
|
401
|
-
invalidValues
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
));
|
|
406
|
-
|
|
407
|
-
function createCrudListFilterQuerySchema(structure = {}) {
|
|
408
|
-
return crudListFilterSchemaFactory(structure);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function buildFilterQuerySchemaDefinition(filterEntries = [], {
|
|
412
|
-
invalidValues = CRUD_LIST_FILTER_INVALID_VALUES_REJECT
|
|
413
|
-
} = {}) {
|
|
414
|
-
const structure = {};
|
|
415
|
-
|
|
416
|
-
for (const filter of filterEntries) {
|
|
417
|
-
Object.assign(structure, buildFilterQueryFieldDefinition(filter, {
|
|
418
|
-
invalidValues
|
|
419
|
-
}));
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return Object.freeze({
|
|
423
|
-
schema: createCrudListFilterQuerySchema(structure),
|
|
424
|
-
mode: "patch"
|
|
425
|
-
});
|
|
426
|
-
}
|
|
9
|
+
import {
|
|
10
|
+
normalizeColumnsMap,
|
|
11
|
+
createCrudListFilterJsonRestSearchSchema,
|
|
12
|
+
normalizeCrudListFilterJsonRestQuery
|
|
13
|
+
} from "./listFilterJsonRest.js";
|
|
14
|
+
import {
|
|
15
|
+
buildCrudListFilterQueryField,
|
|
16
|
+
createCrudListFilterQuerySchema,
|
|
17
|
+
buildFilterQuerySchemaDefinition,
|
|
18
|
+
applyDefaultFilterQuery
|
|
19
|
+
} from "./listFilterTypes.js";
|
|
427
20
|
|
|
428
21
|
function projectNormalizedFilterValues(filterEntries = [], source = {}, errors = {}) {
|
|
429
22
|
const normalizedSource = normalizeObjectInput(source);
|
|
@@ -449,299 +42,6 @@ function projectNormalizedFilterValues(filterEntries = [], source = {}, errors =
|
|
|
449
42
|
return Object.freeze(normalized);
|
|
450
43
|
}
|
|
451
44
|
|
|
452
|
-
function normalizeColumnsMap(columns = {}) {
|
|
453
|
-
const source = normalizeObject(columns);
|
|
454
|
-
const normalized = {};
|
|
455
|
-
for (const [key, value] of Object.entries(source)) {
|
|
456
|
-
const normalizedKey = normalizeText(key);
|
|
457
|
-
const normalizedValue = normalizeText(value);
|
|
458
|
-
if (!normalizedKey || !normalizedValue) {
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
normalized[normalizedKey] = normalizedValue;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
return Object.freeze(normalized);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
function resolveJsonRestFilterActualField(filter = {}, columns = {}) {
|
|
469
|
-
const meta = isPlainObject(filter.meta?.jsonRest) ? filter.meta.jsonRest : {};
|
|
470
|
-
return normalizeText(meta.actualField || meta.column || columns[filter.key] || filter.key);
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
function renderInternalJsonRestFilterKey(filter = {}, suffix = "") {
|
|
474
|
-
return [
|
|
475
|
-
JSON_REST_INTERNAL_FILTER_PREFIX,
|
|
476
|
-
filter.queryKey,
|
|
477
|
-
suffix
|
|
478
|
-
]
|
|
479
|
-
.map((entry) => normalizeText(entry))
|
|
480
|
-
.filter(Boolean)
|
|
481
|
-
.join("_");
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function createJsonRestSearchSchemaEntry({
|
|
485
|
-
type = "string",
|
|
486
|
-
actualField = "",
|
|
487
|
-
filterOperator = "=",
|
|
488
|
-
extra = {}
|
|
489
|
-
} = {}) {
|
|
490
|
-
return {
|
|
491
|
-
type,
|
|
492
|
-
...(actualField ? { actualField } : {}),
|
|
493
|
-
...(filterOperator ? { filterOperator } : {}),
|
|
494
|
-
...extra
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function buildJsonRestFilterItemSchema(filter = {}) {
|
|
499
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_ENUM_MANY) {
|
|
500
|
-
return {
|
|
501
|
-
type: "string",
|
|
502
|
-
enum: (Array.isArray(filter.options) ? filter.options : []).map((entry) => entry.value)
|
|
503
|
-
};
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY) {
|
|
507
|
-
return cloneTransportSchema(recordIdTransportSchema);
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
return {};
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
function buildJsonRestSimpleFilterSearchSchemaEntry(filter = {}, { actualField = "" } = {}) {
|
|
514
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_FLAG) {
|
|
515
|
-
return createJsonRestSearchSchemaEntry({
|
|
516
|
-
type: "boolean",
|
|
517
|
-
actualField,
|
|
518
|
-
filterOperator: "="
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_ENUM) {
|
|
523
|
-
return createJsonRestSearchSchemaEntry({
|
|
524
|
-
type: "string",
|
|
525
|
-
actualField,
|
|
526
|
-
filterOperator: "=",
|
|
527
|
-
extra: {
|
|
528
|
-
enum: (Array.isArray(filter.options) ? filter.options : []).map((entry) => entry.value)
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_ENUM_MANY || filter.type === CRUD_LIST_FILTER_TYPE_RECORD_ID_MANY) {
|
|
534
|
-
return createJsonRestSearchSchemaEntry({
|
|
535
|
-
type: "array",
|
|
536
|
-
actualField,
|
|
537
|
-
filterOperator: "in",
|
|
538
|
-
extra: {
|
|
539
|
-
items: buildJsonRestFilterItemSchema(filter)
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_RECORD_ID) {
|
|
545
|
-
return createJsonRestSearchSchemaEntry({
|
|
546
|
-
type: "id",
|
|
547
|
-
actualField,
|
|
548
|
-
filterOperator: "="
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
return null;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
function buildJsonRestRangeFilterSearchSchemaEntries(filter = {}, { actualField = "" } = {}) {
|
|
556
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_DATE) {
|
|
557
|
-
return {
|
|
558
|
-
[renderInternalJsonRestFilterKey(filter, "from")]: createJsonRestSearchSchemaEntry({
|
|
559
|
-
type: "dateTime",
|
|
560
|
-
actualField,
|
|
561
|
-
filterOperator: ">="
|
|
562
|
-
}),
|
|
563
|
-
[renderInternalJsonRestFilterKey(filter, "toExclusive")]: createJsonRestSearchSchemaEntry({
|
|
564
|
-
type: "dateTime",
|
|
565
|
-
actualField,
|
|
566
|
-
filterOperator: "<"
|
|
567
|
-
})
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_DATE_RANGE) {
|
|
572
|
-
return {
|
|
573
|
-
[renderInternalJsonRestFilterKey(filter, "from")]: createJsonRestSearchSchemaEntry({
|
|
574
|
-
type: "dateTime",
|
|
575
|
-
actualField,
|
|
576
|
-
filterOperator: ">="
|
|
577
|
-
}),
|
|
578
|
-
[renderInternalJsonRestFilterKey(filter, "toExclusive")]: createJsonRestSearchSchemaEntry({
|
|
579
|
-
type: "dateTime",
|
|
580
|
-
actualField,
|
|
581
|
-
filterOperator: "<"
|
|
582
|
-
})
|
|
583
|
-
};
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_NUMBER_RANGE) {
|
|
587
|
-
return {
|
|
588
|
-
[renderInternalJsonRestFilterKey(filter, "min")]: createJsonRestSearchSchemaEntry({
|
|
589
|
-
type: "number",
|
|
590
|
-
actualField,
|
|
591
|
-
filterOperator: ">="
|
|
592
|
-
}),
|
|
593
|
-
[renderInternalJsonRestFilterKey(filter, "max")]: createJsonRestSearchSchemaEntry({
|
|
594
|
-
type: "number",
|
|
595
|
-
actualField,
|
|
596
|
-
filterOperator: "<="
|
|
597
|
-
})
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
return {};
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
function buildJsonRestPresenceFilterSearchSchemaEntry(filter = {}, { actualField = "" } = {}) {
|
|
605
|
-
return {
|
|
606
|
-
type: "string",
|
|
607
|
-
enum: [CRUD_LIST_FILTER_PRESENCE_PRESENT, CRUD_LIST_FILTER_PRESENCE_MISSING],
|
|
608
|
-
applyFilter(queryBuilder, value) {
|
|
609
|
-
applyDefaultFilterQuery(queryBuilder, filter, value, actualField);
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
function buildJsonRestCustomFilterSearchSchemaEntry(filter = {}, customApply = null) {
|
|
615
|
-
return {
|
|
616
|
-
type: "none",
|
|
617
|
-
applyFilter(queryBuilder, value) {
|
|
618
|
-
customApply(queryBuilder, value, {
|
|
619
|
-
filter,
|
|
620
|
-
filters: Object.freeze({
|
|
621
|
-
[filter.key]: value
|
|
622
|
-
})
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function resolveCrudListFilterEntries(value = {}) {
|
|
629
|
-
if (isPlainObject(value?.filters)) {
|
|
630
|
-
return Object.values(normalizeObject(value.filters));
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
return Object.values(defineCrudListFilters(value));
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function createCrudListFilterJsonRestSearchSchema(runtime = {}, { columns = {}, apply = {} } = {}) {
|
|
637
|
-
const filterEntries = resolveCrudListFilterEntries(runtime);
|
|
638
|
-
const normalizedColumns = normalizeColumnsMap(columns);
|
|
639
|
-
const schema = {};
|
|
640
|
-
|
|
641
|
-
for (const filter of filterEntries) {
|
|
642
|
-
const customApply = typeof apply?.[filter.key] === "function" ? apply[filter.key] : null;
|
|
643
|
-
if (customApply) {
|
|
644
|
-
schema[filter.queryKey] = buildJsonRestCustomFilterSearchSchemaEntry(filter, customApply);
|
|
645
|
-
continue;
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
const actualField = resolveJsonRestFilterActualField(filter, normalizedColumns);
|
|
649
|
-
if (!actualField) {
|
|
650
|
-
continue;
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
const simpleEntry = buildJsonRestSimpleFilterSearchSchemaEntry(filter, { actualField });
|
|
654
|
-
if (simpleEntry) {
|
|
655
|
-
schema[filter.queryKey] = simpleEntry;
|
|
656
|
-
continue;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_PRESENCE) {
|
|
660
|
-
schema[filter.queryKey] = buildJsonRestPresenceFilterSearchSchemaEntry(filter, { actualField });
|
|
661
|
-
continue;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
Object.assign(schema, buildJsonRestRangeFilterSearchSchemaEntries(filter, { actualField }));
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
return Object.freeze(schema);
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
function addJsonRestDateFilterValues(target = {}, filter = {}, value = "") {
|
|
671
|
-
const nextDate = addDaysToDateFilterValue(value, 1);
|
|
672
|
-
if (value) {
|
|
673
|
-
target[renderInternalJsonRestFilterKey(filter, "from")] = `${value} 00:00:00`;
|
|
674
|
-
}
|
|
675
|
-
if (nextDate) {
|
|
676
|
-
target[renderInternalJsonRestFilterKey(filter, "toExclusive")] = `${nextDate} 00:00:00`;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
function addJsonRestFilterValue(target = {}, filter = {}, value) {
|
|
681
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_DATE) {
|
|
682
|
-
addJsonRestDateFilterValues(target, filter, value);
|
|
683
|
-
return;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_DATE_RANGE) {
|
|
687
|
-
if (value?.from) {
|
|
688
|
-
target[renderInternalJsonRestFilterKey(filter, "from")] = `${value.from} 00:00:00`;
|
|
689
|
-
}
|
|
690
|
-
if (value?.to) {
|
|
691
|
-
const nextDate = addDaysToDateFilterValue(value.to, 1);
|
|
692
|
-
if (nextDate) {
|
|
693
|
-
target[renderInternalJsonRestFilterKey(filter, "toExclusive")] = `${nextDate} 00:00:00`;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
return;
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
if (filter.type === CRUD_LIST_FILTER_TYPE_NUMBER_RANGE) {
|
|
700
|
-
if (value?.min != null) {
|
|
701
|
-
target[renderInternalJsonRestFilterKey(filter, "min")] = value.min;
|
|
702
|
-
}
|
|
703
|
-
if (value?.max != null) {
|
|
704
|
-
target[renderInternalJsonRestFilterKey(filter, "max")] = value.max;
|
|
705
|
-
}
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
target[filter.queryKey] = value;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
function normalizeCrudListFilterJsonRestQuery(runtime = {}, query = {}) {
|
|
713
|
-
const source = normalizeObjectInput(query);
|
|
714
|
-
const filterEntries = Object.values(normalizeObject(runtime?.filters));
|
|
715
|
-
const filterQueryKeys = new Set(filterEntries.map((filter) => filter.queryKey));
|
|
716
|
-
const parsedFilters = typeof runtime.parseQuery === "function"
|
|
717
|
-
? runtime.parseQuery(source)
|
|
718
|
-
: {};
|
|
719
|
-
const normalized = {};
|
|
720
|
-
|
|
721
|
-
for (const [key, value] of Object.entries(source)) {
|
|
722
|
-
if (!filterQueryKeys.has(key)) {
|
|
723
|
-
normalized[key] = value;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
for (const filter of filterEntries) {
|
|
728
|
-
if (!Object.hasOwn(parsedFilters, filter.key)) {
|
|
729
|
-
continue;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
addJsonRestFilterValue(normalized, filter, parsedFilters[filter.key]);
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
return normalized;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
function applyDefaultFilterQuery(queryBuilder, filter = {}, value, column = "") {
|
|
739
|
-
const handler = FILTER_TYPE_SERVER_HANDLERS[filter.type];
|
|
740
|
-
return handler
|
|
741
|
-
? handler.applyQuery(queryBuilder, value, column)
|
|
742
|
-
: queryBuilder;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
45
|
function createCrudListFilters(definitions = {}, { columns = {}, apply = {} } = {}) {
|
|
746
46
|
const normalizedFilters = defineCrudListFilters(definitions);
|
|
747
47
|
const normalizedColumns = normalizeColumnsMap(columns);
|
|
@@ -766,6 +66,7 @@ function createCrudListFilters(definitions = {}, { columns = {}, apply = {} } =
|
|
|
766
66
|
}
|
|
767
67
|
|
|
768
68
|
function applyQuery(queryBuilder, payload = {}) {
|
|
69
|
+
// readable-check: allow method-probe - input validation: the application passes its own query builder
|
|
769
70
|
if (!queryBuilder || typeof queryBuilder.where !== "function") {
|
|
770
71
|
throw new TypeError("createCrudListFilters.applyQuery requires query builder.");
|
|
771
72
|
}
|
|
@@ -777,7 +78,8 @@ function createCrudListFilters(definitions = {}, { columns = {}, apply = {} } =
|
|
|
777
78
|
}
|
|
778
79
|
|
|
779
80
|
const value = normalized[filter.key];
|
|
780
|
-
|
|
81
|
+
// readable-check: allow method-probe - input validation: apply holds an application's optional custom filters; other filters use the default
|
|
82
|
+
const customApply = typeof apply[filter.key] === "function"
|
|
781
83
|
? apply[filter.key]
|
|
782
84
|
: null;
|
|
783
85
|
if (customApply) {
|
|
@@ -799,16 +101,18 @@ function createCrudListFilters(definitions = {}, { columns = {}, apply = {} } =
|
|
|
799
101
|
return queryValidatorsByInvalidValueMode[invalidValueMode];
|
|
800
102
|
}
|
|
801
103
|
|
|
104
|
+
function toJsonRestQuery(query = {}) {
|
|
105
|
+
return normalizeCrudListFilterJsonRestQuery({
|
|
106
|
+
filters: normalizedFilters,
|
|
107
|
+
parseQuery: parseFilterPayload
|
|
108
|
+
}, query);
|
|
109
|
+
}
|
|
110
|
+
|
|
802
111
|
return Object.freeze({
|
|
803
112
|
filters: normalizedFilters,
|
|
804
113
|
createQueryValidator,
|
|
805
114
|
parseQuery: parseFilterPayload,
|
|
806
|
-
toJsonRestQuery
|
|
807
|
-
return normalizeCrudListFilterJsonRestQuery({
|
|
808
|
-
filters: normalizedFilters,
|
|
809
|
-
parseQuery: parseFilterPayload
|
|
810
|
-
}, query);
|
|
811
|
-
},
|
|
115
|
+
toJsonRestQuery,
|
|
812
116
|
jsonRestSearchSchema: createCrudListFilterJsonRestSearchSchema({
|
|
813
117
|
filters: normalizedFilters
|
|
814
118
|
}, {
|
|
@@ -839,7 +143,7 @@ function createCrudListFilterContract(definitions = {}, {
|
|
|
839
143
|
export {
|
|
840
144
|
CRUD_LIST_FILTER_INVALID_VALUES_REJECT,
|
|
841
145
|
CRUD_LIST_FILTER_INVALID_VALUES_DISCARD,
|
|
842
|
-
|
|
146
|
+
buildCrudListFilterQueryField,
|
|
843
147
|
createCrudListFilterQuerySchema,
|
|
844
148
|
createCrudListFilterJsonRestSearchSchema,
|
|
845
149
|
createCrudListFilters,
|