diff --git a/server/dist/definitions/modepress-api.d.ts b/server/dist/definitions/modepress-api.d.ts index b180814a..50d012e6 100644 --- a/server/dist/definitions/modepress-api.d.ts +++ b/server/dist/definitions/modepress-api.d.ts @@ -26,6 +26,14 @@ declare module Modepress * If true, then all data is returned and is not stripped of sensitive items */ verbose : boolean + + /** + * Defines how many levels deep foreign key traversal iterates. If 1, then only the immediate foreign keys + * are fetched. For example Model X references model Y, which in turn references another model X. When expandMaxDepth=1 + * only model X and its model Y instance are returned (Model Y's reference to any X is ignored) + * Only read if expandForeignKeys is true. + */ + expandMaxDepth? : number; } /* diff --git a/server/dist/src/models/schema-items/schema-foreign-key.js b/server/dist/src/models/schema-items/schema-foreign-key.js index 0a86d9a8..cacabbf9 100644 --- a/server/dist/src/models/schema-items/schema-foreign-key.js +++ b/server/dist/src/models/schema-items/schema-foreign-key.js @@ -29,6 +29,7 @@ class SchemaForeignKey extends schema_item_1.SchemaItem { super(name, val); this.targetCollection = targetCollection; this.optionalKey = optionalKey; + this.curLevel = 1; } /** * Creates a clone of this item @@ -122,7 +123,20 @@ class SchemaForeignKey extends schema_item_1.SchemaItem { if (model) { if (!this.value) return null; + // Make sure the current level is not beyond the max depth + if (options.expandMaxDepth !== undefined) { + if (this.curLevel > options.expandMaxDepth) + return this.value; + } + else + options.expandMaxDepth = 1; var result = yield model.findOne({ _id: this.value }); + // Get the models items are increase their level - this ensures we dont go too deep + var items = result.schema.getItems(); + var nextLevel = this.curLevel + 1; + for (var i = 0, l = items.length; i < l; i++) + if (items[i] instanceof SchemaForeignKey) + items[i].curLevel = nextLevel; return yield result.schema.getAsJson(result.dbEntry._id, options); } else diff --git a/server/src/definitions/custom/modepress-api.d.ts b/server/src/definitions/custom/modepress-api.d.ts index 6cca5278..f8ee1840 100644 --- a/server/src/definitions/custom/modepress-api.d.ts +++ b/server/src/definitions/custom/modepress-api.d.ts @@ -26,6 +26,14 @@ * If true, then all data is returned and is not stripped of sensitive items */ verbose : boolean + + /** + * Defines how many levels deep foreign key traversal iterates. If 1, then only the immediate foreign keys + * are fetched. For example Model X references model Y, which in turn references another model X. When expandMaxDepth=1 + * only model X and its model Y instance are returned (Model Y's reference to any X is ignored) + * Only read if expandForeignKeys is true. + */ + expandMaxDepth? : number; } /* diff --git a/server/src/models/schema-items/schema-foreign-key.ts b/server/src/models/schema-items/schema-foreign-key.ts index 23b46b31..2f180673 100644 --- a/server/src/models/schema-items/schema-foreign-key.ts +++ b/server/src/models/schema-items/schema-foreign-key.ts @@ -14,6 +14,7 @@ export class SchemaForeignKey extends SchemaItem; @@ -29,6 +30,7 @@ export class SchemaForeignKey extends SchemaItem options.expandMaxDepth ) + return this.value; + } + else + options.expandMaxDepth = 1; + var result = await model.findOne( { _id : this.value } ); - return await result.schema.getAsJson( result.dbEntry._id, options); + + // Get the models items are increase their level - this ensures we dont go too deep + var items = result.schema.getItems(); + var nextLevel = this.curLevel + 1; + + for (var i = 0, l = items.length; i < l; i++) + if ( items[i] instanceof SchemaForeignKey ) + (items[i]).curLevel = nextLevel; + + return await result.schema.getAsJson( result.dbEntry._id, options ); } else throw new Error(`${this.name} references a foreign key '${this.targetCollection}' which doesn't seem to exist`);