@@ -599,8 +599,16 @@ DatabaseController.prototype.deleteEverything = function() {
599599
600600// Returns a promise for a list of related ids given an owning id.
601601// 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 )
604612 . then ( results => results . map ( result => result . relatedId ) ) ;
605613} ;
606614
@@ -693,11 +701,11 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
693701
694702// Modifies query so that it no longer has $relatedTo
695703// Returns a promise that resolves when query is mutated
696- DatabaseController . prototype . reduceRelationKeys = function ( className , query ) {
704+ DatabaseController . prototype . reduceRelationKeys = function ( className , query , queryOptions ) {
697705
698706 if ( query [ '$or' ] ) {
699707 return Promise . all ( query [ '$or' ] . map ( ( aQuery ) => {
700- return this . reduceRelationKeys ( className , aQuery ) ;
708+ return this . reduceRelationKeys ( className , aQuery , queryOptions ) ;
701709 } ) ) ;
702710 }
703711
@@ -706,11 +714,12 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
706714 return this . relatedIds (
707715 relatedTo . object . className ,
708716 relatedTo . key ,
709- relatedTo . object . objectId )
717+ relatedTo . object . objectId ,
718+ queryOptions )
710719 . then ( ( ids ) => {
711720 delete query [ '$relatedTo' ] ;
712721 this . addInObjectIdsIds ( ids , query ) ;
713- return this . reduceRelationKeys ( className , query ) ;
722+ return this . reduceRelationKeys ( className , query , queryOptions ) ;
714723 } ) ;
715724 }
716725} ;
@@ -831,8 +840,9 @@ DatabaseController.prototype.find = function(className, query, {
831840 throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid field name: ${ fieldName } .` ) ;
832841 }
833842 } ) ;
843+ const queryOptions = { skip, limit, sort, keys, readPreference } ;
834844 return ( isMaster ? Promise . resolve ( ) : schemaController . validatePermission ( className , aclGroup , op ) )
835- . then ( ( ) => this . reduceRelationKeys ( className , query ) )
845+ . then ( ( ) => this . reduceRelationKeys ( className , query , queryOptions ) )
836846 . then ( ( ) => this . reduceInRelation ( className , query , schemaController ) )
837847 . then ( ( ) => {
838848 if ( ! isMaster ) {
@@ -871,7 +881,7 @@ DatabaseController.prototype.find = function(className, query, {
871881 if ( ! classExists ) {
872882 return [ ] ;
873883 } else {
874- return this . adapter . find ( className , schema , query , { skip , limit , sort , keys , readPreference } )
884+ return this . adapter . find ( className , schema , query , queryOptions )
875885 . then ( objects => objects . map ( object => {
876886 object = untransformObjectACL ( object ) ;
877887 return filterSensitiveData ( isMaster , aclGroup , className , object )
0 commit comments