From abd46961daa6159876ce20c2044e6ac18e2328b2 Mon Sep 17 00:00:00 2001 From: arlaneenalra Date: Tue, 4 Feb 2014 23:10:42 -0600 Subject: [PATCH 1/3] Use type converted data when writing back to database. This allows foreignKey types and other custom types to be converted into natvie values before writing them to the database. --- lib/dao.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 3a8c4a18..2aa948a8 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -847,6 +847,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb var inst = this; var Model = this.constructor var model = Model.modelName; + var typedData = {}; if (typeof data === 'function') { cb = data; @@ -873,9 +874,10 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb 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) { From 5252d0e8058f8654efa163ff4734f6a6f80c8445 Mon Sep 17 00:00:00 2001 From: arlaneenalra Date: Wed, 5 Feb 2014 22:21:27 -0600 Subject: [PATCH 2/3] Move new var into thunk. There really was no reason for it to be in the outer context. --- lib/dao.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 2aa948a8..8fd46204 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -847,7 +847,6 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb var inst = this; var Model = this.constructor var model = Model.modelName; - var typedData = {}; if (typeof data === 'function') { cb = data; @@ -871,6 +870,7 @@ 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]; From 99dff35de2ceedc948669e72a5225cab91513434 Mon Sep 17 00:00:00 2001 From: arlaneenalra Date: Fri, 7 Feb 2014 06:50:35 -0600 Subject: [PATCH 3/3] Add unit test for datatype handling in updateAttributes. --- test/datatype.test.js | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/datatype.test.js b/test/datatype.test.js index 7fb65b68..867a4c1d 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -5,6 +5,25 @@ var db, Model; describe('datatypes', function () { + // Used to emulate a custom id type + function DummyType(value) { + if (!(this instanceof DummyType) && + ( typeof value === 'string' || typeof value === 'object')) { + + return new DummyType(value); + } + + if (typeof value === 'string') { + this.value = value; + return; + } else if (typeof value === 'object') { + this.value = value.value; + return; + } + + return value; + }; + before(function (done) { db = getSchema(); Model = db.define('Model', { @@ -12,6 +31,7 @@ describe('datatypes', function () { date: Date, num: Number, bool: Boolean, + dummy: DummyType, list: {type: []}, }); db.automigrate(function () { @@ -63,4 +83,51 @@ 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, dummy: new DummyType('dummy') + }, 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'); + m.dummy.should.be.an.instanceOf(DummyType); + 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, dummy: 'NotADummy' + }, function (err, m) { + should.not.exist(err); + m.dummy.should.be.an.instanceOf(DummyType); + 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.dummy.should.be.a('object'); + done(); + }); + } + }); });