@aeriajs/core 0.0.138 → 0.0.139

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.
@@ -2,6 +2,7 @@ import type { FixedObjectProperty, Description } from '@aeriajs/types';
2
2
  export type GetReferenceOptions = {
3
3
  memoize?: string;
4
4
  depth?: number;
5
+ maxDepth?: number;
5
6
  };
6
7
  export type Reference = {
7
8
  isArray?: boolean;
@@ -67,7 +67,7 @@ const buildArrayCleanupStages = (referenceMap) => {
67
67
  : null;
68
68
  };
69
69
  const getReferences = async (properties, options) => {
70
- const { depth = 0, memoize, } = options || {};
70
+ const { depth = 0, maxDepth = 3, memoize, } = options || {};
71
71
  if (memoize) {
72
72
  if (referenceMemo[memoize]) {
73
73
  return referenceMemo[memoize];
@@ -77,10 +77,25 @@ const getReferences = async (properties, options) => {
77
77
  for (const [propName, property] of Object.entries(properties)) {
78
78
  const refProperty = (0, common_1.getReferenceProperty)(property);
79
79
  const reference = {};
80
- if (depth === 2 || (refProperty && refProperty.populate && refProperty.populate.length === 0)) {
80
+ if (depth === maxDepth || (refProperty && refProperty.populate && refProperty.populate.length === 0)) {
81
81
  continue;
82
82
  }
83
- if (!refProperty) {
83
+ if (refProperty) {
84
+ const description = (0, common_1.throwIfError)(await (0, assets_js_1.getCollectionAsset)(refProperty.$ref, 'description'));
85
+ const deepReferences = await (0, exports.getReferences)(description.properties, {
86
+ depth: depth + 1,
87
+ maxDepth: refProperty.populateDepth || maxDepth,
88
+ memoize: `${memoize}.${propName}`,
89
+ });
90
+ if (Object.keys(deepReferences).length > 0) {
91
+ reference.deepReferences = deepReferences;
92
+ }
93
+ const indexes = refProperty.indexes
94
+ ? refProperty.indexes
95
+ : description.indexes || [];
96
+ reference.populatedProperties = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
97
+ }
98
+ else {
84
99
  const entrypoint = 'items' in property
85
100
  ? property.items
86
101
  : property;
@@ -99,20 +114,6 @@ const getReferences = async (properties, options) => {
99
114
  }
100
115
  }
101
116
  }
102
- else {
103
- const description = (0, common_1.throwIfError)(await (0, assets_js_1.getCollectionAsset)(refProperty.$ref, 'description'));
104
- const deepReferences = await (0, exports.getReferences)(description.properties, {
105
- depth: depth + 1,
106
- memoize: `${memoize}.${propName}`,
107
- });
108
- if (Object.keys(deepReferences).length > 0) {
109
- reference.deepReferences = deepReferences;
110
- }
111
- const indexes = refProperty.indexes
112
- ? refProperty.indexes
113
- : description.indexes || [];
114
- reference.populatedProperties = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
115
- }
116
117
  if (!refProperty?.$ref && !reference.deepReferences) {
117
118
  continue;
118
119
  }
@@ -215,12 +216,14 @@ const buildLookupStages = async (reference, propName, options) => {
215
216
  }
216
217
  else if (reference.deepReferences && depth <= maxDepth) {
217
218
  refHasDeepReferences = true;
218
- stages.push({
219
- $unwind: {
220
- path: `$${withParent(propName)}`,
221
- preserveNullAndEmptyArrays: true,
222
- },
223
- });
219
+ if (reference.isArray) {
220
+ stages.push({
221
+ $unwind: {
222
+ path: `$${withParent(propName)}`,
223
+ preserveNullAndEmptyArrays: true,
224
+ },
225
+ });
226
+ }
224
227
  for (const [refName, refMap] of Object.entries(reference.deepReferences)) {
225
228
  if (!refMap) {
226
229
  continue;
@@ -63,6 +63,7 @@ const buildArrayCleanupStages = (referenceMap) => {
63
63
  export const getReferences = async (properties, options) => {
64
64
  const {
65
65
  depth = 0,
66
+ maxDepth = 3,
66
67
  memoize
67
68
  } = options || {};
68
69
  if (memoize) {
@@ -74,24 +75,14 @@ export const getReferences = async (properties, options) => {
74
75
  for (const [propName, property] of Object.entries(properties)) {
75
76
  const refProperty = getReferenceProperty(property);
76
77
  const reference = {};
77
- if (depth === 2 || refProperty && refProperty.populate && refProperty.populate.length === 0) {
78
+ if (depth === maxDepth || refProperty && refProperty.populate && refProperty.populate.length === 0) {
78
79
  continue;
79
80
  }
80
- if (!refProperty) {
81
- const entrypoint = "items" in property ? property.items : property;
82
- if ("properties" in entrypoint) {
83
- const deepReferences = await getReferences(entrypoint.properties, {
84
- memoize: `${memoize}.${propName}`
85
- });
86
- if (Object.keys(deepReferences).length > 0) {
87
- reference.deepReferences ??= {};
88
- reference.deepReferences = deepReferences;
89
- }
90
- }
91
- } else {
81
+ if (refProperty) {
92
82
  const description = throwIfError(await getCollectionAsset(refProperty.$ref, "description"));
93
83
  const deepReferences = await getReferences(description.properties, {
94
84
  depth: depth + 1,
85
+ maxDepth: refProperty.populateDepth || maxDepth,
95
86
  memoize: `${memoize}.${propName}`
96
87
  });
97
88
  if (Object.keys(deepReferences).length > 0) {
@@ -99,6 +90,17 @@ export const getReferences = async (properties, options) => {
99
90
  }
100
91
  const indexes = refProperty.indexes ? refProperty.indexes : description.indexes || [];
101
92
  reference.populatedProperties = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === "string"));
93
+ } else {
94
+ const entrypoint = "items" in property ? property.items : property;
95
+ if ("properties" in entrypoint) {
96
+ const deepReferences = await getReferences(entrypoint.properties, {
97
+ memoize: `${memoize}.${propName}`
98
+ });
99
+ if (Object.keys(deepReferences).length > 0) {
100
+ reference.deepReferences ??= {};
101
+ reference.deepReferences = deepReferences;
102
+ }
103
+ }
102
104
  }
103
105
  if (!refProperty?.$ref && !reference.deepReferences) {
104
106
  continue;
@@ -198,12 +200,14 @@ const buildLookupStages = async (reference, propName, options) => {
198
200
  }
199
201
  } else if (reference.deepReferences && depth <= maxDepth) {
200
202
  refHasDeepReferences = true;
201
- stages.push({
202
- $unwind: {
203
- path: `$${withParent(propName)}`,
204
- preserveNullAndEmptyArrays: true
205
- }
206
- });
203
+ if (reference.isArray) {
204
+ stages.push({
205
+ $unwind: {
206
+ path: `$${withParent(propName)}`,
207
+ preserveNullAndEmptyArrays: true
208
+ }
209
+ });
210
+ }
207
211
  for (const [refName, refMap] of Object.entries(reference.deepReferences)) {
208
212
  if (!refMap) {
209
213
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.138",
3
+ "version": "0.0.139",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -41,13 +41,13 @@
41
41
  "mongodb-memory-server": "^9.2.0"
42
42
  },
43
43
  "peerDependencies": {
44
- "@aeriajs/builtins": "^0.0.138",
45
- "@aeriajs/common": "^0.0.87",
46
- "@aeriajs/entrypoint": "^0.0.89",
47
- "@aeriajs/http": "^0.0.98",
48
- "@aeriajs/security": "^0.0.138",
49
- "@aeriajs/types": "^0.0.75",
50
- "@aeriajs/validation": "^0.0.90"
44
+ "@aeriajs/builtins": "^0.0.139",
45
+ "@aeriajs/common": "^0.0.88",
46
+ "@aeriajs/entrypoint": "^0.0.90",
47
+ "@aeriajs/http": "^0.0.99",
48
+ "@aeriajs/security": "^0.0.139",
49
+ "@aeriajs/types": "^0.0.76",
50
+ "@aeriajs/validation": "^0.0.91"
51
51
  },
52
52
  "dependencies": {
53
53
  "mongodb": "^6.5.0",