Better way to update

pull/2/head
Louis Chatriot 12 years ago
parent 4b780922d6
commit 578c60ab79
  1. 18
      lib/datastore.js

@ -240,9 +240,8 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
, self = this
, numReplaced = 0
, multi, upsert
, modifiedDoc
, newData = []
, updatedDocs = []
, i
;
if (typeof options === 'function') { cb = options; options = {}; }
@ -270,24 +269,19 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
}
, function () { // Perform the update
try {
self.data.forEach(function (d) {
if (model.match(d, query) && (multi || numReplaced === 0)) {
for (i = 0; i < self.data.length; i += 1) {
if (model.match(self.data[i], query) && (multi || numReplaced === 0)) {
numReplaced += 1;
modifiedDoc = model.modify(d, updateQuery);
newData.push(modifiedDoc);
updatedDocs.push(modifiedDoc);
} else {
newData.push(d);
self.data[i] = model.modify(self.data[i], updateQuery);
updatedDocs.push(self.data[i]);
}
});
}
} catch (err) {
return callback(err);
}
self.persistNewState(updatedDocs, function (err) {
if (err) { return callback(err); }
self.data = newData;
return callback(null, numReplaced);
});
}

Loading…
Cancel
Save