Skip to content

Commit 4049ce4

Browse files
committed
Move DatabaseController to use new findOneAndUpdate.
1 parent 6d7813b commit 4049ce4

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

src/Controllers/DatabaseController.js

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -142,51 +142,45 @@ DatabaseController.prototype.update = function(className, query, update, options
142142
var isMaster = !('acl' in options);
143143
var aclGroup = options.acl || [];
144144
var mongoUpdate, schema;
145-
return this.loadSchema(acceptor).then((s) => {
146-
schema = s;
147-
if (!isMaster) {
148-
return schema.validatePermission(className, aclGroup, 'update');
149-
}
150-
return Promise.resolve();
151-
}).then(() => {
152-
153-
return this.handleRelationUpdates(className, query.objectId, update);
154-
}).then(() => {
155-
return this.collection(className);
156-
}).then((coll) => {
157-
var mongoWhere = transform.transformWhere(schema, className, query);
158-
if (options.acl) {
159-
var writePerms = [
160-
{_wperm: {'$exists': false}}
161-
];
162-
for (var entry of options.acl) {
163-
writePerms.push({_wperm: {'$in': [entry]}});
145+
return this.loadSchema(acceptor)
146+
.then(s => {
147+
schema = s;
148+
if (!isMaster) {
149+
return schema.validatePermission(className, aclGroup, 'update');
150+
}
151+
return Promise.resolve();
152+
})
153+
.then(() => this.handleRelationUpdates(className, query.objectId, update))
154+
.then(() => this.adaptiveCollection(className))
155+
.then(collection => {
156+
var mongoWhere = transform.transformWhere(schema, className, query);
157+
if (options.acl) {
158+
var writePerms = [
159+
{_wperm: {'$exists': false}}
160+
];
161+
for (var entry of options.acl) {
162+
writePerms.push({_wperm: {'$in': [entry]}});
163+
}
164+
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
165+
}
166+
mongoUpdate = transform.transformUpdate(schema, className, update);
167+
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
168+
})
169+
.then(result => {
170+
if (!result) {
171+
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
172+
'Object not found.'));
164173
}
165-
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]};
166-
}
167-
168-
mongoUpdate = transform.transformUpdate(schema, className, update);
169-
170-
return coll.findAndModify(mongoWhere, {}, mongoUpdate, {});
171-
}).then((result) => {
172-
if (!result.value) {
173-
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
174-
'Object not found.'));
175-
}
176-
if (result.lastErrorObject.n != 1) {
177-
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
178-
'Object not found.'));
179-
}
180174

181-
var response = {};
182-
var inc = mongoUpdate['$inc'];
183-
if (inc) {
184-
for (var key in inc) {
185-
response[key] = (result.value[key] || 0) + inc[key];
175+
let response = {};
176+
let inc = mongoUpdate['$inc'];
177+
if (inc) {
178+
Object.keys(inc).forEach(key => {
179+
response[key] = result[key];
180+
});
186181
}
187-
}
188-
return response;
189-
});
182+
return response;
183+
});
190184
};
191185

192186
// Processes relation-updating operations from a REST-format update.

0 commit comments

Comments
 (0)