@aeriajs/core 0.0.148 → 0.0.149

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
- export type GetReferenceOptions = {
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 PipelineStage = {};
14
+ export type GetReferenceOptions = {
15
+ memoize?: string;
16
+ depth?: number;
17
+ maxDepth?: number;
18
+ populate?: string[];
19
+ };
18
20
  export type BuildLookupPipelineOptions = {
19
- rootPipeline?: PipelineStage[];
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
- }) => PipelineStage;
29
- export declare const buildLookupPipeline: (refMap: ReferenceMap, options?: BuildLookupPipelineOptions) => PipelineStage[];
30
- export declare const getLookupPipeline: (description: Description, options: BuildLookupPipelineOptions) => Promise<PipelineStage[]>;
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,28 @@ 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
- if (depth === maxDepth || (refProperty && refProperty.populate && refProperty.populate.length === 0)) {
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
- const deepReferences = await (0, exports.getReferences)(description.properties, {
29
- depth: depth + 1,
30
- maxDepth: refProperty.populateDepth || maxDepth,
31
- memoize: `${memoize}.${propName}`,
32
- });
33
- if (Object.keys(deepReferences).length > 0) {
34
- reference.deepReferences = deepReferences;
28
+ if (refProperty.populate) {
29
+ const deepReferences = await (0, exports.getReferences)(description.properties, {
30
+ depth: depth + 1,
31
+ maxDepth: refProperty.populateDepth || maxDepth,
32
+ memoize: `${memoize}.${propName}`,
33
+ populate: Array.from(refProperty.populate),
34
+ });
35
+ if (Object.keys(deepReferences).length > 0) {
36
+ reference.deepReferences = deepReferences;
37
+ }
35
38
  }
36
39
  const indexes = refProperty.indexes
37
40
  ? refProperty.indexes
38
- : description.indexes || Object.keys(description.properties).slice(0, 1);
39
- reference.indexes = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
41
+ : description.indexes || [];
42
+ reference.populate = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === 'string'));
40
43
  }
41
44
  else {
42
45
  const entrypoint = 'items' in property
@@ -269,13 +272,9 @@ const buildLookupPipeline = (refMap, options = {}) => {
269
272
  const tempName = getTempName(path.concat(refName));
270
273
  const lookupPipeline = [];
271
274
  tempNames.unshift(tempName);
272
- if (reference.indexes && reference.indexes.length > 0) {
273
- const lookupPopulate = reference.indexes;
274
- if (reference.deepReferences) {
275
- lookupPopulate.push(...Object.keys(reference.deepReferences));
276
- }
275
+ if (reference.populate && reference.populate.length > 0) {
277
276
  lookupPipeline.push({
278
- $project: Object.fromEntries(reference.indexes.map((index) => [
277
+ $project: Object.fromEntries(reference.populate.map((index) => [
279
278
  index,
280
279
  1,
281
280
  ])),
@@ -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,26 @@ 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
- if (depth === maxDepth || refProperty && refProperty.populate && refProperty.populate.length === 0) {
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
- const deepReferences = await getReferences(description.properties, {
31
- depth: depth + 1,
32
- maxDepth: refProperty.populateDepth || maxDepth,
33
- memoize: `${memoize}.${propName}`
34
- });
35
- if (Object.keys(deepReferences).length > 0) {
36
- reference.deepReferences = deepReferences;
31
+ if (refProperty.populate) {
32
+ const deepReferences = await 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
+ }
37
41
  }
38
- const indexes = refProperty.indexes ? refProperty.indexes : description.indexes || Object.keys(description.properties).slice(0, 1);
39
- reference.indexes = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === "string"));
42
+ const indexes = refProperty.indexes ? refProperty.indexes : description.indexes || [];
43
+ reference.populate = (refProperty.populate || []).concat(indexes.filter((index) => typeof index === "string"));
40
44
  } else {
41
45
  const entrypoint = "items" in property ? property.items : property;
42
46
  if ("properties" in entrypoint) {
@@ -258,13 +262,9 @@ export const buildLookupPipeline = (refMap, options = {}) => {
258
262
  const tempName = getTempName(path.concat(refName));
259
263
  const lookupPipeline = [];
260
264
  tempNames.unshift(tempName);
261
- if (reference.indexes && reference.indexes.length > 0) {
262
- const lookupPopulate = reference.indexes;
263
- if (reference.deepReferences) {
264
- lookupPopulate.push(...Object.keys(reference.deepReferences));
265
- }
265
+ if (reference.populate && reference.populate.length > 0) {
266
266
  lookupPipeline.push({
267
- $project: Object.fromEntries(reference.indexes.map((index) => [
267
+ $project: Object.fromEntries(reference.populate.map((index) => [
268
268
  index,
269
269
  1
270
270
  ]))
@@ -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.148",
3
+ "version": "0.0.149",
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.148",
45
+ "@aeriajs/builtins": "^0.0.149",
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.148",
49
+ "@aeriajs/security": "^0.0.149",
50
50
  "@aeriajs/types": "^0.0.78",
51
51
  "@aeriajs/validation": "^0.0.93"
52
52
  },