Skip to content

Commit 953ea4c

Browse files
committed
Fix bug #1795 Count issues related models
1 parent 54e9811 commit 953ea4c

File tree

2 files changed

+386
-810
lines changed

2 files changed

+386
-810
lines changed

lib/scope.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
416416
options = {};
417417
}
418418
options = options || {};
419+
cb = cb || utils.createPromiseCallback();
419420
const targetModel = definition.targetModel(this._receiver);
420421
// If there is a through model
421422
// run another query to apply filter on relatedModel(targetModel)
@@ -446,15 +447,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
446447
const scoped = (this._scope && this._scope.where) || {};
447448
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
448449
if (!scopeOnRelatedModel) {
449-
return targetModel.destroyAll(filter.where, options, cb);
450+
targetModel.destroyAll(filter.where, options, cb);
451+
} else {
452+
targetModel.find(filter, options, function(err, findData) {
453+
const smartMergeSuccessful =
454+
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
455+
if (!smartMergeSuccessful) {
456+
return cb(null, {count: 0});
457+
}
458+
return relatedModel.destroyAll(queryRelated.scope.where, options, cb);
459+
});
450460
}
451-
return targetModel.find(filter, options, function(err, findData) {
452-
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
453-
if (!smartMergeSuccessful) {
454-
return cb(null, {count: 0});
455-
}
456-
return relatedModel.destroyAll(queryRelated.scope.where, options, cb);
457-
});
461+
return cb.promise;
458462
}
459463

460464
function updateAll(where, data, options, cb) {
@@ -471,6 +475,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
471475
options = {};
472476
}
473477
options = options || {};
478+
cb = cb || utils.createPromiseCallback();
474479
const targetModel = definition.targetModel(this._receiver);
475480
// If there is a through model
476481
// run another query to apply filter on relatedModel(targetModel)
@@ -501,15 +506,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
501506
const scoped = (this._scope && this._scope.where) || {};
502507
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
503508
if (!scopeOnRelatedModel) {
504-
return targetModel.updateAll(filter.where, data, options, cb);
509+
targetModel.updateAll(filter.where, data, options, cb);
510+
} else {
511+
targetModel.find(filter, options, function(err, findData) {
512+
const smartMergeSuccessful =
513+
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
514+
if (!smartMergeSuccessful) {
515+
return cb(null, {count: 0});
516+
}
517+
return relatedModel.updateAll(queryRelated.scope.where, data, options, cb);
518+
});
505519
}
506-
return targetModel.find(filter, options, function(err, findData) {
507-
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
508-
if (!smartMergeSuccessful) {
509-
return cb(null, {count: 0});
510-
}
511-
return relatedModel.updateAll(queryRelated.scope.where, data, options, cb);
512-
});
520+
return cb.promise;
513521
}
514522

515523
function findById(id, filter, options, cb) {
@@ -554,6 +562,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
554562
options = {};
555563
}
556564
options = options || {};
565+
cb = cb || utils.createPromiseCallback();
557566
const targetModel = definition.targetModel(this._receiver);
558567
// If there is a through model
559568
// run another query to apply filter on relatedModel(targetModel)
@@ -586,15 +595,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
586595
const scoped = (this._scope && this._scope.where) || {};
587596
filter = mergeQuery({where: scoped}, filter || {});
588597
if (!scopeOnRelatedModel) {
589-
return targetModel.findOne(filter, options, cb);
598+
targetModel.findOne(filter, options, cb);
599+
} else {
600+
targetModel.find(filter, options, function(err, findData) {
601+
const smartMergeSuccessful =
602+
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
603+
if (!smartMergeSuccessful) {
604+
return cb(null, null);
605+
}
606+
return relatedModel.findOne(queryRelated.scope, options, cb);
607+
});
590608
}
591-
return targetModel.find(filter, options, function(err, findData) {
592-
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
593-
if (!smartMergeSuccessful) {
594-
return cb(null, null);
595-
}
596-
return relatedModel.findOne(queryRelated.scope, options, cb);
597-
});
609+
return cb.promise;
598610
}
599611

600612
function count(where, options, cb) {
@@ -608,6 +620,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
608620
options = {};
609621
}
610622
options = options || {};
623+
cb = cb || utils.createPromiseCallback();
611624
const targetModel = definition.targetModel(this._receiver);
612625
// If there is a through model
613626
// run another query to apply filter on relatedModel(targetModel)
@@ -638,15 +651,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
638651
const scoped = (this._scope && this._scope.where) || {};
639652
const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated});
640653
if (!scopeOnRelatedModel) {
641-
return targetModel.count(filter.where, options, cb);
654+
targetModel.count(filter.where, options, cb);
655+
} else {
656+
targetModel.find(filter, options, function(err, findData) {
657+
const smartMergeSuccessful =
658+
smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
659+
if (!smartMergeSuccessful) {
660+
return cb(null, 0);
661+
}
662+
return relatedModel.count(queryRelated.scope.where, options, cb);
663+
});
642664
}
643-
return targetModel.find(filter, options, function(err, findData) {
644-
const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey);
645-
if (!smartMergeSuccessful) {
646-
return cb(null, 0);
647-
}
648-
return relatedModel.count(queryRelated.scope.where, options, cb);
649-
});
665+
return cb.promise;
650666
}
651667

652668
return definition;

0 commit comments

Comments
 (0)