From 4049ce41029827c2d4c6838b73fc9c618c1773d1 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Wed, 2 Mar 2016 00:21:55 -0800 Subject: [PATCH] Move DatabaseController to use new findOneAndUpdate. --- src/Controllers/DatabaseController.js | 80 +++++++++++++-------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index a7d26245..91507ef8 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -142,51 +142,45 @@ DatabaseController.prototype.update = function(className, query, update, options var isMaster = !('acl' in options); var aclGroup = options.acl || []; var mongoUpdate, schema; - return this.loadSchema(acceptor).then((s) => { - schema = s; - 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]}}); + return this.loadSchema(acceptor) + .then(s => { + schema = s; + if (!isMaster) { + return schema.validatePermission(className, aclGroup, 'update'); } - mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]}; - } - - mongoUpdate = transform.transformUpdate(schema, className, update); - - return coll.findAndModify(mongoWhere, {}, mongoUpdate, {}); - }).then((result) => { - if (!result.value) { - 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, - 'Object not found.')); - } - - var response = {}; - var inc = mongoUpdate['$inc']; - if (inc) { - for (var key in inc) { - response[key] = (result.value[key] || 0) + inc[key]; + return Promise.resolve(); + }) + .then(() => this.handleRelationUpdates(className, query.objectId, update)) + .then(() => this.adaptiveCollection(className)) + .then(collection => { + 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 response; - }); + mongoUpdate = transform.transformUpdate(schema, className, update); + 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.