From a7bd64b204ec6d52aebcfc0968a600b6fd615319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Thu, 11 Dec 2014 01:06:56 +0100 Subject: [PATCH 1/4] Allow undefined values, instance.hasOwnProperty --- lib/Instance.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Instance.js b/lib/Instance.js index 9670ddc4..43b9357c 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -473,7 +473,7 @@ function Instance(Model, opts) { } var addInstanceProperty = function (key) { - var defaultValue = null; + var defaultValue = undefined; var prop = Model.allProperties[key]; // This code was first added, and then commented out in a later commit. @@ -711,7 +711,7 @@ function Instance(Model, opts) { if (!asc.reversed && !asc.extension) { for (k in asc.field) { - if (!opts.data.hasOwnProperty(k)) { + if (!instance.hasOwnProperty(k)) { addInstanceProperty(k); } } From c012627ea1ef32c5067e0c68fe7673b075ef5a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Thu, 11 Dec 2014 02:56:18 +0100 Subject: [PATCH 2/4] Check instance.hasOwnProperty instead of data's --- lib/Instance.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Instance.js b/lib/Instance.js index 43b9357c..2f5a1131 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -138,7 +138,7 @@ function Instance(Model, opts) { var getInstanceData = function () { var data = {}, prop; for (var k in opts.data) { - if (!opts.data.hasOwnProperty(k)) continue; + if (!instance.hasOwnProperty(k)) continue; prop = Model.allProperties[k]; if (prop) { @@ -339,7 +339,7 @@ function Instance(Model, opts) { var conditions = {}; for (var i = 0; i < opts.extrachanges.length; i++) { - if (!opts.data.hasOwnProperty(opts.extrachanges[i])) continue; + if (!instance.hasOwnProperty(opts.extrachanges[i])) continue; if (opts.extra[opts.extrachanges[i]]) { data[opts.extrachanges[i]] = opts.data[opts.extrachanges[i]]; @@ -600,7 +600,7 @@ function Instance(Model, opts) { } for (var k in data) { - if (data.hasOwnProperty(k)) { + if (instance.hasOwnProperty(k)) { this[k] = data[k]; } } From fc9901fd1eb1669fb6878f8c94ff2cde23e14548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Thu, 11 Dec 2014 02:57:02 +0100 Subject: [PATCH 3/4] Spaces to tabs and minor refactoring --- lib/Instance.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Instance.js b/lib/Instance.js index 2f5a1131..9997c3fc 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -40,7 +40,7 @@ function Instance(Model, opts) { var pending = [], errors = [], required; Hook.wait(instance, opts.hooks.beforeValidation, function (err) { - var k, i; + var k, i; if (err) { return saveError(cb, err); } @@ -136,26 +136,26 @@ function Instance(Model, opts) { cb(); }; var getInstanceData = function () { - var data = {}, prop; + var data = {}, prop, val; for (var k in opts.data) { if (!instance.hasOwnProperty(k)) continue; prop = Model.allProperties[k]; + val = opts.data[k]; if (prop) { - if (opts.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) { + if (val == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) { continue; } if (opts.driver.propertyToValue) { - data[k] = opts.driver.propertyToValue(opts.data[k], prop); + data[k] = opts.driver.propertyToValue(val, prop); } else { - data[k] = opts.data[k]; + data[k] = val; } } else { - data[k] = opts.data[k]; + data[k] = val; } } - return data; }; var waitHooks = function (hooks, next) { From 7dde240182293c8637eaee4c20f85947c2c5e69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Thu, 11 Dec 2014 02:57:32 +0100 Subject: [PATCH 4/4] Fix tests, need sql-query updated to PR #37 --- test/integration/model-find-chain.js | 16 ++++++++-------- test/integration/property-lazyload.js | 2 +- test/integration/validation.js | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/integration/model-find-chain.js b/test/integration/model-find-chain.js index 7a0083c9..596d6ca2 100644 --- a/test/integration/model-find-chain.js +++ b/test/integration/model-find-chain.js @@ -189,26 +189,26 @@ describe("Model.find() chaining", function() { describe("only", function () { before(setup()); - it("('property', ...) should return only those properties, others null", function (done) { + it("('property', ...) should return only those properties, others undefined", function (done) { Person.find().only("age", "surname").order("-age").run(function (err, instances) { should.equal(err, null); instances.should.have.property("length", 3); instances[0].should.have.property("age"); instances[0].should.have.property("surname", "Doe"); - instances[0].should.have.property("name", null); + instances[0].should.not.have.property("name"); return done(); }); }); // This works if cache is disabled. I suspect a cache bug. - xit("(['property', ...]) should return only those properties, others null", function (done) { + xit("(['property', ...]) should return only those properties, others undefined", function (done) { Person.find().only([ "age", "surname" ]).order("-age").run(function (err, instances) { should.equal(err, null); instances.should.have.property("length", 3); instances[0].should.have.property("age"); instances[0].should.have.property("surname", "Doe"); - instances[0].should.have.property("name", null); + instances[0].should.not.have.property("name"); return done(); }); @@ -226,8 +226,8 @@ describe("Model.find() chaining", function() { should.exist(instances[0].id); } should.exist(instances[0].friend_id); - instances[0].should.have.property("age", null); - instances[0].should.have.property("surname", null); + instances[0].should.not.have.property("age"); + instances[0].should.not.have.property("surname"); instances[0].should.have.property("name", "Jane"); return done(); @@ -238,8 +238,8 @@ describe("Model.find() chaining", function() { Person.find().omit(["age", "surname"]).order("-age").run(function (err, instances) { should.equal(err, null); instances.should.have.property("length", 3); - instances[0].should.have.property("age", null); - instances[0].should.have.property("surname", null); + instances[0].should.not.have.property("age"); + instances[0].should.not.have.property("surname"); instances[0].should.have.property("name", "Jane"); return done(); diff --git a/test/integration/property-lazyload.js b/test/integration/property-lazyload.js index da6df0c8..7d7381dd 100644 --- a/test/integration/property-lazyload.js +++ b/test/integration/property-lazyload.js @@ -48,7 +48,7 @@ describe("LazyLoad properties", function() { John.should.be.a("object"); John.should.have.property("name", "John Doe"); - John.should.have.property("photo", null); + John.should.not.have.property("photo"); return done(); }); diff --git a/test/integration/validation.js b/test/integration/validation.js index 8e5edb18..acd02081 100644 --- a/test/integration/validation.js +++ b/test/integration/validation.js @@ -277,7 +277,7 @@ describe("Validations", function() { describe("properties.required = false", function() { before(setup(false, false)); - it("should save when properties are null", function(done) { + it("should save when properties are undefined", function(done) { var john = new Person(); john.save(function (err) { @@ -375,11 +375,11 @@ describe("Validations", function() { // `type` is a non enumerable undocumented property of `Error` in V8. should.deepEqual(err[0], _.extend(new Error(),{ - property: 'name', value: null, msg: 'required' + property: 'name', value: undefined, msg: 'required' })); should.deepEqual(err[1], _.extend(new Error(),{ - property: 'name', value: null, msg: 'undefined' + property: 'name', value: undefined, msg: 'undefined' })); should.deepEqual(err[2], _.extend(new Error(),{