Move DatabaseController to use new findOneAndUpdate.

This commit is contained in:
Nikita Lutsenko
2016-03-02 00:21:55 -08:00
parent 6d7813be4a
commit 4049ce4102

View File

@@ -142,51 +142,45 @@ DatabaseController.prototype.update = function(className, query, update, options
var isMaster = !('acl' in options); var isMaster = !('acl' in options);
var aclGroup = options.acl || []; var aclGroup = options.acl || [];
var mongoUpdate, schema; var mongoUpdate, schema;
return this.loadSchema(acceptor).then((s) => { return this.loadSchema(acceptor)
schema = s; .then(s => {
if (!isMaster) { schema = s;
return schema.validatePermission(className, aclGroup, 'update'); if (!isMaster) {
} return schema.validatePermission(className, aclGroup, 'update');
return Promise.resolve();
}).then(() => {
return this.handleRelationUpdates(className, query.objectId, update);
}).then(() => {
return this.collection(className);
}).then((coll) => {
var mongoWhere = transform.transformWhere(schema, className, query);
if (options.acl) {
var writePerms = [
{_wperm: {'$exists': false}}
];
for (var entry of options.acl) {
writePerms.push({_wperm: {'$in': [entry]}});
} }
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]}; return Promise.resolve();
} })
.then(() => this.handleRelationUpdates(className, query.objectId, update))
mongoUpdate = transform.transformUpdate(schema, className, update); .then(() => this.adaptiveCollection(className))
.then(collection => {
return coll.findAndModify(mongoWhere, {}, mongoUpdate, {}); var mongoWhere = transform.transformWhere(schema, className, query);
}).then((result) => { if (options.acl) {
if (!result.value) { var writePerms = [
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, {_wperm: {'$exists': false}}
'Object not found.')); ];
} for (var entry of options.acl) {
if (result.lastErrorObject.n != 1) { writePerms.push({_wperm: {'$in': [entry]}});
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, }
'Object not found.')); mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
}
var response = {};
var inc = mongoUpdate['$inc'];
if (inc) {
for (var key in inc) {
response[key] = (result.value[key] || 0) + inc[key];
} }
} mongoUpdate = transform.transformUpdate(schema, className, update);
return response; return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
}); })
.then(result => {
if (!result) {
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
'Object not found.'));
}
let response = {};
let inc = mongoUpdate['$inc'];
if (inc) {
Object.keys(inc).forEach(key => {
response[key] = result[key];
});
}
return response;
});
}; };
// Processes relation-updating operations from a REST-format update. // Processes relation-updating operations from a REST-format update.