@aeriajs/core 0.0.297 → 0.0.298

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.
@@ -7,6 +7,7 @@ export type Reference = {
7
7
  isRecursive?: boolean;
8
8
  deepReferences?: ReferenceMap;
9
9
  referencedCollection?: string;
10
+ foreignField?: string;
10
11
  indexes?: string[];
11
12
  populate?: string[];
12
13
  };
@@ -40,7 +40,9 @@ export const getReferences = async (properties, options = {}) => {
40
40
  }
41
41
  const { indexes = description.indexes || [] } = refProperty;
42
42
  if (Array.isArray(refProperty.populate) || !refProperty.populate) {
43
- reference.populate = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
43
+ reference.populate = (refProperty.populate || [])
44
+ .concat(indexes.filter((index) => typeof index === 'string'))
45
+ .concat([refProperty.foreignField]);
44
46
  }
45
47
  }
46
48
  else {
@@ -77,11 +79,13 @@ export const getReferences = async (properties, options = {}) => {
77
79
  if (refProperty) {
78
80
  if (refProperty.$ref) {
79
81
  reference.referencedCollection = refProperty.$ref;
82
+ reference.foreignField = refProperty.foreignField;
80
83
  }
81
84
  if (refProperty.inline) {
82
85
  reference.isInline = true;
83
86
  }
84
87
  }
88
+ reference.foreignField ||= '_id';
85
89
  refMap[propName] = reference;
86
90
  }
87
91
  if (memoize) {
@@ -99,13 +103,13 @@ export const recurseSetStage = (reference, path, parentElem, options = {
99
103
  if (isRef) {
100
104
  indexOfArray = {
101
105
  $indexOfArray: [
102
- `$${getTempName(path)}._id`,
106
+ `$${getTempName(path)}.${reference.foreignField}`,
103
107
  {
104
108
  $arrayElemAt: [
105
109
  `$${getTempName(path.slice(0, -1))}.${refName}`,
106
110
  {
107
111
  $indexOfArray: [
108
- `$${getTempName(path.slice(0, -1))}._id`,
112
+ `$${getTempName(path.slice(0, -1))}.${reference.foreignField}`,
109
113
  parentElem,
110
114
  ],
111
115
  },
@@ -117,7 +121,7 @@ export const recurseSetStage = (reference, path, parentElem, options = {
117
121
  else {
118
122
  indexOfArray = {
119
123
  $indexOfArray: [
120
- `$${getTempName(path.slice(0, -1))}._id`,
124
+ `$${getTempName(path.slice(0, -1))}.${reference.foreignField}`,
121
125
  parentElem,
122
126
  ],
123
127
  };
@@ -126,7 +130,7 @@ export const recurseSetStage = (reference, path, parentElem, options = {
126
130
  else {
127
131
  indexOfArray = {
128
132
  $indexOfArray: [
129
- `$${getTempName(path)}._id`,
133
+ `$${getTempName(path)}.${reference.foreignField}`,
130
134
  parentElem,
131
135
  ],
132
136
  };
@@ -159,7 +163,7 @@ export const recurseSetStage = (reference, path, parentElem, options = {
159
163
  `$${getTempName(path.slice(0, -1))}.${refName}`,
160
164
  {
161
165
  $indexOfArray: [
162
- `$${getTempName(path.slice(0, -1))}._id`,
166
+ `$${getTempName(path.slice(0, -1))}.${reference.foreignField}`,
163
167
  parentElem,
164
168
  ],
165
169
  },
@@ -205,7 +209,7 @@ export const recurseSetStage = (reference, path, parentElem, options = {
205
209
  `$${getTempName(path.slice(0, -1))}.${refName}`,
206
210
  {
207
211
  $indexOfArray: [
208
- `$${getTempName(path.slice(0, -1))}._id`,
212
+ `$${getTempName(path.slice(0, -1))}.${reference.foreignField}`,
209
213
  parentElem,
210
214
  ],
211
215
  },
@@ -349,7 +353,7 @@ export const buildLookupPipeline = (refMap, options = {}) => {
349
353
  rootPipeline.unshift({
350
354
  $lookup: {
351
355
  from: reference.referencedCollection,
352
- foreignField: '_id',
356
+ foreignField: reference.foreignField,
353
357
  localField,
354
358
  as: tempName,
355
359
  pipeline: lookupPipeline,
@@ -376,10 +380,14 @@ export const buildLookupPipeline = (refMap, options = {}) => {
376
380
  if (memoize) {
377
381
  lookupMemo[memoize] = finalPipeline;
378
382
  }
383
+ console.log(inspect(finalPipeline, {
384
+ depth: null,
385
+ }));
379
386
  return finalPipeline;
380
387
  }
381
388
  return pipeline;
382
389
  };
390
+ import { inspect } from 'util';
383
391
  export const getLookupPipeline = async (description, options) => {
384
392
  const refMap = await getReferences(description.properties);
385
393
  return buildLookupPipeline(refMap, options);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
3
  "type": "module",
4
- "version": "0.0.297",
4
+ "version": "0.0.298",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "aeriaMain": "tests/fixtures/aeriaMain.js",
@@ -40,13 +40,13 @@
40
40
  "mongodb-memory-server": "^11.0.1"
41
41
  },
42
42
  "peerDependencies": {
43
- "@aeriajs/builtins": "^0.0.297",
44
- "@aeriajs/common": "^0.0.169",
45
- "@aeriajs/entrypoint": "^0.0.177",
46
- "@aeriajs/http": "^0.0.209",
47
- "@aeriajs/security": "^0.0.297",
48
- "@aeriajs/types": "^0.0.141",
49
- "@aeriajs/validation": "^0.0.194"
43
+ "@aeriajs/builtins": "^0.0.298",
44
+ "@aeriajs/common": "^0.0.170",
45
+ "@aeriajs/entrypoint": "^0.0.178",
46
+ "@aeriajs/http": "^0.0.210",
47
+ "@aeriajs/security": "^0.0.298",
48
+ "@aeriajs/types": "^0.0.142",
49
+ "@aeriajs/validation": "^0.0.195"
50
50
  },
51
51
  "dependencies": {
52
52
  "mongodb": "^7.1.0",