Memory connector returns updated records count

The memory connector now includes the number of updated items in its callback
when updating records.
This commit is contained in:
Simon Ho 2015-03-11 21:10:30 -07:00
parent a8d8556dcc
commit 04f35b31b4
4 changed files with 106 additions and 61 deletions

View File

@ -585,9 +585,11 @@ Memory.prototype.update =
filter = applyFilter({where: where}); filter = applyFilter({where: where});
var ids = Object.keys(cache); var ids = Object.keys(cache);
var count = 0;
async.each(ids, function (id, done) { async.each(ids, function (id, done) {
var inst = self.fromDb(model, cache[id]); var inst = self.fromDb(model, cache[id]);
if (!filter || filter(inst)) { if (!filter || filter(inst)) {
count++;
// The id value from the cache is string // The id value from the cache is string
// Get the real id from the inst // Get the real id from the inst
id = self.getIdValue(model, inst); id = self.getIdValue(model, inst);
@ -596,9 +598,8 @@ Memory.prototype.update =
process.nextTick(done); process.nextTick(done);
} }
}, function (err) { }, function (err) {
if (!err) { if (err) return cb(err);
self.saveToFile(null, cb); self.saveToFile({count: count}, cb);
}
}); });
}; };

View File

@ -97,7 +97,8 @@ Transient.prototype.save = function save(model, data, callback) {
Transient.prototype.update = Transient.prototype.update =
Transient.prototype.updateAll = function updateAll(model, where, data, cb) { Transient.prototype.updateAll = function updateAll(model, where, data, cb) {
this.flush('update', null, cb); var count = 0;
this.flush('update', count, cb);
}; };
Transient.prototype.updateAttributes = function updateAttributes(model, id, data, cb) { Transient.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {

View File

@ -599,64 +599,7 @@ describe('basic-querying', function () {
}); });
}); });
}); });
}); });
describe('updateAll ', 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.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(0);
User.find({where: {name: 'John Smith'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
});
});
});
});
it('should update all instances without where', function (done) {
User.update({name: 'John Smith'}, function () {
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(0);
User.find({where: {name: 'John Smith'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(6);
done();
});
});
});
});
it('should ignore undefined values of data', function(done) {
User.update({name: 'John Lennon'}, {name: undefined,
email: 'johnl@b3atl3s.co.uk'}, function(err) {
should.not.exist(err);
User.find({where: {name: 'John Lennon'}}, function(err, data) {
should.not.exist(err);
data.length.should.equal(1);
data[0].email.should.equal('johnl@b3atl3s.co.uk');
done();
});
});
});
it('should coerce data', function (done) {
User.update({name: 'John Lennon'}, {birthday: 'invalidate'}, function (err) {
should.exist(err);
done();
});
});
});
}); });
function seed(done) { function seed(done) {

View File

@ -1,4 +1,5 @@
// This test written in mocha+should.js // This test written in mocha+should.js
var async = require('async');
var should = require('./init.js'); var should = require('./init.js');
var db, Person; var db, Person;
@ -982,6 +983,105 @@ describe('manipulation', function () {
e.should.be.eql(new Error('Invalid date: X')); e.should.be.eql(new Error('Invalid date: X'));
} }
}); });
});
describe('update/updateAll', function() {
beforeEach(function destroyFixtures(done) {
Person.destroyAll(done);
});
beforeEach(function createFixtures(done) {
Person.create([{
name: 'Brett Boe',
age: 19
}, {
name: 'Carla Coe',
age: 20
}, {
name: 'Donna Doe',
age: 21
}, {
name: 'Frank Foe',
age: 22
}, {
name: 'Grace Goe',
age: 23
}], done);
});
it('should be a function', function() {
Person.update.should.be.a.Function;
Person.updateAll.should.be.a.Function;
});
it('should not update instances that do not satisfy the where condition',
function(done) {
Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err,
results) {
should.not.exist(err);
results.count.should.equal(0);
Person.find({where: {name: 'Harry Hoe'}}, function(err, people) {
should.not.exist(err);
people.should.be.empty;
done();
});
});
});
it('should update instances that satisfy the where condition',
function(done) {
Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err,
results) {
should.not.exist(err);
results.count.should.equal(1);
Person.find({where: {age: 19}}, function(err, people) {
should.not.exist(err);
people.should.have.length(1);
people[0].name.should.equal('Harry Hoe');
done();
});
});
});
it('should update all instances when the where condition is not provided',
function(done) {
Person.update({name: 'Harry Hoe'}, function(err, results) {
should.not.exist(err);
results.count.should.equal(5);
Person.find({where: {name: 'Brett Boe'}}, function(err, people) {
should.not.exist(err);
people.should.be.empty;
Person.find({where: {name: 'Harry Hoe'}}, function(err, people) {
should.not.exist(err);
people.should.have.length(5);
done();
});
});
});
});
it('should ignore where conditions with undefined values',
function(done) {
Person.update({name: 'Brett Boe'}, {name: undefined, gender: 'male'},
function(err, results) {
should.not.exist(err);
results.count.should.equal(1);
Person.find({where: {name: 'Brett Boe'}}, function(err, people) {
should.not.exist(err);
people.should.have.length(1);
people[0].name.should.equal('Brett Boe');
done();
});
});
});
it('should not coerce invalid values provided in where conditions',
function(done) {
Person.update({name: 'Brett Boe'}, {dob: 'Carla Coe'}, function(err) {
should.exist(err);
err.message.should.equal('Invalid date: Carla Coe');
done();
});
});
}); });
}); });