diff --git a/lib/dao.js b/lib/dao.js index bfcecb41..eaaab854 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -870,12 +870,14 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb } else { inst.trigger('save', function (saveDone) { inst.trigger('update', function (done) { + var typedData = {}; for (var key in data) { inst[key] = data[key]; + typedData[key] = inst.__data[key]; } - inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(data), function (err) { + inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(typedData), function (err) { if (!err) { // update $was attrs for (var key in data) { diff --git a/package.json b/package.json index 2de17c61..7b28d083 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "1.3.1", + "version": "1.3.2", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop", diff --git a/test/datatype.test.js b/test/datatype.test.js index 7fb65b68..4db0d506 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -12,7 +12,7 @@ describe('datatypes', function () { date: Date, num: Number, bool: Boolean, - list: {type: []}, + list: {type: [String]}, }); db.automigrate(function () { Model.destroyAll(done); @@ -63,4 +63,49 @@ describe('datatypes', function () { }); + it('should respect data types when updating attributes', function (done) { + var d = new Date, id; + + Model.create({ + str: 'hello', date: d, num: '3', bool: 1}, function(err, m) { + should.not.exist(err); + should.exist(m && m.id); + + // sanity check initial types + m.str.should.be.a('string'); + m.num.should.be.a('number'); + m.bool.should.be.a('boolean'); + id = m.id; + testDataInDB(function () { + testUpdate(function() { + testDataInDB(done); + }); + }); + }); + + function testUpdate(done) { + Model.findById(id, function(err, m) { + should.not.exist(err); + + // update using updateAttributes + m.updateAttributes({ + id: id, num: '10' + }, function (err, m) { + should.not.exist(err); + m.num.should.be.a('number'); + done(); + }); + }); + } + + function testDataInDB(done) { + + // verify that the value stored in the db is still an object + db.connector.find(Model.modelName, id, function (err, data) { + should.exist(data); + data.num.should.be.a('number'); + done(); + }); + } + }); });