Skip to content

Commit 3ed3c7b

Browse files
committed
Move more mongo specific stuff into mongo adapter
1 parent 05ae010 commit 3ed3c7b

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ export class MongoStorageAdapter {
200200
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
201201
find(className, query, schema, { skip, limit, sort }) {
202202
let mongoWhere = this.transform.transformWhere(className, query, schema);
203+
let mongoSort = _.mapKeys(sort, (value, fieldName) => transform.transformKey(className, fieldName, schema));
203204
return this.adaptiveCollection(className)
204-
.then(collection => collection.find(mongoWhere, { skip, limit, sort }))
205+
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
205206
.then(objects => objects.map(object => transform.mongoObjectToParseObject(className, object, schema)));
206207
}
207208

src/Controllers/DatabaseController.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
628628
skip,
629629
limit,
630630
acl,
631-
sort,
631+
sort = {},
632632
count,
633633
} = {}) {
634634
let isMaster = acl === undefined;
@@ -646,29 +646,25 @@ DatabaseController.prototype.find = function(className, query, {
646646
throw error;
647647
})
648648
.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(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
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;
671655
}
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(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
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+
});
672668
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))
673669
.then(() => this.reduceRelationKeys(className, query))
674670
.then(() => this.reduceInRelation(className, query, schemaController))
@@ -691,7 +687,7 @@ DatabaseController.prototype.find = function(className, query, {
691687
if (count) {
692688
return this.adapter.count(className, query, schema);
693689
} else {
694-
return this.adapter.find(className, query, schema, { skip, limit, sort: transformedSort })
690+
return this.adapter.find(className, query, schema, { skip, limit, sort })
695691
.then(objects => objects.map(object => filterSensitiveData(isMaster, aclGroup, className, object)));
696692
}
697693
});

0 commit comments

Comments
 (0)