Merge pull request #470 from rudzon/master

Enhance updateAll to coerce the data per property definitions
This commit is contained in:
Raymond Feng 2015-02-24 08:28:39 -08:00
commit 6891da4535
2 changed files with 39 additions and 8 deletions

View File

@ -768,17 +768,28 @@ DataAccessObject._normalize = function (filter) {
}; };
function DateType(arg) { function DateType(arg) {
return new Date(arg); var d = new Date(arg);
if (isNaN(d.getTime())) {
throw new Error('Invalid date: ' + arg);
}
return d;
} }
function BooleanType(val) { function BooleanType(arg) {
if (val === 'true') { if (typeof arg === 'string') {
return true; switch (arg) {
} else if (val === 'false') { case 'true':
return false; case '1':
} else { return true;
return Boolean(val); case 'false':
case '0':
return false;
}
} }
if (arg == null) {
return null;
}
return Boolean(arg);
} }
function NumberType(val) { function NumberType(val) {
@ -1530,6 +1541,8 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
try { try {
where = removeUndefined(where); where = removeUndefined(where);
where = Model._coerce(where); where = Model._coerce(where);
data = removeUndefined(data);
data = Model._coerce(data);
} catch (err) { } catch (err) {
return process.nextTick(function () { return process.nextTick(function () {
cb(err); cb(err);

View File

@ -654,6 +654,24 @@ describe('basic-querying', function () {
}); });
}); });
it('should ignore undefined values of data', function (done) {
User.update({name: 'John Lennon'}, {name: undefined}, function (err) {
should.not.exist(err);
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
});
});
});
it('should coerce data', function (done) {
User.update({name: 'John Lennon'}, {birthday: 'invalidate'}, function (err) {
should.exist(err);
done();
});
});
}); });
}); });