From 4b780922d60360f5070b5a1fed017e8aa9071a1b Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 May 2013 14:25:41 +0200 Subject: [PATCH] Use append-only format for removes too --- lib/datastore.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index dbff47b..871e06f 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -242,7 +242,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { , multi, upsert , modifiedDoc , newData = [] - , updatedData = [] + , updatedDocs = [] ; if (typeof options === 'function') { cb = options; options = {}; } @@ -276,7 +276,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { modifiedDoc = model.modify(d, updateQuery); newData.push(modifiedDoc); - updatedData.push(modifiedDoc); + updatedDocs.push(modifiedDoc); } else { newData.push(d); } @@ -285,7 +285,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { return callback(err); } - self.persistNewState(updatedData, function (err) { + self.persistNewState(updatedDocs, function (err) { if (err) { return callback(err); } self.data = newData; return callback(null, numReplaced); @@ -313,7 +313,9 @@ Datastore.prototype._remove = function (query, options, cb) { , self = this , numRemoved = 0 , multi - , newData = []; + , newData = [] + , removedDocs = [] + ; if (typeof options === 'function') { cb = options; options = {}; } callback = cb || function () {}; @@ -323,6 +325,7 @@ Datastore.prototype._remove = function (query, options, cb) { self.data.forEach(function (d) { if (model.match(d, query) && (multi || numRemoved === 0)) { numRemoved += 1; + removedDocs.push({ $$deleted: true, _id: d._id }); } else { newData.push(d); } @@ -331,7 +334,7 @@ Datastore.prototype._remove = function (query, options, cb) { return callback(err); } - self.persistWholeDatabase(newData, function (err) { + self.persistNewState(removedDocs, function (err) { if (err) { return callback(err); } self.data = newData; return callback(null, numRemoved);