@aeriajs/core 0.0.148 → 0.0.150
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.
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import type { FixedObjectProperty, Description } from '@aeriajs/types';
|
|
2
|
-
|
|
3
|
-
memoize?: string;
|
|
4
|
-
depth?: number;
|
|
5
|
-
maxDepth?: number;
|
|
6
|
-
};
|
|
2
|
+
import type { Document } from 'mongodb';
|
|
7
3
|
export type Reference = {
|
|
8
4
|
isArray?: boolean;
|
|
9
5
|
isArrayElement?: boolean;
|
|
@@ -12,11 +8,17 @@ export type Reference = {
|
|
|
12
8
|
deepReferences?: ReferenceMap;
|
|
13
9
|
referencedCollection?: string;
|
|
14
10
|
indexes?: string[];
|
|
11
|
+
populate?: string[];
|
|
15
12
|
};
|
|
16
13
|
export type ReferenceMap = Record<string, Reference>;
|
|
17
|
-
export type
|
|
14
|
+
export type GetReferenceOptions = {
|
|
15
|
+
memoize?: string;
|
|
16
|
+
depth?: number;
|
|
17
|
+
maxDepth?: number;
|
|
18
|
+
populate?: string[];
|
|
19
|
+
};
|
|
18
20
|
export type BuildLookupPipelineOptions = {
|
|
19
|
-
rootPipeline?:
|
|
21
|
+
rootPipeline?: Document[];
|
|
20
22
|
path?: string[];
|
|
21
23
|
tempNames?: string[];
|
|
22
24
|
memoize?: string;
|
|
@@ -25,6 +27,6 @@ export type BuildLookupPipelineOptions = {
|
|
|
25
27
|
export declare const getReferences: (properties: FixedObjectProperty["properties"], options?: GetReferenceOptions) => Promise<{}>;
|
|
26
28
|
export declare const recurseSetStage: (reference: Reference, path: string[], parentElem: {}, options?: {
|
|
27
29
|
noCond: boolean;
|
|
28
|
-
}) =>
|
|
29
|
-
export declare const buildLookupPipeline: (refMap: ReferenceMap, options?: BuildLookupPipelineOptions) =>
|
|
30
|
-
export declare const getLookupPipeline: (description: Description, options: BuildLookupPipelineOptions) => Promise<
|
|
30
|
+
}) => Document;
|
|
31
|
+
export declare const buildLookupPipeline: (refMap: ReferenceMap, options?: BuildLookupPipelineOptions) => Document[];
|
|
32
|
+
export declare const getLookupPipeline: (description: Description, options: BuildLookupPipelineOptions) => Promise<Document[]>;
|
|
@@ -10,7 +10,7 @@ const getTempName = (path) => {
|
|
|
10
10
|
const referenceMemo = {};
|
|
11
11
|
const lookupMemo = {};
|
|
12
12
|
const getReferences = async (properties, options = {}) => {
|
|
13
|
-
const { depth = 0, maxDepth = 3, memoize, } = options;
|
|
13
|
+
const { depth = 0, maxDepth = 3, memoize, populate, } = options;
|
|
14
14
|
if (memoize) {
|
|
15
15
|
if (referenceMemo[memoize]) {
|
|
16
16
|
return referenceMemo[memoize];
|
|
@@ -18,25 +18,31 @@ const getReferences = async (properties, options = {}) => {
|
|
|
18
18
|
}
|
|
19
19
|
const refMap = {};
|
|
20
20
|
for (const [propName, property] of Object.entries(properties)) {
|
|
21
|
-
const refProperty = (0, common_1.getReferenceProperty)(property);
|
|
22
21
|
const reference = {};
|
|
23
|
-
|
|
22
|
+
const refProperty = (0, common_1.getReferenceProperty)(property);
|
|
23
|
+
if (depth === maxDepth || (populate && !populate.includes(propName))) {
|
|
24
24
|
continue;
|
|
25
25
|
}
|
|
26
26
|
if (refProperty) {
|
|
27
27
|
const description = (0, common_1.throwIfError)(await (0, assets_js_1.getCollectionAsset)(refProperty.$ref, 'description'));
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
if (refProperty.populate) {
|
|
29
|
+
if (refProperty.populate.length === 0) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const deepReferences = await (0, exports.getReferences)(description.properties, {
|
|
33
|
+
depth: depth + 1,
|
|
34
|
+
maxDepth: refProperty.populateDepth || maxDepth,
|
|
35
|
+
memoize: `${memoize}.${propName}`,
|
|
36
|
+
populate: Array.from(refProperty.populate),
|
|
37
|
+
});
|
|
38
|
+
if (Object.keys(deepReferences).length > 0) {
|
|
39
|
+
reference.deepReferences = deepReferences;
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
42
|
const indexes = refProperty.indexes
|
|
37
43
|
? refProperty.indexes
|
|
38
|
-
: description.indexes ||
|
|
39
|
-
reference.
|
|
44
|
+
: description.indexes || [];
|
|
45
|
+
reference.populate = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
|
|
40
46
|
}
|
|
41
47
|
else {
|
|
42
48
|
const entrypoint = 'items' in property
|
|
@@ -269,13 +275,9 @@ const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
269
275
|
const tempName = getTempName(path.concat(refName));
|
|
270
276
|
const lookupPipeline = [];
|
|
271
277
|
tempNames.unshift(tempName);
|
|
272
|
-
if (reference.
|
|
273
|
-
const lookupPopulate = reference.indexes;
|
|
274
|
-
if (reference.deepReferences) {
|
|
275
|
-
lookupPopulate.push(...Object.keys(reference.deepReferences));
|
|
276
|
-
}
|
|
278
|
+
if (reference.populate && reference.populate.length > 0) {
|
|
277
279
|
lookupPipeline.push({
|
|
278
|
-
$project: Object.fromEntries(reference.
|
|
280
|
+
$project: Object.fromEntries(reference.populate.map((index) => [
|
|
279
281
|
index,
|
|
280
282
|
1,
|
|
281
283
|
])),
|
|
@@ -11,7 +11,8 @@ export const getReferences = async (properties, options = {}) => {
|
|
|
11
11
|
const {
|
|
12
12
|
depth = 0,
|
|
13
13
|
maxDepth = 3,
|
|
14
|
-
memoize
|
|
14
|
+
memoize,
|
|
15
|
+
populate
|
|
15
16
|
} = options;
|
|
16
17
|
if (memoize) {
|
|
17
18
|
if (referenceMemo[memoize]) {
|
|
@@ -20,23 +21,29 @@ export const getReferences = async (properties, options = {}) => {
|
|
|
20
21
|
}
|
|
21
22
|
const refMap = {};
|
|
22
23
|
for (const [propName, property] of Object.entries(properties)) {
|
|
23
|
-
const refProperty = getReferenceProperty(property);
|
|
24
24
|
const reference = {};
|
|
25
|
-
|
|
25
|
+
const refProperty = getReferenceProperty(property);
|
|
26
|
+
if (depth === maxDepth || populate && !populate.includes(propName)) {
|
|
26
27
|
continue;
|
|
27
28
|
}
|
|
28
29
|
if (refProperty) {
|
|
29
30
|
const description = throwIfError(await getCollectionAsset(refProperty.$ref, "description"));
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
if (refProperty.populate) {
|
|
32
|
+
if (refProperty.populate.length === 0) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const deepReferences = await getReferences(description.properties, {
|
|
36
|
+
depth: depth + 1,
|
|
37
|
+
maxDepth: refProperty.populateDepth || maxDepth,
|
|
38
|
+
memoize: `${memoize}.${propName}`,
|
|
39
|
+
populate: Array.from(refProperty.populate)
|
|
40
|
+
});
|
|
41
|
+
if (Object.keys(deepReferences).length > 0) {
|
|
42
|
+
reference.deepReferences = deepReferences;
|
|
43
|
+
}
|
|
37
44
|
}
|
|
38
|
-
const indexes = refProperty.indexes ? refProperty.indexes : description.indexes ||
|
|
39
|
-
reference.
|
|
45
|
+
const indexes = refProperty.indexes ? refProperty.indexes : description.indexes || [];
|
|
46
|
+
reference.populate = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === "string"));
|
|
40
47
|
} else {
|
|
41
48
|
const entrypoint = "items" in property ? property.items : property;
|
|
42
49
|
if ("properties" in entrypoint) {
|
|
@@ -258,13 +265,9 @@ export const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
258
265
|
const tempName = getTempName(path.concat(refName));
|
|
259
266
|
const lookupPipeline = [];
|
|
260
267
|
tempNames.unshift(tempName);
|
|
261
|
-
if (reference.
|
|
262
|
-
const lookupPopulate = reference.indexes;
|
|
263
|
-
if (reference.deepReferences) {
|
|
264
|
-
lookupPopulate.push(...Object.keys(reference.deepReferences));
|
|
265
|
-
}
|
|
268
|
+
if (reference.populate && reference.populate.length > 0) {
|
|
266
269
|
lookupPipeline.push({
|
|
267
|
-
$project: Object.fromEntries(reference.
|
|
270
|
+
$project: Object.fromEntries(reference.populate.map((index) => [
|
|
268
271
|
index,
|
|
269
272
|
1
|
|
270
273
|
]))
|
|
@@ -326,6 +326,10 @@ const recurse = async (target, ctx) => {
|
|
|
326
326
|
documents.push(elem);
|
|
327
327
|
continue;
|
|
328
328
|
}
|
|
329
|
+
if (typeof elem === 'string') {
|
|
330
|
+
documents.push(new mongodb_1.ObjectId(elem));
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
329
333
|
const { error, result } = await (0, exports.traverseDocument)(elem, targetDescription, ctx.options);
|
|
330
334
|
if (error) {
|
|
331
335
|
return types_1.Result.error(error);
|
|
@@ -286,6 +286,10 @@ const recurse = async (target, ctx) => {
|
|
|
286
286
|
documents.push(elem);
|
|
287
287
|
continue;
|
|
288
288
|
}
|
|
289
|
+
if (typeof elem === "string") {
|
|
290
|
+
documents.push(new ObjectId(elem));
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
289
293
|
const { error: error2, result } = await traverseDocument(elem, targetDescription, ctx.options);
|
|
290
294
|
if (error2) {
|
|
291
295
|
return Result.error(error2);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.150",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"aeriaMain": "tests/fixtures/aeriaMain.js",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"mongodb-memory-server": "^9.2.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@aeriajs/builtins": "^0.0.
|
|
45
|
+
"@aeriajs/builtins": "^0.0.150",
|
|
46
46
|
"@aeriajs/common": "^0.0.90",
|
|
47
47
|
"@aeriajs/entrypoint": "^0.0.92",
|
|
48
48
|
"@aeriajs/http": "^0.0.102",
|
|
49
|
-
"@aeriajs/security": "^0.0.
|
|
49
|
+
"@aeriajs/security": "^0.0.150",
|
|
50
50
|
"@aeriajs/types": "^0.0.78",
|
|
51
51
|
"@aeriajs/validation": "^0.0.93"
|
|
52
52
|
},
|