Move more mongo specific stuff into mongo adapter

This commit is contained in:
Drew Gross
2016-05-23 21:16:03 -07:00
parent 05ae010b91
commit 3ed3c7b62f
2 changed files with 22 additions and 25 deletions

View File

@@ -200,8 +200,9 @@ export class MongoStorageAdapter {
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
find(className, query, schema, { skip, limit, sort }) {
let mongoWhere = this.transform.transformWhere(className, query, schema);
let mongoSort = _.mapKeys(sort, (value, fieldName) => transform.transformKey(className, fieldName, schema));
return this.adaptiveCollection(className)
.then(collection => collection.find(mongoWhere, { skip, limit, sort }))
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
.then(objects => objects.map(object => transform.mongoObjectToParseObject(className, object, schema)));
}

View File

@@ -628,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
skip,
limit,
acl,
sort,
sort = {},
count,
} = {}) {
let isMaster = acl === undefined;
@@ -646,29 +646,25 @@ DatabaseController.prototype.find = function(className, query, {
throw error;
})
.then(schema => {
const transformedSort = {};
if (sort) {
for (let fieldName in sort) {
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
// so duplicate that behaviour here.
if (fieldName === '_created_at') {
fieldName = 'createdAt';
sort['createdAt'] = sort['_created_at'];
} else if (fieldName === '_updated_at') {
fieldName = 'updatedAt';
sort['updatedAt'] = sort['_updated_at'];
}
if (!SchemaController.fieldNameIsValid(fieldName)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
}
if (fieldName.match(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Cannot sort by ${fieldName}`);
}
const mongoKey = this.transform.transformKey(className, fieldName, schema);
transformedSort[mongoKey] = sort[fieldName];
}
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
// so duplicate that behaviour here. If both are specified, the corrent behaviour to match Parse.com is to
// use the one that appears first in the sort list.
if (sort && sort._created_at) {
sort.createdAt = sort._created_at;
delete sort._created_at;
}
if (sort && sort._updated_at) {
sort.updatedAt = sort._updated_at;
delete sort._updated_at;
}
Object.keys(sort).forEach(fieldName => {
if (fieldName.match(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Cannot sort by ${fieldName}`);
}
if (!SchemaController.fieldNameIsValid(fieldName)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
}
});
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))
.then(() => this.reduceRelationKeys(className, query))
.then(() => this.reduceInRelation(className, query, schemaController))
@@ -691,7 +687,7 @@ DatabaseController.prototype.find = function(className, query, {
if (count) {
return this.adapter.count(className, query, schema);
} else {
return this.adapter.find(className, query, schema, { skip, limit, sort: transformedSort })
return this.adapter.find(className, query, schema, { skip, limit, sort })
.then(objects => objects.map(object => filterSensitiveData(isMaster, aclGroup, className, object)));
}
});