Pass parse format schema to transformWhere

This commit is contained in:
Drew Gross
2016-04-25 22:45:16 -07:00
parent 608cba9e8c
commit 4371ca164c
3 changed files with 77 additions and 38 deletions

View File

@@ -174,14 +174,15 @@ export class MongoStorageAdapter {
// If there is some other error, reject with INTERNAL_SERVER_ERROR. // If there is some other error, reject with INTERNAL_SERVER_ERROR.
// Currently accepts the schemaController, and validate for lecacy reasons // Currently accepts the schemaController, and validate for lecacy reasons
deleteObjectsByQuery(className, query, schemaController, validate) { deleteObjectsByQuery(className, query, schemaController, validate, parseFormatSchema) {
return this.adaptiveCollection(className) return this.adaptiveCollection(className)
.then(collection => { .then(collection => {
let mongoWhere = transform.transformWhere( let mongoWhere = transform.transformWhere(
schemaController, schemaController,
className, className,
query, query,
{ validate } { validate },
parseFormatSchema
); );
return collection.deleteMany(mongoWhere) return collection.deleteMany(mongoWhere)
}) })

View File

@@ -139,7 +139,7 @@ const valueAsDate = value => {
return false; return false;
} }
function transformQueryKeyValue(schema, className, key, value, { validate } = {}) { function transformQueryKeyValue(schema, className, key, value, { validate } = {}, parseFormatSchema) {
switch(key) { switch(key) {
case 'createdAt': case 'createdAt':
if (valueAsDate(value)) { if (valueAsDate(value)) {
@@ -168,12 +168,12 @@ function transformQueryKeyValue(schema, className, key, value, { validate } = {}
if (!(value instanceof Array)) { if (!(value instanceof Array)) {
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $or format - use an array value'); throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $or format - use an array value');
} }
return {key: '$or', value: value.map(subQuery => transformWhere(schema, className, subQuery))}; return {key: '$or', value: value.map(subQuery => transformWhere(schema, className, subQuery, parseFormatSchema))};
case '$and': case '$and':
if (!(value instanceof Array)) { if (!(value instanceof Array)) {
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $and format - use an array value'); throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $and format - use an array value');
} }
return {key: '$and', value: value.map(subQuery => transformWhere(schema, className, subQuery))}; return {key: '$and', value: value.map(subQuery => transformWhere(schema, className, subQuery, parseFormatSchema))};
default: default:
// Other auth data // Other auth data
const authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/); const authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
@@ -218,13 +218,13 @@ function transformQueryKeyValue(schema, className, key, value, { validate } = {}
// restWhere is the "where" clause in REST API form. // restWhere is the "where" clause in REST API form.
// Returns the mongo form of the query. // Returns the mongo form of the query.
// Throws a Parse.Error if the input query is invalid. // Throws a Parse.Error if the input query is invalid.
function transformWhere(schema, className, restWhere, { validate = true } = {}) { function transformWhere(schema, className, restWhere, { validate = true } = {}, parseFormatSchema) {
let mongoWhere = {}; let mongoWhere = {};
if (restWhere['ACL']) { if (restWhere['ACL']) {
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Cannot query on ACL.'); throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Cannot query on ACL.');
} }
for (let restKey in restWhere) { for (let restKey in restWhere) {
let out = transformQueryKeyValue(schema, className, restKey, restWhere[restKey], { validate }); let out = transformQueryKeyValue(schema, className, restKey, restWhere[restKey], { validate }, parseFormatSchema);
mongoWhere[out.key] = out.value; mongoWhere[out.key] = out.value;
} }
return mongoWhere; return mongoWhere;

View File

@@ -174,11 +174,22 @@ DatabaseController.prototype.update = function(className, query, update, {
if (acl) { if (acl) {
query = addWriteACL(query, acl); query = addWriteACL(query, acl);
} }
return schemaController.getOneSchema(className)
.catch(error => {
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
// will likely need revisiting.
if (error === undefined) {
return { fields: {} };
}
throw error;
})
.then(parseFormatSchema => {
var mongoWhere = this.transform.transformWhere( var mongoWhere = this.transform.transformWhere(
schemaController, schemaController,
className, className,
query, query,
{validate: !this.skipValidation} {validate: !this.skipValidation},
parseFormatSchema
); );
mongoUpdate = this.transform.transformUpdate( mongoUpdate = this.transform.transformUpdate(
schemaController, schemaController,
@@ -193,6 +204,7 @@ DatabaseController.prototype.update = function(className, query, update, {
} else { } else {
return collection.findOneAndUpdate(mongoWhere, mongoUpdate); return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
} }
});
}) })
.then(result => { .then(result => {
if (!result) { if (!result) {
@@ -322,7 +334,22 @@ DatabaseController.prototype.destroy = function(className, query, { acl } = {})
if (acl) { if (acl) {
query = addWriteACL(query, acl); query = addWriteACL(query, acl);
} }
return this.adapter.deleteObjectsByQuery(className, query, schemaController, !this.skipValidation) return schemaController.getOneSchema(className)
.catch(error => {
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
// will likely need revisiting.
if (error === undefined) {
return { fields: {} };
}
throw error;
})
.then(parseFormatSchema => this.adapter.deleteObjectsByQuery(
className,
query,
schemaController,
!this.skipValidation,
parseFormatSchema
))
.catch(error => { .catch(error => {
// When deleting sessions while changing passwords, don't throw an error if they don't have any sessions. // When deleting sessions while changing passwords, don't throw an error if they don't have any sessions.
if (className === "_Session" && error.code === Parse.Error.OBJECT_NOT_FOUND) { if (className === "_Session" && error.code === Parse.Error.OBJECT_NOT_FOUND) {
@@ -627,7 +654,17 @@ DatabaseController.prototype.find = function(className, query, {
if (!isMaster) { if (!isMaster) {
query = addReadACL(query, aclGroup); query = addReadACL(query, aclGroup);
} }
let mongoWhere = this.transform.transformWhere(schemaController, className, query); return schemaController.getOneSchema(className)
.catch(error => {
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
// will likely need revisiting.
if (error === undefined) {
return { fields: {} };
}
throw error;
})
.then(parseFormatSchema => {
let mongoWhere = this.transform.transformWhere(schemaController, className, query, parseFormatSchema);
if (count) { if (count) {
delete mongoOptions.limit; delete mongoOptions.limit;
return collection.count(mongoWhere, mongoOptions); return collection.count(mongoWhere, mongoOptions);
@@ -641,6 +678,7 @@ DatabaseController.prototype.find = function(className, query, {
} }
}); });
}); });
});
}; };
DatabaseController.prototype.deleteSchema = function(className) { DatabaseController.prototype.deleteSchema = function(className) {