Merge pull request #511 from strongloop/feature/report-count-of-deleted-instances
deleteAll returns number of deleted records
This commit is contained in:
commit
e83edf8555
|
@ -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) {
|
||||
|
|
|
@ -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 () {
|
||||
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue