Skip to content

Commit

Permalink
Implemented foreign key depth traversal feature. Allows you to set a …
Browse files Browse the repository at this point in the history
…limit to how deep foreign key instance retrieval goes
  • Loading branch information
MKHenson committed May 23, 2016
1 parent 93ff59c commit 6128276
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions server/dist/definitions/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down
14 changes: 14 additions & 0 deletions server/dist/src/models/schema-items/schema-foreign-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions server/src/definitions/custom/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down
22 changes: 21 additions & 1 deletion server/src/models/schema-items/schema-foreign-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
{
public targetCollection : string;
public optionalKey : boolean;
public curLevel: number;

private _targetDoc : ModelInstance<Modepress.IModelEntry>;

Expand All @@ -29,6 +30,7 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
super(name, val);
this.targetCollection = targetCollection;
this.optionalKey = optionalKey;
this.curLevel = 1;
}

/**
Expand Down Expand Up @@ -147,8 +149,26 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
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 = await model.findOne<Modepress.IModelEntry>( { _id : <ObjectID>this.value } );
return await result.schema.getAsJson<Modepress.IModelEntry>( 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 )
(<SchemaForeignKey>items[i]).curLevel = nextLevel;

return await result.schema.getAsJson<Modepress.IModelEntry>( result.dbEntry._id, options );
}
else
throw new Error(`${this.name} references a foreign key '${this.targetCollection}' which doesn't seem to exist`);
Expand Down

0 comments on commit 6128276

Please sign in to comment.