@@ -416,6 +416,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
416
416
options = { } ;
417
417
}
418
418
options = options || { } ;
419
+ cb = cb || utils . createPromiseCallback ( ) ;
419
420
const targetModel = definition . targetModel ( this . _receiver ) ;
420
421
// If there is a through model
421
422
// run another query to apply filter on relatedModel(targetModel)
@@ -446,15 +447,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
446
447
const scoped = ( this . _scope && this . _scope . where ) || { } ;
447
448
const filter = mergeQuery ( { where : scoped } , { where : where || { } , fields : fieldsRelated } ) ;
448
449
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
+ } ) ;
450
460
}
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 ;
458
462
}
459
463
460
464
function updateAll ( where , data , options , cb ) {
@@ -471,6 +475,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
471
475
options = { } ;
472
476
}
473
477
options = options || { } ;
478
+ cb = cb || utils . createPromiseCallback ( ) ;
474
479
const targetModel = definition . targetModel ( this . _receiver ) ;
475
480
// If there is a through model
476
481
// run another query to apply filter on relatedModel(targetModel)
@@ -501,15 +506,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
501
506
const scoped = ( this . _scope && this . _scope . where ) || { } ;
502
507
const filter = mergeQuery ( { where : scoped } , { where : where || { } , fields : fieldsRelated } ) ;
503
508
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
+ } ) ;
505
519
}
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 ;
513
521
}
514
522
515
523
function findById ( id , filter , options , cb ) {
@@ -554,6 +562,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
554
562
options = { } ;
555
563
}
556
564
options = options || { } ;
565
+ cb = cb || utils . createPromiseCallback ( ) ;
557
566
const targetModel = definition . targetModel ( this . _receiver ) ;
558
567
// If there is a through model
559
568
// run another query to apply filter on relatedModel(targetModel)
@@ -586,15 +595,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
586
595
const scoped = ( this . _scope && this . _scope . where ) || { } ;
587
596
filter = mergeQuery ( { where : scoped } , filter || { } ) ;
588
597
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
+ } ) ;
590
608
}
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 ;
598
610
}
599
611
600
612
function count ( where , options , cb ) {
@@ -608,6 +620,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
608
620
options = { } ;
609
621
}
610
622
options = options || { } ;
623
+ cb = cb || utils . createPromiseCallback ( ) ;
611
624
const targetModel = definition . targetModel ( this . _receiver ) ;
612
625
// If there is a through model
613
626
// run another query to apply filter on relatedModel(targetModel)
@@ -638,15 +651,18 @@ function defineScope(cls, targetClass, name, params, methods, options) {
638
651
const scoped = ( this . _scope && this . _scope . where ) || { } ;
639
652
const filter = mergeQuery ( { where : scoped } , { where : where || { } , fields : fieldsRelated } ) ;
640
653
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
+ } ) ;
642
664
}
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 ;
650
666
}
651
667
652
668
return definition ;
0 commit comments