@@ -628,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
628
628
skip,
629
629
limit,
630
630
acl,
631
- sort,
631
+ sort = { } ,
632
632
count,
633
633
} = { } ) {
634
634
let isMaster = acl === undefined ;
@@ -646,29 +646,25 @@ DatabaseController.prototype.find = function(className, query, {
646
646
throw error ;
647
647
} )
648
648
. then ( schema => {
649
- const transformedSort = { } ;
650
- if ( sort ) {
651
- for ( let fieldName in sort ) {
652
- // Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
653
- // so duplicate that behaviour here.
654
- if ( fieldName === '_created_at' ) {
655
- fieldName = 'createdAt' ;
656
- sort [ 'createdAt' ] = sort [ '_created_at' ] ;
657
- } else if ( fieldName === '_updated_at' ) {
658
- fieldName = 'updatedAt' ;
659
- sort [ 'updatedAt' ] = sort [ '_updated_at' ] ;
660
- }
661
-
662
- if ( ! SchemaController . fieldNameIsValid ( fieldName ) ) {
663
- throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid field name: ${ fieldName } .` ) ;
664
- }
665
- if ( fieldName . match ( / ^ a u t h D a t a \. ( [ a - z A - Z 0 - 9 _ ] + ) \. i d $ / ) ) {
666
- throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Cannot sort by ${ fieldName } ` ) ;
667
- }
668
- const mongoKey = this . transform . transformKey ( className , fieldName , schema ) ;
669
- transformedSort [ mongoKey ] = sort [ fieldName ] ;
670
- }
649
+ // Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
650
+ // so duplicate that behaviour here. If both are specified, the corrent behaviour to match Parse.com is to
651
+ // use the one that appears first in the sort list.
652
+ if ( sort && sort . _created_at ) {
653
+ sort . createdAt = sort . _created_at ;
654
+ delete sort . _created_at ;
671
655
}
656
+ if ( sort && sort . _updated_at ) {
657
+ sort . updatedAt = sort . _updated_at ;
658
+ delete sort . _updated_at ;
659
+ }
660
+ Object . keys ( sort ) . forEach ( fieldName => {
661
+ if ( fieldName . match ( / ^ a u t h D a t a \. ( [ a - z A - Z 0 - 9 _ ] + ) \. i d $ / ) ) {
662
+ throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Cannot sort by ${ fieldName } ` ) ;
663
+ }
664
+ if ( ! SchemaController . fieldNameIsValid ( fieldName ) ) {
665
+ throw new Parse . Error ( Parse . Error . INVALID_KEY_NAME , `Invalid field name: ${ fieldName } .` ) ;
666
+ }
667
+ } ) ;
672
668
return ( isMaster ? Promise . resolve ( ) : schemaController . validatePermission ( className , aclGroup , op ) )
673
669
. then ( ( ) => this . reduceRelationKeys ( className , query ) )
674
670
. then ( ( ) => this . reduceInRelation ( className , query , schemaController ) )
@@ -691,7 +687,7 @@ DatabaseController.prototype.find = function(className, query, {
691
687
if ( count ) {
692
688
return this . adapter . count ( className , query , schema ) ;
693
689
} else {
694
- return this . adapter . find ( className , query , schema , { skip, limit, sort : transformedSort } )
690
+ return this . adapter . find ( className , query , schema , { skip, limit, sort } )
695
691
. then ( objects => objects . map ( object => filterSensitiveData ( isMaster , aclGroup , className , object ) ) ) ;
696
692
}
697
693
} ) ;
0 commit comments