fix: coerce primitive properties on update

This commit is contained in:
biniam 2019-04-16 10:51:44 -04:00
parent 1534392c62
commit 48af738ec4
3 changed files with 36 additions and 1 deletions

View File

@ -2301,6 +2301,7 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
if (!(data instanceof Model)) {
try {
inst = new Model(data, {applyDefaultValues: false});
ctx.data = inst.toObject(true);
} catch (err) {
return cb(err);
}

View File

@ -1055,6 +1055,40 @@ describe('basic-querying', function() {
});
});
describe('updateAll', function() {
it('coerces primitive datatypes on update', async () => {
const numAndDateModel = db.define('numAndDateModel', {
dateProp: Date,
dateArray: [Date],
numProp: Number,
numArray: [Number],
});
const createDate = new Date('2019-02-21T12:00:00').toISOString();
const createData = {
dateProp: createDate,
dateArray: [createDate, createDate],
numProp: '1',
numArray: ['1', '2'],
};
const updateDate = new Date('2019-04-15T12:00:00').toISOString();
const updateData = {
dateProp: updateDate,
dateArray: [updateDate, updateDate],
numProp: '3',
numArray: ['3', '4'],
};
const created = await numAndDateModel.create(createData);
const updated = await numAndDateModel.updateAll({id: created.id}, updateData);
const found = await numAndDateModel.findById(created.id);
found.dateProp.should.deepEqual(new Date(updateDate));
found.dateArray[0].should.deepEqual(new Date(updateDate));
found.dateArray[1].should.deepEqual(new Date(updateDate));
found.numProp.should.equal(3);
found.numArray[0].should.equal(3);
found.numArray[1].should.equal(4);
});
});
context('regexp operator', function() {
const invalidDataTypes = [0, true, {}, [], Function, null];

View File

@ -2366,7 +2366,7 @@ describe('manipulation', function() {
it('should not coerce invalid values provided in where conditions', function(done) {
Person.update({name: 'Brett Boe'}, {dob: 'notadate'}, function(err) {
should.exist(err);
err.message.should.equal('Invalid date: notadate');
err.message.should.equal('Invalid date: Invalid Date');
done();
});
});