From 9482d3c7debf8cbdd34b83a0c1feab788a772d5f Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sun, 18 Aug 2013 10:58:53 -0700 Subject: [PATCH] Fix the where option for delete --- lib/connectors/memory.js | 21 +++++++++++++++++---- lib/dao.js | 29 +++++++++++++++++++++-------- test/basic-querying.test.js | 21 +++++++++++++++++++++ 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index aa128da7..7174f396 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -225,11 +225,24 @@ function applyFilter(filter) { } } -Memory.prototype.destroyAll = function destroyAll(model, callback) { - Object.keys(this.cache[model]).forEach(function (id) { - delete this.cache[model][id]; +Memory.prototype.destroyAll = function destroyAll(model, where, callback) { + if(!callback && 'function' === typeof where) { + callback = where; + where = undefined; + } + var cache = this.cache[model]; + var filter = null; + if (where) { + filter = applyFilter({where: where}); + } + Object.keys(cache).forEach(function (id) { + if(!filter || filter(this.fromDb(model, cache[id]))) { + delete cache[id]; + } }.bind(this)); - this.cache[model] = {}; + if(!where) { + this.cache[model] = {}; + } process.nextTick(callback); }; diff --git a/lib/dao.js b/lib/dao.js index 2537604d..d1da0138 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -469,18 +469,29 @@ DataAccessObject.findOne.http = [ ]; /** - * Destroy all records - * @param {Function} cb - callback called with (err) + * Destroy all matching records + * @param {Object} [where] An object that defines the criteria + * @param {Function} [cb] - callback called with (err) */ +DataAccessObject.remove = DataAccessObject.deleteAll = -DataAccessObject.destroyAll = function destroyAll(cb) { +DataAccessObject.destroyAll = function destroyAll(where, cb) { if (stillConnecting(this.dataSource, this, arguments)) return; - this.dataSource.connector.destroyAll(this.modelName, function (err) { - if ('function' === typeof cb) { - cb(err); - } - }.bind(this)); + if(!cb && 'function' === typeof where) { + cb = where; + where = undefined; + } + if(!where) { + this.dataSource.connector.destroyAll(this.modelName, function (err, data) { + cb && cb(err, data); + }.bind(this)); + } else { + // Support an optional where object + this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) { + cb && cb(err, data); + }.bind(this)); + } }; /** @@ -488,6 +499,7 @@ DataAccessObject.destroyAll = function destroyAll(cb) { * @param {*} id The id value * @param {Function} cb - callback called with (err) */ +DataAccessObject.removeById = DataAccessObject.deleteById = DataAccessObject.destroyById = function deleteById(id, cb) { if (stillConnecting(this.dataSource, this, arguments)) return; @@ -633,6 +645,7 @@ DataAccessObject.prototype._adapter = function () { * * @triggers `destroy` hook (async) before and after destroying object */ +DataAccessObject.prototype.remove = DataAccessObject.prototype.delete = DataAccessObject.prototype.destroy = function (cb) { if (stillConnecting(this.constructor.dataSource, this, arguments)) return; diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index a9c7e6c3..fcc0088f 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -291,6 +291,27 @@ describe('basic-querying', function() { }); + describe('destroyAll', function() { + + before(seed); + + it('should check whether record not exist', function(done) { + User.destroyAll({name: 'John Lennon'}, function() { + User.find({where: {name: 'John Lennon'}}, function(err, data) { + should.not.exist(err); + data.length.should.equal(0); + User.find({where: {name: 'Paul McCartney'}}, function(err, data) { + should.not.exist(err); + data.length.should.equal(1); + done(); + }); + }); + }); + }); + + }); + + });