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 }. // Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
find(className, query, schema, { skip, limit, sort }) { find(className, query, schema, { skip, limit, sort }) {
let mongoWhere = this.transform.transformWhere(className, query, schema); let mongoWhere = this.transform.transformWhere(className, query, schema);
let mongoSort = _.mapKeys(sort, (value, fieldName) => transform.transformKey(className, fieldName, schema));
return this.adaptiveCollection(className) 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))); .then(objects => objects.map(object => transform.mongoObjectToParseObject(className, object, schema)));
} }

View File

@@ -628,7 +628,7 @@ DatabaseController.prototype.find = function(className, query, {
skip, skip,
limit, limit,
acl, acl,
sort, sort = {},
count, count,
} = {}) { } = {}) {
let isMaster = acl === undefined; let isMaster = acl === undefined;
@@ -646,29 +646,25 @@ DatabaseController.prototype.find = function(className, query, {
throw error; throw error;
}) })
.then(schema => { .then(schema => {
const transformedSort = {}; // Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
if (sort) { // so duplicate that behaviour here. If both are specified, the corrent behaviour to match Parse.com is to
for (let fieldName in sort) { // use the one that appears first in the sort list.
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt, if (sort && sort._created_at) {
// so duplicate that behaviour here. sort.createdAt = sort._created_at;
if (fieldName === '_created_at') { delete sort._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];
}
} }
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)) return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))
.then(() => this.reduceRelationKeys(className, query)) .then(() => this.reduceRelationKeys(className, query))
.then(() => this.reduceInRelation(className, query, schemaController)) .then(() => this.reduceInRelation(className, query, schemaController))
@@ -691,7 +687,7 @@ DatabaseController.prototype.find = function(className, query, {
if (count) { if (count) {
return this.adapter.count(className, query, schema); return this.adapter.count(className, query, schema);
} else { } 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))); .then(objects => objects.map(object => filterSensitiveData(isMaster, aclGroup, className, object)));
} }
}); });