From d73125294156e47ca6f4db21f18ef784758a34bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 18 Mar 2015 10:18:03 +0100 Subject: [PATCH] deleteAll returns number of deleted records The number is returned as `data.ctx` to `cb(null, data)`. --- lib/connectors/memory.js | 5 +++- test/basic-querying.test.js | 21 +-------------- test/manipulation.test.js | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 22002c67..e94c3702 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -545,17 +545,20 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) { } var cache = this.collection(model); var filter = null; + var count = 0; if (where) { filter = applyFilter({where: where}); Object.keys(cache).forEach(function (id) { if (!filter || filter(this.fromDb(model, cache[id]))) { + count++; delete cache[id]; } }.bind(this)); } else { + count = Object.keys(cache).length; this.collection(model, {}); } - this.saveToFile(null, callback); + this.saveToFile({ count: count }, callback); }; Memory.prototype.count = function count(model, callback, where) { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 43159d17..f333fb67 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -58,7 +58,7 @@ describe('basic-querying', function () { }); }); - + describe('findByIds', function () { var createdUsers; before(function(done) { @@ -602,25 +602,6 @@ describe('basic-querying', function () { }); - describe('destroyAll with where option', function () { - - before(seed); - - it('should only delete instances that satisfy the where condition', 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(); - }); - }); - }); - }); - - }); describe('updateAll ', function () { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index e0df7896..912b29a8 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -735,6 +735,59 @@ describe('manipulation', function () { it('should destroy filtered set of records'); }); + describe('deleteAll/destroyAll', function () { + beforeEach(function clearOldData(done) { + Person.deleteAll(done); + }); + + beforeEach(function createTestData(done) { + Person.create([ + { name: 'John' }, + { name: 'Jane' } + ], done); + }); + + it('should only delete instances that satisfy the where condition', function (done) { + Person.deleteAll({name: 'John'}, function (err, data) { + if (err) return done(err); + data.count.should.equal(1); + Person.find({where: {name: 'John'}}, function (err, data) { + if (err) return done(err); + data.should.have.length(0); + Person.find({where: {name: 'Jane'}}, function (err, data) { + if (err) return done(err); + data.should.have.length(1); + done(); + }); + }); + }); + }); + + it('should report zero deleted instances', function (done) { + Person.deleteAll({name: 'does-not-match'}, function (err, data) { + if (err) return done(err); + data.count.should.equal(0); + Person.count(function(err, count) { + if (err) return done(err); + count.should.equal(2); + done(); + }); + }); + }); + + it('should delete all instances when "where" is not provided', function(done) { + Person.deleteAll(function (err, data) { + if (err) return done(err); + data.count.should.equal(2); + Person.count(function(err, count) { + if (err) return done(err); + count.should.equal(0); + done(); + }); + }); + }); + }); + describe('initialize', function () { it('should initialize object properly', function () { var hw = 'Hello word',