@jskit-ai/crud-core 0.2.3 → 0.2.4
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,27 +1,22 @@
|
|
|
1
|
-
import { normalizeOpaqueId, normalizeText, normalizeUniqueTextList } from "@jskit-ai/kernel/shared/support/normalize";
|
|
1
|
+
import { isRecord, normalizeOpaqueId, normalizeText, normalizeUniqueTextList } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
2
|
import { normalizeVisibilityContext } from "@jskit-ai/kernel/shared/support/visibility";
|
|
3
3
|
import {
|
|
4
|
+
normalizeCrudLookupApiPath,
|
|
4
5
|
normalizeCrudLookupNamespace,
|
|
5
6
|
resolveCrudLookupApiPathFromNamespace,
|
|
6
|
-
normalizeCrudLookupContainerKey,
|
|
7
7
|
resolveCrudLookupContainerKey
|
|
8
8
|
} from "@jskit-ai/resource-crud-core/shared/crudLookup";
|
|
9
9
|
import { buildCrudFieldContractMap } from "@jskit-ai/resource-crud-core/shared/crudFieldContract";
|
|
10
|
-
import {
|
|
10
|
+
import { normalizeLookupOwnershipFilter } from "../lookupPathSupport.js";
|
|
11
11
|
|
|
12
12
|
const DEFAULT_LOOKUP_INCLUDE = "*";
|
|
13
13
|
const DEFAULT_LOOKUP_MAX_DEPTH = 3;
|
|
14
14
|
const MAX_LOOKUP_MAX_DEPTH = 10;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
]);
|
|
21
|
-
const LOOKUP_OWNERSHIP_FILTER_SET = new Set(LOOKUP_OWNERSHIP_FILTER_VALUES);
|
|
22
|
-
|
|
23
|
-
function normalizeLookupRelationEntry(entry = {}, outputKeys = new Set()) {
|
|
24
|
-
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
15
|
+
|
|
16
|
+
/** @typedef {ReturnType<typeof buildCrudLookupRuntime>} LookupRuntime */
|
|
17
|
+
|
|
18
|
+
function normalizeLookupRelationEntry(entry, outputKeys) {
|
|
19
|
+
if (!isRecord(entry)) {
|
|
25
20
|
return null;
|
|
26
21
|
}
|
|
27
22
|
|
|
@@ -31,7 +26,7 @@ function normalizeLookupRelationEntry(entry = {}, outputKeys = new Set()) {
|
|
|
31
26
|
}
|
|
32
27
|
|
|
33
28
|
const relation = entry.relation;
|
|
34
|
-
if (!
|
|
29
|
+
if (!isRecord(relation)) {
|
|
35
30
|
return null;
|
|
36
31
|
}
|
|
37
32
|
|
|
@@ -50,22 +45,22 @@ function normalizeLookupRelationEntry(entry = {}, outputKeys = new Set()) {
|
|
|
50
45
|
const apiPath = explicitApiPath || resolveCrudLookupApiPathFromNamespace(namespace);
|
|
51
46
|
|
|
52
47
|
if (relationKind === "lookup") {
|
|
53
|
-
if (
|
|
48
|
+
if (!outputKeys.has(key)) {
|
|
54
49
|
return null;
|
|
55
50
|
}
|
|
56
51
|
const valueKey = normalizeText(relation.valueKey) || "id";
|
|
57
52
|
|
|
58
|
-
return {
|
|
53
|
+
return Object.freeze({
|
|
59
54
|
key,
|
|
60
|
-
relation: {
|
|
55
|
+
relation: Object.freeze({
|
|
61
56
|
kind: "lookup",
|
|
62
57
|
namespace,
|
|
63
58
|
apiPath,
|
|
64
59
|
valueKey,
|
|
65
60
|
hydrateOnList: relation.hydrateOnList !== false,
|
|
66
61
|
hydrateOnView: relation.hydrateOnView !== false
|
|
67
|
-
}
|
|
68
|
-
};
|
|
62
|
+
})
|
|
63
|
+
});
|
|
69
64
|
}
|
|
70
65
|
|
|
71
66
|
const foreignKey = normalizeText(relation.foreignKey);
|
|
@@ -74,13 +69,13 @@ function normalizeLookupRelationEntry(entry = {}, outputKeys = new Set()) {
|
|
|
74
69
|
}
|
|
75
70
|
|
|
76
71
|
const parentValueKey = normalizeText(relation.parentValueKey) || "id";
|
|
77
|
-
if (
|
|
72
|
+
if (!outputKeys.has(parentValueKey)) {
|
|
78
73
|
return null;
|
|
79
74
|
}
|
|
80
75
|
|
|
81
|
-
return {
|
|
76
|
+
return Object.freeze({
|
|
82
77
|
key,
|
|
83
|
-
relation: {
|
|
78
|
+
relation: Object.freeze({
|
|
84
79
|
kind: "collection",
|
|
85
80
|
namespace,
|
|
86
81
|
apiPath,
|
|
@@ -88,8 +83,8 @@ function normalizeLookupRelationEntry(entry = {}, outputKeys = new Set()) {
|
|
|
88
83
|
parentValueKey,
|
|
89
84
|
hydrateOnList: relation.hydrateOnList !== false,
|
|
90
85
|
hydrateOnView: relation.hydrateOnView !== false
|
|
91
|
-
}
|
|
92
|
-
};
|
|
86
|
+
})
|
|
87
|
+
});
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
function normalizeLookupDefaultInclude(value) {
|
|
@@ -114,8 +109,8 @@ function normalizeLookupMaxDepth(value) {
|
|
|
114
109
|
return Math.min(parsed, MAX_LOOKUP_MAX_DEPTH);
|
|
115
110
|
}
|
|
116
111
|
|
|
117
|
-
function resolveLookupRuntimeDefaults(resource
|
|
118
|
-
const lookupContract = resource
|
|
112
|
+
function resolveLookupRuntimeDefaults(resource) {
|
|
113
|
+
const lookupContract = resource.contract?.lookup;
|
|
119
114
|
if (
|
|
120
115
|
lookupContract !== undefined &&
|
|
121
116
|
lookupContract !== null &&
|
|
@@ -130,12 +125,15 @@ function resolveLookupRuntimeDefaults(resource = {}) {
|
|
|
130
125
|
};
|
|
131
126
|
}
|
|
132
127
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
128
|
+
/**
|
|
129
|
+
* A resource's lookup relations, resolved once: the error context, the
|
|
130
|
+
* resource's namespace and lookup container key, the default include and
|
|
131
|
+
* depth, and each lookup entry, in order and by key.
|
|
132
|
+
* @param {import("../types.js").CrudResourceDefinition} resource
|
|
133
|
+
* @param {{ outputKeys: readonly string[], context: string }} options
|
|
134
|
+
*/
|
|
135
|
+
function buildCrudLookupRuntime(resource, { outputKeys, context }) {
|
|
136
|
+
const outputKeySet = new Set(outputKeys);
|
|
139
137
|
|
|
140
138
|
const sourceEntries = Object.values(buildCrudFieldContractMap(resource, {
|
|
141
139
|
context: "crud lookup runtime field contract"
|
|
@@ -165,33 +163,32 @@ function createCrudLookupRuntime(resource = {}, { outputKeys = [] } = {}) {
|
|
|
165
163
|
context: "crud lookup runtime container key"
|
|
166
164
|
});
|
|
167
165
|
const namespace =
|
|
168
|
-
normalizeCrudLookupNamespace(resource
|
|
169
|
-
normalizeCrudLookupNamespace(resource
|
|
166
|
+
normalizeCrudLookupNamespace(resource.namespace) ||
|
|
167
|
+
normalizeCrudLookupNamespace(resource.apiPath);
|
|
170
168
|
const defaults = resolveLookupRuntimeDefaults(resource);
|
|
171
169
|
|
|
172
|
-
return {
|
|
170
|
+
return Object.freeze({
|
|
171
|
+
context,
|
|
173
172
|
namespace,
|
|
174
173
|
containerKey,
|
|
175
174
|
defaultInclude: defaults.defaultInclude,
|
|
176
175
|
maxDepth: defaults.maxDepth,
|
|
177
|
-
entries: lookupEntries,
|
|
178
|
-
byKey: lookupEntryByKey
|
|
179
|
-
};
|
|
176
|
+
entries: Object.freeze(lookupEntries),
|
|
177
|
+
byKey: Object.freeze(lookupEntryByKey)
|
|
178
|
+
});
|
|
180
179
|
}
|
|
181
180
|
|
|
182
181
|
function normalizeLookupIdentifier(value) {
|
|
183
|
-
|
|
184
|
-
if (normalized == null) {
|
|
185
|
-
return "";
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return normalizeText(normalized);
|
|
182
|
+
return normalizeOpaqueId(value, { fallback: "" });
|
|
189
183
|
}
|
|
190
184
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
185
|
+
/**
|
|
186
|
+
* @param {unknown} include
|
|
187
|
+
* @param {string} defaultInclude The lookup runtime's normalized default include.
|
|
188
|
+
*/
|
|
189
|
+
function normalizeIncludePaths(include, defaultInclude) {
|
|
190
|
+
const normalizedInclude = normalizeText(include) || defaultInclude;
|
|
191
|
+
if (normalizedInclude.toLowerCase() === "none") {
|
|
195
192
|
return [];
|
|
196
193
|
}
|
|
197
194
|
|
|
@@ -221,31 +218,21 @@ function normalizeIncludePaths(include, { defaultInclude = DEFAULT_LOOKUP_INCLUD
|
|
|
221
218
|
return paths;
|
|
222
219
|
}
|
|
223
220
|
|
|
224
|
-
|
|
225
|
-
|
|
221
|
+
/** @param {string[][]} paths Normalized include paths below one lookup. */
|
|
222
|
+
function resolveChildIncludeFromPaths(paths) {
|
|
226
223
|
const pathKeys = new Set();
|
|
227
224
|
let includesAll = false;
|
|
228
225
|
|
|
229
|
-
for (const path of
|
|
230
|
-
if (
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const normalizedPath = path
|
|
235
|
-
.map((entry) => normalizeText(entry))
|
|
236
|
-
.filter(Boolean);
|
|
237
|
-
if (normalizedPath.length < 1) {
|
|
226
|
+
for (const path of paths) {
|
|
227
|
+
if (path.length < 1) {
|
|
238
228
|
continue;
|
|
239
229
|
}
|
|
240
|
-
if (
|
|
230
|
+
if (path[0] === "*") {
|
|
241
231
|
includesAll = true;
|
|
242
232
|
continue;
|
|
243
233
|
}
|
|
244
234
|
|
|
245
|
-
|
|
246
|
-
if (childPath) {
|
|
247
|
-
pathKeys.add(childPath);
|
|
248
|
-
}
|
|
235
|
+
pathKeys.add(path.join("."));
|
|
249
236
|
}
|
|
250
237
|
|
|
251
238
|
if (includesAll) {
|
|
@@ -257,27 +244,22 @@ function resolveChildIncludeFromPaths(paths = []) {
|
|
|
257
244
|
return [...pathKeys].join(",");
|
|
258
245
|
}
|
|
259
246
|
|
|
260
|
-
function shouldHydrateByMode(entry
|
|
261
|
-
const relation = entry
|
|
247
|
+
function shouldHydrateByMode(entry, mode) {
|
|
248
|
+
const relation = entry.relation;
|
|
262
249
|
if (mode === "view") {
|
|
263
250
|
return relation.hydrateOnView !== false;
|
|
264
251
|
}
|
|
265
252
|
return relation.hydrateOnList !== false;
|
|
266
253
|
}
|
|
267
254
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
} = {}
|
|
277
|
-
) {
|
|
278
|
-
const includePaths = normalizeIncludePaths(include, {
|
|
279
|
-
defaultInclude: runtime?.defaultInclude
|
|
280
|
-
});
|
|
255
|
+
/**
|
|
256
|
+
* @param {LookupRuntime} runtime
|
|
257
|
+
* @param {unknown} include
|
|
258
|
+
* @param {{ mode: string, includeWasExplicit: boolean, skippedNamespaces: Set<string> }} options
|
|
259
|
+
*/
|
|
260
|
+
function buildLookupHydrationPlan(runtime, include, { mode, includeWasExplicit, skippedNamespaces }) {
|
|
261
|
+
const { context, entries } = runtime;
|
|
262
|
+
const includePaths = normalizeIncludePaths(include, runtime.defaultInclude);
|
|
281
263
|
if (includePaths.length < 1) {
|
|
282
264
|
return {
|
|
283
265
|
entries: [],
|
|
@@ -285,11 +267,8 @@ function buildLookupHydrationPlan(
|
|
|
285
267
|
};
|
|
286
268
|
}
|
|
287
269
|
|
|
288
|
-
const entries = Array.isArray(runtime?.entries) ? runtime.entries : [];
|
|
289
270
|
if (entries.length < 1) {
|
|
290
|
-
const wildcardOnlyInclude =
|
|
291
|
-
includePaths.length > 0 &&
|
|
292
|
-
includePaths.every((pathSegments) => Array.isArray(pathSegments) && pathSegments[0] === "*");
|
|
271
|
+
const wildcardOnlyInclude = includePaths.every((pathSegments) => pathSegments[0] === "*");
|
|
293
272
|
if (includeWasExplicit === true && wildcardOnlyInclude !== true) {
|
|
294
273
|
throw new Error(`${context} include requires lookups, but the resource declares no lookup relations.`);
|
|
295
274
|
}
|
|
@@ -303,8 +282,8 @@ function buildLookupHydrationPlan(
|
|
|
303
282
|
const childIncludeByKey = {};
|
|
304
283
|
const seenKeys = new Set();
|
|
305
284
|
|
|
306
|
-
function appendEntry(entry
|
|
307
|
-
if (
|
|
285
|
+
function appendEntry(entry, childPaths) {
|
|
286
|
+
if (seenKeys.has(entry.key)) {
|
|
308
287
|
return;
|
|
309
288
|
}
|
|
310
289
|
|
|
@@ -312,8 +291,7 @@ function buildLookupHydrationPlan(
|
|
|
312
291
|
return;
|
|
313
292
|
}
|
|
314
293
|
|
|
315
|
-
|
|
316
|
-
if (relationNamespace && skippedNamespaces instanceof Set && skippedNamespaces.has(relationNamespace)) {
|
|
294
|
+
if (skippedNamespaces.has(entry.relation.namespace)) {
|
|
317
295
|
return;
|
|
318
296
|
}
|
|
319
297
|
|
|
@@ -329,17 +307,13 @@ function buildLookupHydrationPlan(
|
|
|
329
307
|
}
|
|
330
308
|
}
|
|
331
309
|
|
|
332
|
-
const entryByKey = runtime?.byKey && typeof runtime.byKey === "object" ? runtime.byKey : {};
|
|
333
310
|
for (const pathSegments of includePaths) {
|
|
334
|
-
if (!Array.isArray(pathSegments) || pathSegments.length < 1) {
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
311
|
const [head, ...tail] = pathSegments;
|
|
338
312
|
if (head === "*") {
|
|
339
313
|
continue;
|
|
340
314
|
}
|
|
341
315
|
|
|
342
|
-
const entry =
|
|
316
|
+
const entry = runtime.byKey[head];
|
|
343
317
|
if (!entry) {
|
|
344
318
|
throw new Error(`${context} include references unknown lookup key "${head}".`);
|
|
345
319
|
}
|
|
@@ -358,20 +332,21 @@ function buildLookupHydrationPlan(
|
|
|
358
332
|
};
|
|
359
333
|
}
|
|
360
334
|
|
|
361
|
-
function resolveLookupResolver(repositoryOptions
|
|
362
|
-
const resolver = callOptions
|
|
335
|
+
function resolveLookupResolver(repositoryOptions, callOptions, { context }) {
|
|
336
|
+
const resolver = callOptions.resolveLookup || repositoryOptions.resolveLookup;
|
|
337
|
+
// readable-check: allow method-probe - input validation: the application passes resolveLookup(relation) in its options
|
|
363
338
|
if (typeof resolver !== "function") {
|
|
364
339
|
throw new TypeError(`${context} requires resolveLookup(relation) to hydrate lookups.`);
|
|
365
340
|
}
|
|
366
341
|
return resolver;
|
|
367
342
|
}
|
|
368
343
|
|
|
369
|
-
function resolveLookupDepthRuntime(runtime
|
|
344
|
+
function resolveLookupDepthRuntime(runtime, repositoryOptions, callOptions) {
|
|
370
345
|
const maxDepth = normalizeLookupMaxDepth(
|
|
371
|
-
callOptions
|
|
346
|
+
callOptions.lookupMaxDepth ?? repositoryOptions.lookupMaxDepth ?? runtime.maxDepth
|
|
372
347
|
);
|
|
373
348
|
|
|
374
|
-
const parsedDepth = Number(callOptions
|
|
349
|
+
const parsedDepth = Number(callOptions.lookupDepth);
|
|
375
350
|
const depth = Number.isInteger(parsedDepth) && parsedDepth >= 0 ? parsedDepth : 0;
|
|
376
351
|
|
|
377
352
|
return {
|
|
@@ -380,14 +355,14 @@ function resolveLookupDepthRuntime(runtime = {}, repositoryOptions = {}, callOpt
|
|
|
380
355
|
};
|
|
381
356
|
}
|
|
382
357
|
|
|
383
|
-
function buildLookupGroupKey(relation
|
|
384
|
-
if (relation
|
|
358
|
+
function buildLookupGroupKey(relation) {
|
|
359
|
+
if (relation.kind === "collection") {
|
|
385
360
|
return `collection::${relation.namespace}::${relation.foreignKey}::${relation.parentValueKey}`;
|
|
386
361
|
}
|
|
387
362
|
return `lookup::${relation.namespace}::${relation.valueKey}`;
|
|
388
363
|
}
|
|
389
364
|
|
|
390
|
-
function normalizeLookupRelationValues(records
|
|
365
|
+
function normalizeLookupRelationValues(records, entries, childIncludeByKey) {
|
|
391
366
|
const byGroup = new Map();
|
|
392
367
|
for (const entry of entries) {
|
|
393
368
|
const relation = entry.relation;
|
|
@@ -403,17 +378,17 @@ function normalizeLookupRelationValues(records = [], entries = [], childIncludeB
|
|
|
403
378
|
|
|
404
379
|
const group = byGroup.get(groupKey);
|
|
405
380
|
group.entries.push(entry);
|
|
406
|
-
group.childIncludes.add(normalizeText(childIncludeByKey
|
|
381
|
+
group.childIncludes.add(normalizeText(childIncludeByKey[entry.key]) || "none");
|
|
407
382
|
}
|
|
408
383
|
|
|
409
384
|
for (const group of byGroup.values()) {
|
|
410
385
|
const seen = new Set();
|
|
411
386
|
for (const record of records) {
|
|
412
387
|
for (const entry of group.entries) {
|
|
413
|
-
const relation = entry
|
|
388
|
+
const relation = entry.relation;
|
|
414
389
|
const rawValue = relation.kind === "collection"
|
|
415
|
-
? record
|
|
416
|
-
: record
|
|
390
|
+
? record[relation.parentValueKey]
|
|
391
|
+
: record[entry.key];
|
|
417
392
|
const normalized = normalizeLookupIdentifier(rawValue);
|
|
418
393
|
if (!normalized || seen.has(normalized)) {
|
|
419
394
|
continue;
|
|
@@ -427,12 +402,11 @@ function normalizeLookupRelationValues(records = [], entries = [], childIncludeB
|
|
|
427
402
|
return byGroup;
|
|
428
403
|
}
|
|
429
404
|
|
|
430
|
-
function resolveGroupChildInclude(group
|
|
431
|
-
const sourceIncludes = group?.childIncludes instanceof Set ? [...group.childIncludes] : [];
|
|
405
|
+
function resolveGroupChildInclude(group) {
|
|
432
406
|
const includeSet = new Set();
|
|
433
407
|
let includeAll = false;
|
|
434
408
|
|
|
435
|
-
for (const includeValue of
|
|
409
|
+
for (const includeValue of group.childIncludes) {
|
|
436
410
|
const normalized = normalizeText(includeValue);
|
|
437
411
|
if (!normalized || normalized.toLowerCase() === "none") {
|
|
438
412
|
continue;
|
|
@@ -461,40 +435,22 @@ function resolveGroupChildInclude(group = {}) {
|
|
|
461
435
|
return [...includeSet].join(",");
|
|
462
436
|
}
|
|
463
437
|
|
|
464
|
-
function normalizeLookup(provider, relation
|
|
465
|
-
|
|
438
|
+
function normalizeLookup(provider, relation, { context }) {
|
|
439
|
+
// readable-check: allow method-probe - input validation: an application's resolveLookup must return a provider object
|
|
440
|
+
if (!isRecord(provider)) {
|
|
466
441
|
throw new Error(`${context} could not resolve lookup "${relation.namespace}".`);
|
|
467
442
|
}
|
|
443
|
+
// readable-check: allow method-probe - input validation: an application's resolveLookup must return a provider with listByIds()
|
|
468
444
|
if (typeof provider.listByIds !== "function") {
|
|
469
445
|
throw new Error(`${context} lookup "${relation.namespace}" must expose listByIds(ids, options).`);
|
|
470
446
|
}
|
|
471
447
|
return provider;
|
|
472
448
|
}
|
|
473
449
|
|
|
474
|
-
function
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (LOOKUP_OWNERSHIP_FILTER_SET.has(normalizedOwnershipFilter)) {
|
|
481
|
-
return normalizedOwnershipFilter;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
throw new TypeError(
|
|
485
|
-
`${context} lookup ownershipFilter must be one of: ${LOOKUP_OWNERSHIP_FILTER_VALUES.join(", ")}.`
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
function resolveLookupVisibilityContext(
|
|
490
|
-
provider = {},
|
|
491
|
-
relation = {},
|
|
492
|
-
callOptions = {},
|
|
493
|
-
{ context = "crudRepository" } = {}
|
|
494
|
-
) {
|
|
495
|
-
const parentVisibilityContext = normalizeVisibilityContext(callOptions?.visibilityContext);
|
|
496
|
-
const lookupOwnershipFilter = resolveLookupOwnershipFilter(provider, {
|
|
497
|
-
context
|
|
450
|
+
function resolveLookupVisibilityContext(provider, relation, callOptions, { context }) {
|
|
451
|
+
const parentVisibilityContext = normalizeVisibilityContext(callOptions.visibilityContext);
|
|
452
|
+
const lookupOwnershipFilter = normalizeLookupOwnershipFilter(provider.ownershipFilter, {
|
|
453
|
+
context: `${context} lookup ownershipFilter`
|
|
498
454
|
});
|
|
499
455
|
|
|
500
456
|
if (!lookupOwnershipFilter) {
|
|
@@ -503,7 +459,7 @@ function resolveLookupVisibilityContext(
|
|
|
503
459
|
`${context} lookup "${relation.namespace}" must declare ownershipFilter when parent visibility is "${parentVisibilityContext.visibility}".`
|
|
504
460
|
);
|
|
505
461
|
}
|
|
506
|
-
return callOptions
|
|
462
|
+
return callOptions.visibilityContext;
|
|
507
463
|
}
|
|
508
464
|
|
|
509
465
|
const nextVisibilityContext = {
|
|
@@ -519,7 +475,7 @@ function resolveLookupVisibilityContext(
|
|
|
519
475
|
return nextVisibilityContext;
|
|
520
476
|
}
|
|
521
477
|
|
|
522
|
-
function buildLookupRecordMap(records
|
|
478
|
+
function buildLookupRecordMap(records, valueKey) {
|
|
523
479
|
const lookupMap = new Map();
|
|
524
480
|
for (const record of Array.isArray(records) ? records : []) {
|
|
525
481
|
const lookupId = normalizeLookupIdentifier(record?.[valueKey]);
|
|
@@ -531,7 +487,7 @@ function buildLookupRecordMap(records = [], valueKey = "") {
|
|
|
531
487
|
return lookupMap;
|
|
532
488
|
}
|
|
533
489
|
|
|
534
|
-
function buildLookupCollectionMap(records
|
|
490
|
+
function buildLookupCollectionMap(records, foreignKey) {
|
|
535
491
|
const collectionMap = new Map();
|
|
536
492
|
for (const record of Array.isArray(records) ? records : []) {
|
|
537
493
|
const ownerId = normalizeLookupIdentifier(record?.[foreignKey]);
|
|
@@ -548,7 +504,7 @@ function buildLookupCollectionMap(records = [], foreignKey = "") {
|
|
|
548
504
|
return collectionMap;
|
|
549
505
|
}
|
|
550
506
|
|
|
551
|
-
function resolveLookupVisitedNamespaces(runtime
|
|
507
|
+
function resolveLookupVisitedNamespaces(runtime, callOptions) {
|
|
552
508
|
const namespaces = [];
|
|
553
509
|
const seen = new Set();
|
|
554
510
|
|
|
@@ -561,58 +517,49 @@ function resolveLookupVisitedNamespaces(runtime = {}, callOptions = {}) {
|
|
|
561
517
|
namespaces.push(normalized);
|
|
562
518
|
}
|
|
563
519
|
|
|
564
|
-
const sourceVisitedNamespaces = Array.isArray(callOptions
|
|
520
|
+
const sourceVisitedNamespaces = Array.isArray(callOptions.lookupVisitedNamespaces)
|
|
565
521
|
? callOptions.lookupVisitedNamespaces
|
|
566
522
|
: [];
|
|
567
523
|
for (const namespace of sourceVisitedNamespaces) {
|
|
568
524
|
appendNamespace(namespace);
|
|
569
525
|
}
|
|
570
|
-
appendNamespace(runtime
|
|
526
|
+
appendNamespace(runtime.namespace);
|
|
571
527
|
|
|
572
528
|
return namespaces;
|
|
573
529
|
}
|
|
574
530
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
const sourceRecords = Array.isArray(records) ? records : [];
|
|
586
|
-
if (sourceRecords.length < 1) {
|
|
587
|
-
return sourceRecords;
|
|
531
|
+
/**
|
|
532
|
+
* Records with their lookups hydrated under the lookup container key, up to the lookup depth.
|
|
533
|
+
* @param {object[]} records
|
|
534
|
+
* @param {LookupRuntime} runtime
|
|
535
|
+
* @param {{ include: unknown, mode: "list" | "view", repositoryOptions: object, callOptions: object }} options
|
|
536
|
+
*/
|
|
537
|
+
async function hydrateCrudLookupRecords(records, runtime, { include, mode, repositoryOptions, callOptions }) {
|
|
538
|
+
const { context, containerKey: lookupContainerKey } = runtime;
|
|
539
|
+
if (records.length < 1) {
|
|
540
|
+
return records;
|
|
588
541
|
}
|
|
589
542
|
|
|
590
543
|
const depthRuntime = resolveLookupDepthRuntime(runtime, repositoryOptions, callOptions);
|
|
591
544
|
if (depthRuntime.depth >= depthRuntime.maxDepth) {
|
|
592
|
-
return
|
|
545
|
+
return records;
|
|
593
546
|
}
|
|
594
547
|
|
|
595
|
-
const lookupContainerKey = normalizeCrudLookupContainerKey(runtime?.containerKey, {
|
|
596
|
-
context: `${runtime?.context || "crudRepository"} lookup runtime container key`
|
|
597
|
-
});
|
|
598
548
|
const visitedNamespaces = resolveLookupVisitedNamespaces(runtime, callOptions);
|
|
599
549
|
|
|
600
550
|
const lookupPlan = buildLookupHydrationPlan(runtime, include, {
|
|
601
551
|
mode,
|
|
602
|
-
context: runtime?.context || "crudRepository",
|
|
603
552
|
includeWasExplicit: normalizeText(include).length > 0,
|
|
604
553
|
skippedNamespaces: new Set(visitedNamespaces)
|
|
605
554
|
});
|
|
606
555
|
const selectedEntries = lookupPlan.entries;
|
|
607
556
|
if (selectedEntries.length < 1) {
|
|
608
|
-
return
|
|
557
|
+
return records;
|
|
609
558
|
}
|
|
610
559
|
|
|
611
|
-
const resolveLookup = resolveLookupResolver(repositoryOptions, callOptions, {
|
|
612
|
-
context: runtime?.context || "crudRepository"
|
|
613
|
-
});
|
|
560
|
+
const resolveLookup = resolveLookupResolver(repositoryOptions, callOptions, { context });
|
|
614
561
|
|
|
615
|
-
const relationGroups = normalizeLookupRelationValues(
|
|
562
|
+
const relationGroups = normalizeLookupRelationValues(records, selectedEntries, lookupPlan.childIncludeByKey);
|
|
616
563
|
const groupRecordMaps = new Map();
|
|
617
564
|
for (const group of relationGroups.values()) {
|
|
618
565
|
if (group.values.length < 1) {
|
|
@@ -620,19 +567,10 @@ async function hydrateCrudLookupRecords(
|
|
|
620
567
|
continue;
|
|
621
568
|
}
|
|
622
569
|
|
|
623
|
-
const provider = normalizeLookup(resolveLookup(group.relation), group.relation, {
|
|
624
|
-
|
|
625
|
-
});
|
|
626
|
-
const childVisibilityContext = resolveLookupVisibilityContext(
|
|
627
|
-
provider,
|
|
628
|
-
group.relation,
|
|
629
|
-
callOptions,
|
|
630
|
-
{
|
|
631
|
-
context: runtime?.context || "crudRepository"
|
|
632
|
-
}
|
|
633
|
-
);
|
|
570
|
+
const provider = normalizeLookup(resolveLookup(group.relation), group.relation, { context });
|
|
571
|
+
const childVisibilityContext = resolveLookupVisibilityContext(provider, group.relation, callOptions, { context });
|
|
634
572
|
const childInclude = resolveGroupChildInclude(group);
|
|
635
|
-
const groupValueKey = group
|
|
573
|
+
const groupValueKey = group.relation.kind === "collection"
|
|
636
574
|
? group.relation.foreignKey
|
|
637
575
|
: group.relation.valueKey;
|
|
638
576
|
const groupRecords = await provider.listByIds(group.values, {
|
|
@@ -646,31 +584,28 @@ async function hydrateCrudLookupRecords(
|
|
|
646
584
|
});
|
|
647
585
|
groupRecordMaps.set(
|
|
648
586
|
buildLookupGroupKey(group.relation),
|
|
649
|
-
group
|
|
587
|
+
group.relation.kind === "collection"
|
|
650
588
|
? buildLookupCollectionMap(groupRecords, group.relation.foreignKey)
|
|
651
589
|
: buildLookupRecordMap(groupRecords, group.relation.valueKey)
|
|
652
590
|
);
|
|
653
591
|
}
|
|
654
592
|
|
|
655
|
-
return
|
|
656
|
-
const baseRecord =
|
|
657
|
-
const existingLookups =
|
|
658
|
-
baseRecord[lookupContainerKey]
|
|
659
|
-
|
|
660
|
-
!Array.isArray(baseRecord[lookupContainerKey])
|
|
661
|
-
? baseRecord[lookupContainerKey]
|
|
662
|
-
: {};
|
|
593
|
+
return records.map((record) => {
|
|
594
|
+
const baseRecord = isRecord(record) ? record : {};
|
|
595
|
+
const existingLookups = isRecord(baseRecord[lookupContainerKey])
|
|
596
|
+
? baseRecord[lookupContainerKey]
|
|
597
|
+
: {};
|
|
663
598
|
const nextLookups = { ...existingLookups };
|
|
664
599
|
|
|
665
600
|
for (const entry of selectedEntries) {
|
|
666
601
|
const relation = entry.relation;
|
|
667
602
|
const lookupMap = groupRecordMaps.get(buildLookupGroupKey(relation)) || new Map();
|
|
668
603
|
if (relation.kind === "collection") {
|
|
669
|
-
const ownerId = normalizeLookupIdentifier(baseRecord
|
|
604
|
+
const ownerId = normalizeLookupIdentifier(baseRecord[relation.parentValueKey]);
|
|
670
605
|
nextLookups[entry.key] = ownerId ? (lookupMap.get(ownerId) || []) : [];
|
|
671
606
|
continue;
|
|
672
607
|
}
|
|
673
|
-
const lookupId = normalizeLookupIdentifier(baseRecord
|
|
608
|
+
const lookupId = normalizeLookupIdentifier(baseRecord[entry.key]);
|
|
674
609
|
if (!lookupId) {
|
|
675
610
|
nextLookups[entry.key] = null;
|
|
676
611
|
continue;
|
|
@@ -686,6 +621,6 @@ async function hydrateCrudLookupRecords(
|
|
|
686
621
|
}
|
|
687
622
|
|
|
688
623
|
export {
|
|
689
|
-
|
|
624
|
+
buildCrudLookupRuntime,
|
|
690
625
|
hydrateCrudLookupRecords
|
|
691
626
|
};
|