Skip to content

Commit

Permalink
Attempting to reset the juggler back off of v2.45.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencerhutch committed Mar 1, 2016
1 parent 1268e0f commit 13d9c02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
24 changes: 14 additions & 10 deletions lib/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,13 @@ Inclusion.include = function (objects, include, options, cb) {
filter.where[relation.keyTo] = {
inq: uniq(allTargetIds)
};
relation.applyScope(null, filter);
/**
* Make the DB Call, fetch all target objects
*/
relation.modelTo.find(filter, options, targetFetchHandler);
relation.applyScope(null, filter, function(err) {
/**
* Make the DB Call, fetch all target objects
*/
relation.modelTo.find(filter, options, targetFetchHandler);
});

/**
* Handle the fetched target objects
* @param err
Expand Down Expand Up @@ -501,8 +503,9 @@ Inclusion.include = function (objects, include, options, cb) {
inq: uniq(objIdMap2.getKeys())
};

relation.applyScope(null, filter);
relation.modelTo.find(filter, options, targetFetchHandler);
relation.applyScope(null, filter, function(err) {
relation.modelTo.find(filter, options, targetFetchHandler);
});

function targetFetchHandler(err, targets) {
if(err) {
Expand Down Expand Up @@ -542,9 +545,10 @@ Inclusion.include = function (objects, include, options, cb) {
filter.where[relation.keyTo] = {
inq: uniq(sourceIds)
};
relation.applyScope(null, filter);
options.partitionBy = relation.keyTo;
relation.modelTo.find(filter, options, targetFetchHandler);
relation.applyScope(null, filter, function() {
options.partitionBy = relation.keyTo;
relation.modelTo.find(filter, options, targetFetchHandler);
});
/**
* Process fetched related objects
* @param err
Expand Down
19 changes: 17 additions & 2 deletions lib/relation-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ RelationDefinition.prototype.defineMethod = function(name, fn) {
* @param {Object} modelInstance
* @param {Object} filter (where, order, limit, fields, ...)
*/
RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
RelationDefinition.prototype.applyScope = function(modelInstance, filter, cb) {
filter = filter || {};
filter.where = filter.where || {};
if ((this.type !== 'belongsTo' || this.type === 'hasOne')
Expand All @@ -208,14 +208,29 @@ RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
}
}
var scope;
var waiting = false;
if (typeof this.scope === 'function') {
scope = this.scope.call(this, modelInstance, filter);
// Did they pass a callback?
if (this.scope.length === 3) {
waiting = true;
scope = this.scope.call(this, modelInstance, filter, function(scope){
mergeQuery(filter, scope);
if (cb) {
return cb();
}
});
} else {
scope = this.scope.call(this, modelInstance, filter);
}
} else {
scope = this.scope;
}
if (typeof scope === 'object') {
mergeQuery(filter, scope);
}
if (!waiting && cb) {
return cb();
}
};

/**
Expand Down

0 comments on commit 13d9c02

Please sign in to comment.