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,18 +142,17 @@ 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)
.then(s => {
schema = s; schema = s;
if (!isMaster) { if (!isMaster) {
return schema.validatePermission(className, aclGroup, 'update'); return schema.validatePermission(className, aclGroup, 'update');
} }
return Promise.resolve(); return Promise.resolve();
}).then(() => { })
.then(() => this.handleRelationUpdates(className, query.objectId, update))
return this.handleRelationUpdates(className, query.objectId, update); .then(() => this.adaptiveCollection(className))
}).then(() => { .then(collection => {
return this.collection(className);
}).then((coll) => {
var mongoWhere = transform.transformWhere(schema, className, query); var mongoWhere = transform.transformWhere(schema, className, query);
if (options.acl) { if (options.acl) {
var writePerms = [ var writePerms = [
@@ -164,26 +163,21 @@ DatabaseController.prototype.update = function(className, query, update, options
} }
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]}; mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
} }
mongoUpdate = transform.transformUpdate(schema, className, update); mongoUpdate = transform.transformUpdate(schema, className, update);
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
return coll.findAndModify(mongoWhere, {}, mongoUpdate, {}); })
}).then((result) => { .then(result => {
if (!result.value) { if (!result) {
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
'Object not found.'));
}
if (result.lastErrorObject.n != 1) {
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
'Object not found.')); 'Object not found.'));
} }
var response = {}; let response = {};
var inc = mongoUpdate['$inc']; let inc = mongoUpdate['$inc'];
if (inc) { if (inc) {
for (var key in inc) { Object.keys(inc).forEach(key => {
response[key] = (result.value[key] || 0) + inc[key]; response[key] = result[key];
} });
} }
return response; return response;
}); });