Merge pull request #511 from strongloop/feature/report-count-of-deleted-instances

deleteAll returns number of deleted records
This commit is contained in:
Miroslav Bajtoš 2015-03-19 19:01:30 +01:00
commit e83edf8555
3 changed files with 58 additions and 21 deletions

View File

@ -545,17 +545,20 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
} }
var cache = this.collection(model); var cache = this.collection(model);
var filter = null; var filter = null;
var count = 0;
if (where) { if (where) {
filter = applyFilter({where: where}); filter = applyFilter({where: where});
Object.keys(cache).forEach(function (id) { Object.keys(cache).forEach(function (id) {
if (!filter || filter(this.fromDb(model, cache[id]))) { if (!filter || filter(this.fromDb(model, cache[id]))) {
count++;
delete cache[id]; delete cache[id];
} }
}.bind(this)); }.bind(this));
} else { } else {
count = Object.keys(cache).length;
this.collection(model, {}); this.collection(model, {});
} }
this.saveToFile(null, callback); this.saveToFile({ count: count }, callback);
}; };
Memory.prototype.count = function count(model, callback, where) { Memory.prototype.count = function count(model, callback, where) {

View File

@ -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 () { describe('updateAll ', function () {

View File

@ -735,6 +735,59 @@ describe('manipulation', function () {
it('should destroy filtered set of records'); 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 () { describe('initialize', function () {
it('should initialize object properly', function () { it('should initialize object properly', function () {
var hw = 'Hello word', var hw = 'Hello word',