@@ -599,8 +599,16 @@ DatabaseController.prototype.deleteEverything = function() {
599
599
600
600
// Returns a promise for a list of related ids given an owning id.
601
601
// className here is the owning className.
602
- DatabaseController . prototype . relatedIds = function ( className , key , owningId ) {
603
- return this . adapter . find ( joinTableName ( className , key ) , relationSchema , { owningId } , { } )
602
+ DatabaseController . prototype . relatedIds = function ( className , key , owningId , queryOptions ) {
603
+ const { skip, limit, sort } = queryOptions ;
604
+ const findOptions = { } ;
605
+ if ( sort && sort . createdAt && this . adapter . canSortOnJoinTables ) {
606
+ findOptions . sort = { '_id' : sort . createdAt } ;
607
+ findOptions . limit = limit ;
608
+ findOptions . skip = skip ;
609
+ queryOptions . skip = 0 ;
610
+ }
611
+ return this . adapter . find ( joinTableName ( className , key ) , relationSchema , { owningId } , findOptions )
604
612
. then ( results => results . map ( result => result . relatedId ) ) ;
605
613
} ;
606
614
@@ -693,11 +701,11 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
693
701
694
702
// Modifies query so that it no longer has $relatedTo
695
703
// Returns a promise that resolves when query is mutated
696
- DatabaseController . prototype . reduceRelationKeys = function ( className , query ) {
704
+ DatabaseController . prototype . reduceRelationKeys = function ( className , query , queryOptions ) {
697
705
698
706
if ( query [ '$or' ] ) {
699
707
return Promise . all ( query [ '$or' ] . map ( ( aQuery ) => {
700
- return this . reduceRelationKeys ( className , aQuery ) ;
708
+ return this . reduceRelationKeys ( className , aQuery , queryOptions ) ;
701
709
} ) ) ;
702
710
}
703
711
@@ -706,11 +714,12 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
706
714
return this . relatedIds (
707
715
relatedTo . object . className ,
708
716
relatedTo . key ,
709
- relatedTo . object . objectId )
717
+ relatedTo . object . objectId ,
718
+ queryOptions )
710
719
. then ( ( ids ) => {
711
720
delete query [ '$relatedTo' ] ;
712
721
this . addInObjectIdsIds ( ids , query ) ;
713
- return this . reduceRelationKeys ( className , query ) ;
722
+ return this . reduceRelationKeys ( className , query , queryOptions ) ;
714
723
} ) ;
715
724
}
716
725
} ;
@@ -831,8 +840,9 @@ DatabaseController.prototype.find = function(className, query, {
831
840
throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid field name: ${ fieldName } .` ) ;
832
841
}
833
842
} ) ;
843
+ const queryOptions = { skip, limit, sort, keys, readPreference } ;
834
844
return ( isMaster ? Promise . resolve ( ) : schemaController . validatePermission ( className , aclGroup , op ) )
835
- . then ( ( ) => this . reduceRelationKeys ( className , query ) )
845
+ . then ( ( ) => this . reduceRelationKeys ( className , query , queryOptions ) )
836
846
. then ( ( ) => this . reduceInRelation ( className , query , schemaController ) )
837
847
. then ( ( ) => {
838
848
if ( ! isMaster ) {
@@ -871,7 +881,7 @@ DatabaseController.prototype.find = function(className, query, {
871
881
if ( ! classExists ) {
872
882
return [ ] ;
873
883
} else {
874
- return this . adapter . find ( className , schema , query , { skip , limit , sort , keys , readPreference } )
884
+ return this . adapter . find ( className , schema , query , queryOptions )
875
885
. then ( objects => objects . map ( object => {
876
886
object = untransformObjectACL ( object ) ;
877
887
return filterSensitiveData ( isMaster , aclGroup , className , object )
0 commit comments