From c50f43bc1b6ecf08a8fd1ce03a02d03d3979e7cf Mon Sep 17 00:00:00 2001 From: Joshua Kifer Date: Wed, 2 Apr 2014 17:00:56 -0700 Subject: [PATCH 1/3] Allow setInstanceProperty to accept undefined and return true in order to facilitate https://github.com/dresende/node-sql-query/commit/8ecf962eeeb4aa244ec1159ca75d603ce99212ba --- lib/Instance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Instance.js b/lib/Instance.js index 1e6dc813..4c27e488 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -427,7 +427,7 @@ function Instance(Model, opts) { if ('valueToProperty' in opts.driver) { value = opts.driver.valueToProperty(value, prop); } - if (opts.data[key] !== value) { + if (opts.data[key] !== value || value === undefined) { opts.data[key] = value; return true; } From 6910216b5d835929fd8870d29db6e9b5162b3f8c Mon Sep 17 00:00:00 2001 From: Arek W Date: Sun, 20 Apr 2014 08:59:21 +1000 Subject: [PATCH 2/3] Add failing test case --- test/integration/model-create.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/integration/model-create.js b/test/integration/model-create.js index 99ca8659..5f99c3c9 100644 --- a/test/integration/model-create.js +++ b/test/integration/model-create.js @@ -13,7 +13,8 @@ describe("Model.create()", function() { name : String }); Pet = db.define("pet", { - name : { type: "text", defaultValue: "Mutt" } + name : { type: "text", defaultValue: "Mutt" }, + age : { type: 'number' } }); Person.hasMany("pets", Pet); @@ -123,5 +124,29 @@ describe("Model.create()", function() { return done(); }); }); + + it("should not use defaultValue when updating", function (done) { + Pet.create({name: "Ruffy"}, function (err, pet) { + should.equal(err, null); + should.equal(pet.name, "Ruffy"); + + // simulated data from a user + var params = { age: 4 }; + + pet.age = params.age; + pet.name = params.name; // we expected a name, but it's undefined + + pet.save(function (err) { + should.not.exist(err); + + Pet.get(pet.id, function (err, pet) { + should.not.exist(err); + should.strictEqual(pet.name, null); + + done(); + }) + }); + }) + }); }); }); From 6940e7a7ef7085fb81ebd16a0619b8af86113c29 Mon Sep 17 00:00:00 2001 From: Arek W Date: Sun, 20 Apr 2014 09:20:49 +1000 Subject: [PATCH 3/3] Add default returned test #485 --- test/integration/model-create.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/integration/model-create.js b/test/integration/model-create.js index 5f99c3c9..4d36426a 100644 --- a/test/integration/model-create.js +++ b/test/integration/model-create.js @@ -6,6 +6,7 @@ describe("Model.create()", function() { var db = null; var Pet = null; var Person = null; + var Pen = null; var setup = function () { return function (done) { @@ -18,7 +19,11 @@ describe("Model.create()", function() { }); Person.hasMany("pets", Pet); - return helper.dropSync([ Person, Pet ], done); + Pen = db.define("pen", { + colour : { type: "text" } + }); + + return helper.dropSync([ Person, Pet, Pen ], done); }; }; @@ -148,5 +153,22 @@ describe("Model.create()", function() { }); }) }); + + it("should return the default value set in the databse", function (done) { + db.driver.execQuery( + "ALTER TABLE ?? ALTER COLUMN ?? SET DEFAULT ?", [Pen.table, 'colour', 'blue'], + function (err) { + should.not.exist(err); + + Pen.create({colour: undefined}, function (err, pen) { + should.not.exist(err); + + should.equal(pen.colour, 'blue'); + + done(); + }); + } + ); + }); }); });