@@ -581,65 +581,79 @@ RestWrite.prototype.handleInstallation = function() {
581
581
var promise = Promise . resolve ( ) ;
582
582
583
583
var idMatch ; // Will be a match on either objectId or installationId
584
+ var objectIdMatch ;
585
+ var installationIdMatch ;
584
586
var deviceTokenMatches = [ ] ;
585
587
588
+ // Instead of issuing 3 reads, let's do it with one OR.
589
+ let orQueries = [ ] ;
586
590
if ( this . query && this . query . objectId ) {
587
- promise = promise . then ( ( ) => {
588
- return this . config . database . find ( '_Installation' , {
591
+ orQueries . push ( {
589
592
objectId : this . query . objectId
590
- } , { } ) . then ( ( results ) => {
591
- if ( ! results . length ) {
592
- throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,
593
+ } ) ;
594
+ }
595
+ if ( this . data . installationId ) {
596
+ orQueries . push ( {
597
+ 'installationId' : this . data . installationId
598
+ } ) ;
599
+ }
600
+ if ( this . data . deviceToken ) {
601
+ orQueries . push ( { 'deviceToken' : this . data . deviceToken } ) ;
602
+ }
603
+
604
+ promise = promise . then ( ( ) => {
605
+ return this . config . database . find ( '_Installation' , {
606
+ '$or' : orQueries
607
+ } , { } ) ;
608
+ } ) . then ( ( results ) => {
609
+ results . forEach ( ( result ) => {
610
+ if ( this . query && this . query . objectId && result . objectId == this . query . objectId ) {
611
+ objectIdMatch = result ;
612
+ }
613
+ if ( result . installationId == this . data . installationId ) {
614
+ installationIdMatch = result ;
615
+ }
616
+ if ( result . deviceToken == this . data . deviceToken ) {
617
+ deviceTokenMatches . push ( result ) ;
618
+ }
619
+ } ) ;
620
+
621
+ // Sanity checks when running a query
622
+ if ( this . query && this . query . objectId ) {
623
+ if ( ! objectIdMatch ) {
624
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,
593
625
'Object not found for update.' ) ;
594
- }
595
- idMatch = results [ 0 ] ;
596
- if ( this . data . installationId && idMatch . installationId &&
597
- this . data . installationId !== idMatch . installationId ) {
626
+ }
627
+ if ( this . data . installationId && objectIdMatch . installationId &&
628
+ this . data . installationId !== objectIdMatch . installationId ) {
598
629
throw new Parse . Error ( 136 ,
599
630
'installationId may not be changed in this ' +
600
631
'operation' ) ;
601
632
}
602
- if ( this . data . deviceToken && idMatch . deviceToken &&
603
- this . data . deviceToken !== idMatch . deviceToken &&
604
- ! this . data . installationId && ! idMatch . installationId ) {
633
+ if ( this . data . deviceToken && objectIdMatch . deviceToken &&
634
+ this . data . deviceToken !== objectIdMatch . deviceToken &&
635
+ ! this . data . installationId && ! objectIdMatch . installationId ) {
605
636
throw new Parse . Error ( 136 ,
606
637
'deviceToken may not be changed in this ' +
607
638
'operation' ) ;
608
639
}
609
640
if ( this . data . deviceType && this . data . deviceType &&
610
- this . data . deviceType !== idMatch . deviceType ) {
641
+ this . data . deviceType !== objectIdMatch . deviceType ) {
611
642
throw new Parse . Error ( 136 ,
612
643
'deviceType may not be changed in this ' +
613
644
'operation' ) ;
614
645
}
615
- return ;
616
- } ) ;
617
- } ) ;
618
- }
619
-
620
- // Check if we already have installations for the installationId/deviceToken
621
- promise = promise . then ( ( ) => {
622
- if ( this . data . installationId ) {
623
- return this . config . database . find ( '_Installation' , {
624
- 'installationId' : this . data . installationId
625
- } ) ;
626
646
}
627
- return Promise . resolve ( [ ] ) ;
628
- } ) . then ( ( results ) => {
629
- if ( results && results . length ) {
630
- // We only take the first match by installationId
631
- idMatch = results [ 0 ] ;
632
- }
633
- if ( this . data . deviceToken ) {
634
- return this . config . database . find (
635
- '_Installation' ,
636
- { 'deviceToken' : this . data . deviceToken } ) ;
647
+
648
+ if ( this . query && this . query . objectId && objectIdMatch ) {
649
+ idMatch = objectIdMatch ;
637
650
}
638
- return Promise . resolve ( [ ] ) ;
639
- } ) . then ( ( results ) => {
640
- if ( results ) {
641
- deviceTokenMatches = results ;
651
+
652
+ if ( this . data . installationId && installationIdMatch ) {
653
+ idMatch = installationIdMatch ;
642
654
}
655
+
656
+ } ) . then ( ( ) => {
643
657
if ( ! idMatch ) {
644
658
if ( ! deviceTokenMatches . length ) {
645
659
return ;
0 commit comments