diff --git a/lib/datastore.js b/lib/datastore.js index 535d7be..3b6c4f1 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -218,18 +218,46 @@ Datastore.prototype.update = function (query, newDoc, options, cb) { }; +/** + * Remove all docs matching the query + * For now very naive implementation (similar to update) + * @param {Object} query + * @param {Object} options Optional options + * options.multi If true, can update multiple documents (defaults to false) + * @param {Function} cb Optional callback, signature: err, numRemoved + */ +Datastore.prototype.remove = function (query, options, cb) { + var callback + , self = this + , numRemoved = 0 + , multi + , newData = []; + + if (typeof options === 'function') { cb = options; options = {}; } + callback = cb || function () {}; + multi = options.multi !== undefined ? options.multi : false; + + self.data.forEach(function (d) { + if (Datastore.match(d, query) && (multi || numRemoved === 0)) { + numRemoved += 1; + } else { + newData.push(d); + } + }); + + self.persistWholeDatabase(newData, function (err) { + if (err) { return callback(err); } + self.data = newData; + return callback(null, numRemoved); + }); +}; + var d = new Datastore('workspace/test.db'); d.loadDatabase(function (err) { console.log(d.data); - d.update({ yahoo: 'great' }, {yahoo: 'great2' }, { multi: false }, function (err, n) { - console.log("--------------"); - console.log(err); - console.log(n); - }); - });