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

Loading…
Cancel
Save