Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 (!opts.data.hasOwnProperty(k)) continue;
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) {
Expand Down Expand Up @@ -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]];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/integration/model-find-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/integration/property-lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
6 changes: 3 additions & 3 deletions test/integration/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),{
Expand Down