@aeriajs/core 0.0.201 → 0.0.203
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.
|
@@ -20,14 +20,16 @@ export type GetReferenceOptions = {
|
|
|
20
20
|
};
|
|
21
21
|
export type BuildLookupPipelineOptions = {
|
|
22
22
|
rootPipeline?: Document[];
|
|
23
|
-
path?:
|
|
23
|
+
path?: PathSegment[];
|
|
24
24
|
tempNames?: string[];
|
|
25
25
|
memoize?: string;
|
|
26
26
|
project?: string[];
|
|
27
27
|
};
|
|
28
|
+
type PathSegment = [segment: string, isRef: boolean];
|
|
28
29
|
export declare const getReferences: (properties: FixedObjectProperty["properties"], options?: GetReferenceOptions) => Promise<ReferenceMap>;
|
|
29
|
-
export declare const recurseSetStage: (reference: Reference, path:
|
|
30
|
+
export declare const recurseSetStage: (reference: Reference, path: PathSegment[], parentElem: {}, options?: {
|
|
30
31
|
noCond: boolean;
|
|
31
32
|
}) => Document;
|
|
32
33
|
export declare const buildLookupPipeline: (refMap: ReferenceMap, options?: BuildLookupPipelineOptions) => Document[];
|
|
33
34
|
export declare const getLookupPipeline: (description: Description, options: BuildLookupPipelineOptions) => Promise<Document[]>;
|
|
35
|
+
export {};
|
|
@@ -4,7 +4,7 @@ exports.getLookupPipeline = exports.buildLookupPipeline = exports.recurseSetStag
|
|
|
4
4
|
const common_1 = require("@aeriajs/common");
|
|
5
5
|
const assets_js_1 = require("../assets.js");
|
|
6
6
|
const getTempName = (path) => {
|
|
7
|
-
return `_${path.join('_')}`;
|
|
7
|
+
return `_${path.map(([segment]) => segment).join('_')}`;
|
|
8
8
|
};
|
|
9
9
|
const referenceMemo = {};
|
|
10
10
|
const lookupMemo = {};
|
|
@@ -96,26 +96,36 @@ exports.getReferences = getReferences;
|
|
|
96
96
|
const recurseSetStage = (reference, path, parentElem, options = {
|
|
97
97
|
noCond: false,
|
|
98
98
|
}) => {
|
|
99
|
-
const refName = path.at(-1);
|
|
99
|
+
const [refName, isRef] = path.at(-1);
|
|
100
100
|
const shouldUseArrayIndex = reference.isRecursive && !(reference.isArrayElement && reference.isArray === false);
|
|
101
101
|
let indexOfArray;
|
|
102
102
|
if (shouldUseArrayIndex) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
103
|
+
if (isRef) {
|
|
104
|
+
indexOfArray = {
|
|
105
|
+
$indexOfArray: [
|
|
106
|
+
`$${getTempName(path)}._id`,
|
|
107
|
+
{
|
|
108
|
+
$arrayElemAt: [
|
|
109
|
+
`$${getTempName(path.slice(0, -1))}.${refName}`,
|
|
110
|
+
{
|
|
111
|
+
$indexOfArray: [
|
|
112
|
+
`$${getTempName(path.slice(0, -1))}._id`,
|
|
113
|
+
parentElem,
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
indexOfArray = {
|
|
123
|
+
$indexOfArray: [
|
|
124
|
+
`$${getTempName(path.slice(0, -1))}._id`,
|
|
125
|
+
parentElem,
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
119
129
|
}
|
|
120
130
|
else {
|
|
121
131
|
indexOfArray = {
|
|
@@ -193,19 +203,34 @@ const recurseSetStage = (reference, path, parentElem, options = {
|
|
|
193
203
|
for (const [subRefName, subReference] of Object.entries(reference.deepReferences)) {
|
|
194
204
|
let newElem;
|
|
195
205
|
if (shouldUseArrayIndex) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
206
|
+
if (isRef) {
|
|
207
|
+
newElem = {
|
|
208
|
+
$arrayElemAt: [
|
|
209
|
+
`$${getTempName(path.slice(0, -1))}.${refName}`,
|
|
210
|
+
indexOfArray,
|
|
211
|
+
],
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
newElem = {
|
|
216
|
+
$arrayElemAt: [
|
|
217
|
+
`$${getTempName(path.slice(0, -1))}.${refName}.${subRefName}`,
|
|
218
|
+
indexOfArray,
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
}
|
|
202
222
|
}
|
|
203
223
|
else {
|
|
204
224
|
newElem = reference.referencedCollection
|
|
205
225
|
? parentElem
|
|
206
226
|
: `${parentElem}.${subRefName}`;
|
|
207
227
|
}
|
|
208
|
-
const result = (0, exports.recurseSetStage)(subReference, path.concat(
|
|
228
|
+
const result = (0, exports.recurseSetStage)(subReference, path.concat([
|
|
229
|
+
[
|
|
230
|
+
subRefName,
|
|
231
|
+
'referencedCollection' in subReference,
|
|
232
|
+
],
|
|
233
|
+
]), newElem);
|
|
209
234
|
stages.push([
|
|
210
235
|
subRefName,
|
|
211
236
|
result,
|
|
@@ -278,20 +303,26 @@ const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
278
303
|
continue;
|
|
279
304
|
}
|
|
280
305
|
}
|
|
306
|
+
const newPath = path.concat([
|
|
307
|
+
[
|
|
308
|
+
refName,
|
|
309
|
+
'referencedCollection' in reference,
|
|
310
|
+
],
|
|
311
|
+
]);
|
|
281
312
|
if (reference.deepReferences) {
|
|
282
313
|
(0, exports.buildLookupPipeline)(reference.deepReferences, {
|
|
283
314
|
rootPipeline,
|
|
284
315
|
tempNames,
|
|
285
|
-
path:
|
|
316
|
+
path: newPath,
|
|
286
317
|
});
|
|
287
|
-
const result = (0, exports.recurseSetStage)(reference,
|
|
318
|
+
const result = (0, exports.recurseSetStage)(reference, newPath, `$${refName}`);
|
|
288
319
|
setProperties.push([
|
|
289
320
|
refName,
|
|
290
321
|
result,
|
|
291
322
|
]);
|
|
292
323
|
}
|
|
293
324
|
if (reference.referencedCollection) {
|
|
294
|
-
const tempName = getTempName(
|
|
325
|
+
const tempName = getTempName(newPath);
|
|
295
326
|
const lookupPipeline = [];
|
|
296
327
|
tempNames.unshift(tempName);
|
|
297
328
|
if (reference.populate && reference.populate.length > 0) {
|
|
@@ -302,9 +333,16 @@ const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
302
333
|
])),
|
|
303
334
|
});
|
|
304
335
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
336
|
+
let localField;
|
|
337
|
+
if (reference.isRecursive) {
|
|
338
|
+
localField = `${getTempName(path)}.${refName}`;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const localFieldPath = newPath.map(([segment]) => segment).join('.');
|
|
342
|
+
localField = path[0] && path[0][1]
|
|
343
|
+
? `_${localFieldPath}`
|
|
344
|
+
: localFieldPath;
|
|
345
|
+
}
|
|
308
346
|
rootPipeline.unshift({
|
|
309
347
|
$lookup: {
|
|
310
348
|
from: reference.referencedCollection,
|
|
@@ -315,7 +353,7 @@ const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
315
353
|
},
|
|
316
354
|
});
|
|
317
355
|
if (!reference.deepReferences) {
|
|
318
|
-
const result = (0, exports.recurseSetStage)(reference,
|
|
356
|
+
const result = (0, exports.recurseSetStage)(reference, newPath, `$${refName}`);
|
|
319
357
|
setProperties.push([
|
|
320
358
|
refName,
|
|
321
359
|
result,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { throwIfError, getReferenceProperty } from "@aeriajs/common";
|
|
3
3
|
import { getCollectionAsset } from "../assets.mjs";
|
|
4
4
|
const getTempName = (path) => {
|
|
5
|
-
return `_${path.join("_")}`;
|
|
5
|
+
return `_${path.map(([segment]) => segment).join("_")}`;
|
|
6
6
|
};
|
|
7
7
|
const referenceMemo = {};
|
|
8
8
|
const lookupMemo = {};
|
|
@@ -87,26 +87,35 @@ export const getReferences = async (properties, options = {}) => {
|
|
|
87
87
|
export const recurseSetStage = (reference, path, parentElem, options = {
|
|
88
88
|
noCond: false
|
|
89
89
|
}) => {
|
|
90
|
-
const refName = path.at(-1);
|
|
90
|
+
const [refName, isRef] = path.at(-1);
|
|
91
91
|
const shouldUseArrayIndex = reference.isRecursive && !(reference.isArrayElement && reference.isArray === false);
|
|
92
92
|
let indexOfArray;
|
|
93
93
|
if (shouldUseArrayIndex) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
94
|
+
if (isRef) {
|
|
95
|
+
indexOfArray = {
|
|
96
|
+
$indexOfArray: [
|
|
97
|
+
`$${getTempName(path)}._id`,
|
|
98
|
+
{
|
|
99
|
+
$arrayElemAt: [
|
|
100
|
+
`$${getTempName(path.slice(0, -1))}.${refName}`,
|
|
101
|
+
{
|
|
102
|
+
$indexOfArray: [
|
|
103
|
+
`$${getTempName(path.slice(0, -1))}._id`,
|
|
104
|
+
parentElem
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
};
|
|
111
|
+
} else {
|
|
112
|
+
indexOfArray = {
|
|
113
|
+
$indexOfArray: [
|
|
114
|
+
`$${getTempName(path.slice(0, -1))}._id`,
|
|
115
|
+
parentElem
|
|
116
|
+
]
|
|
117
|
+
};
|
|
118
|
+
}
|
|
110
119
|
} else {
|
|
111
120
|
indexOfArray = {
|
|
112
121
|
$indexOfArray: [
|
|
@@ -181,16 +190,30 @@ export const recurseSetStage = (reference, path, parentElem, options = {
|
|
|
181
190
|
for (const [subRefName, subReference] of Object.entries(reference.deepReferences)) {
|
|
182
191
|
let newElem;
|
|
183
192
|
if (shouldUseArrayIndex) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
193
|
+
if (isRef) {
|
|
194
|
+
newElem = {
|
|
195
|
+
$arrayElemAt: [
|
|
196
|
+
`$${getTempName(path.slice(0, -1))}.${refName}`,
|
|
197
|
+
indexOfArray
|
|
198
|
+
]
|
|
199
|
+
};
|
|
200
|
+
} else {
|
|
201
|
+
newElem = {
|
|
202
|
+
$arrayElemAt: [
|
|
203
|
+
`$${getTempName(path.slice(0, -1))}.${refName}.${subRefName}`,
|
|
204
|
+
indexOfArray
|
|
205
|
+
]
|
|
206
|
+
};
|
|
207
|
+
}
|
|
190
208
|
} else {
|
|
191
209
|
newElem = reference.referencedCollection ? parentElem : `${parentElem}.${subRefName}`;
|
|
192
210
|
}
|
|
193
|
-
const result = recurseSetStage(subReference, path.concat(
|
|
211
|
+
const result = recurseSetStage(subReference, path.concat([
|
|
212
|
+
[
|
|
213
|
+
subRefName,
|
|
214
|
+
"referencedCollection" in subReference
|
|
215
|
+
]
|
|
216
|
+
]), newElem);
|
|
194
217
|
stages.push([
|
|
195
218
|
subRefName,
|
|
196
219
|
result
|
|
@@ -266,20 +289,26 @@ export const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
266
289
|
continue;
|
|
267
290
|
}
|
|
268
291
|
}
|
|
292
|
+
const newPath = path.concat([
|
|
293
|
+
[
|
|
294
|
+
refName,
|
|
295
|
+
"referencedCollection" in reference
|
|
296
|
+
]
|
|
297
|
+
]);
|
|
269
298
|
if (reference.deepReferences) {
|
|
270
299
|
buildLookupPipeline(reference.deepReferences, {
|
|
271
300
|
rootPipeline,
|
|
272
301
|
tempNames,
|
|
273
|
-
path:
|
|
302
|
+
path: newPath
|
|
274
303
|
});
|
|
275
|
-
const result = recurseSetStage(reference,
|
|
304
|
+
const result = recurseSetStage(reference, newPath, `$${refName}`);
|
|
276
305
|
setProperties.push([
|
|
277
306
|
refName,
|
|
278
307
|
result
|
|
279
308
|
]);
|
|
280
309
|
}
|
|
281
310
|
if (reference.referencedCollection) {
|
|
282
|
-
const tempName = getTempName(
|
|
311
|
+
const tempName = getTempName(newPath);
|
|
283
312
|
const lookupPipeline = [];
|
|
284
313
|
tempNames.unshift(tempName);
|
|
285
314
|
if (reference.populate && reference.populate.length > 0) {
|
|
@@ -290,7 +319,13 @@ export const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
290
319
|
]))
|
|
291
320
|
});
|
|
292
321
|
}
|
|
293
|
-
|
|
322
|
+
let localField;
|
|
323
|
+
if (reference.isRecursive) {
|
|
324
|
+
localField = `${getTempName(path)}.${refName}`;
|
|
325
|
+
} else {
|
|
326
|
+
const localFieldPath = newPath.map(([segment]) => segment).join(".");
|
|
327
|
+
localField = path[0] && path[0][1] ? `_${localFieldPath}` : localFieldPath;
|
|
328
|
+
}
|
|
294
329
|
rootPipeline.unshift({
|
|
295
330
|
$lookup: {
|
|
296
331
|
from: reference.referencedCollection,
|
|
@@ -301,7 +336,7 @@ export const buildLookupPipeline = (refMap, options = {}) => {
|
|
|
301
336
|
}
|
|
302
337
|
});
|
|
303
338
|
if (!reference.deepReferences) {
|
|
304
|
-
const result = recurseSetStage(reference,
|
|
339
|
+
const result = recurseSetStage(reference, newPath, `$${refName}`);
|
|
305
340
|
setProperties.push([
|
|
306
341
|
refName,
|
|
307
342
|
result
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.203",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"aeriaMain": "tests/fixtures/aeriaMain.js",
|
|
@@ -42,13 +42,13 @@
|
|
|
42
42
|
"mongodb-memory-server": "^9.2.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@aeriajs/builtins": "^0.0.
|
|
46
|
-
"@aeriajs/common": "^0.0.
|
|
47
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
48
|
-
"@aeriajs/http": "^0.0.
|
|
49
|
-
"@aeriajs/security": "^0.0.
|
|
50
|
-
"@aeriajs/types": "^0.0.
|
|
51
|
-
"@aeriajs/validation": "^0.0.
|
|
45
|
+
"@aeriajs/builtins": "^0.0.203",
|
|
46
|
+
"@aeriajs/common": "^0.0.124",
|
|
47
|
+
"@aeriajs/entrypoint": "^0.0.127",
|
|
48
|
+
"@aeriajs/http": "^0.0.143",
|
|
49
|
+
"@aeriajs/security": "^0.0.203",
|
|
50
|
+
"@aeriajs/types": "^0.0.106",
|
|
51
|
+
"@aeriajs/validation": "^0.0.132"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"mongodb": "^6.5.0",
|