Skip to content

Commit

Permalink
Fix bug #1795 Count issues related models
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Dec 7, 2019
1 parent 54e9811 commit 953ea4c
Show file tree
Hide file tree
Showing 2 changed files with 386 additions and 810 deletions.
80 changes: 48 additions & 32 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
options = {};
}
options = options || {};
cb = cb || utils.createPromiseCallback();
const targetModel = definition.targetModel(this._receiver);
// If there is a through model
// run another query to apply filter on relatedModel(targetModel)
Expand Down Expand Up @@ -446,15 +447,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
if (!scopeOnRelatedModel) {
return targetModel.destroyAll(filter.where, options, cb);
targetModel.destroyAll(filter.where, options, cb);
} else {
targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful =
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, {count: 0});
}
return relatedModel.destroyAll(queryRelated.scope.where, options, cb);
});
}
return targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, {count: 0});
}
return relatedModel.destroyAll(queryRelated.scope.where, options, cb);
});
return cb.promise;
}

function updateAll(where, data, options, cb) {
Expand All @@ -471,6 +475,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
options = {};
}
options = options || {};
cb = cb || utils.createPromiseCallback();
const targetModel = definition.targetModel(this._receiver);
// If there is a through model
// run another query to apply filter on relatedModel(targetModel)
Expand Down Expand Up @@ -501,15 +506,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
if (!scopeOnRelatedModel) {
return targetModel.updateAll(filter.where, data, options, cb);
targetModel.updateAll(filter.where, data, options, cb);
} else {
targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful =
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, {count: 0});
}
return relatedModel.updateAll(queryRelated.scope.where, data, options, cb);
});
}
return targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, {count: 0});
}
return relatedModel.updateAll(queryRelated.scope.where, data, options, cb);
});
return cb.promise;
}

function findById(id, filter, options, cb) {
Expand Down Expand Up @@ -554,6 +562,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
options = {};
}
options = options || {};
cb = cb || utils.createPromiseCallback();
const targetModel = definition.targetModel(this._receiver);
// If there is a through model
// run another query to apply filter on relatedModel(targetModel)
Expand Down Expand Up @@ -586,15 +595,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
const scoped = (this._scope && this._scope.where) || {};
filter = mergeQuery({where: scoped}, filter || {});
if (!scopeOnRelatedModel) {
return targetModel.findOne(filter, options, cb);
targetModel.findOne(filter, options, cb);
} else {
targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful =
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, null);
}
return relatedModel.findOne(queryRelated.scope, options, cb);
});
}
return targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, null);
}
return relatedModel.findOne(queryRelated.scope, options, cb);
});
return cb.promise;
}

function count(where, options, cb) {
Expand All @@ -608,6 +620,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
options = {};
}
options = options || {};
cb = cb || utils.createPromiseCallback();
const targetModel = definition.targetModel(this._receiver);
// If there is a through model
// run another query to apply filter on relatedModel(targetModel)
Expand Down Expand Up @@ -638,15 +651,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
if (!scopeOnRelatedModel) {
return targetModel.count(filter.where, options, cb);
targetModel.count(filter.where, options, cb);
} else {
targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful =
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, 0);
}
return relatedModel.count(queryRelated.scope.where, options, cb);
});
}
return targetModel.find(filter, options, function(err, findData) {
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
if (!smartMergeSuccessful) {
return cb(null, 0);
}
return relatedModel.count(queryRelated.scope.where, options, cb);
});
return cb.promise;
}

return definition;
Expand Down
Loading

0 comments on commit 953ea4c

Please sign in to comment.