Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide feedback (result count) for updateAll and destroyAll #321

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/connectors/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,22 @@ 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(null, function(err) {
callback && callback(err, count);
});
};

Memory.prototype.count = function count(model, callback, where) {
Expand All @@ -564,16 +569,20 @@ Memory.prototype.update =
filter = applyFilter({where: where});

var ids = Object.keys(cache);
var count = 0;
async.each(ids, function (id, done) {
var inst = self.fromDb(model, cache[id]);
if (!filter || filter(inst)) {
count++;
self.updateAttributes(model, id, data, done);
} else {
process.nextTick(done);
}
}, function (err) {
if (!err) {
self.saveToFile(null, cb);
self.saveToFile(null, function(err) {
cb && cb(err, count);
});
}
});
};
Expand Down
8 changes: 4 additions & 4 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
* Delete the record with the specified ID.
* Aliases are `destroyById` and `deleteById`.
* @param {*} id The id value
* @param {Function} cb Callback called with (err)
* @param {Function} cb Callback called with (err, deleted)
*/

// [FIXME] rfeng: This is a hack to set up 'deleteById' first so that
Expand All @@ -921,11 +921,11 @@ DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.de
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;

this.remove(byIdQuery(this, id).where, function(err) {
this.remove(byIdQuery(this, id).where, function(err, count) {
if ('function' === typeof cb) {
cb(err);
cb(err, !err && count > 0);
}
if(!err) Model.emit('deleted', id);
if(!err && count > 0) Model.emit('deleted', id);
});
};

Expand Down
8 changes: 6 additions & 2 deletions test/basic-querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ describe('basic-querying', function () {
beforeEach(seed);

it('should only update instances that satisfy the where condition', function (done) {
User.update({name: 'John Lennon'}, {name: 'John Smith'}, function () {
User.update({name: 'John Lennon'}, {name: 'John Smith'}, function (err, count) {
should.not.exist(err);
count.should.equal(1);
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(0);
Expand All @@ -623,7 +625,9 @@ describe('basic-querying', function () {
});

it('should update all instances without where', function (done) {
User.update({name: 'John Smith'}, function () {
User.update({name: 'John Smith'}, function (err, count) {
should.not.exist(err);
count.should.equal(6);
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(0);
Expand Down
12 changes: 8 additions & 4 deletions test/default-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,22 +426,25 @@ describe('default scope', function () {
});

it('should apply default scope', function(done) {
Product.removeById(ids.widgetZ, function(err) {
Product.removeById(ids.widgetZ, function(err, deleted) {
should.not.exist(err);
deleted.should.be.true;
isDeleted(ids.widgetZ, done);
});
});

it('should apply default scope - tool', function(done) {
Tool.removeById(ids.toolA, function(err) {
Tool.removeById(ids.toolA, function(err, deleted) {
should.not.exist(err);
deleted.should.be.true;
isDeleted(ids.toolA, done);
});
});

it('should apply default scope - no match', function(done) {
Tool.removeById(ids.widgetA, function(err) {
Tool.removeById(ids.widgetA, function(err, deleted) {
should.not.exist(err);
deleted.should.be.false;
Product.exists(ids.widgetA, function(err, exists) {
should.not.exist(err);
exists.should.be.true;
Expand All @@ -451,8 +454,9 @@ describe('default scope', function () {
});

it('should apply default scope - widget', function(done) {
Widget.removeById(ids.widgetA, function(err) {
Widget.removeById(ids.widgetA, function(err, deleted) {
should.not.exist(err);
deleted.should.be.true;
isDeleted(ids.widgetA, done);
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/manipulation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ describe('manipulation', function () {
});

it('should destroy all records', function (done) {
Person.destroyAll(function (err) {
Person.destroyAll(function (err, count) {
should.not.exist(err);
count.should.equal(1);
Person.all(function (err, posts) {
posts.should.have.lengthOf(0);
Person.count(function (err, count) {
Expand Down
3 changes: 2 additions & 1 deletion test/memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('Memory connector', function () {
});
}, function (err, results) {
// Now try to delete one
User.deleteById(ids[0], function (err) {
User.deleteById(ids[0], function (err, deleted) {
assert(deleted);
readModels(function (err, json) {
assert.equal(Object.keys(json.models.User).length, 2);
User.upsert({id: ids[1], name: 'John'}, function(err, result) {
Expand Down