Permalink
Browse files

Now, null values retrieved from the database are hydrated as null val…

…ues in the document (before hydrated to undefined). With tests.
  • Loading branch information...
1 parent 6fa608b commit 2290833efb9805b4eda5e0e086e72f772ee3d995 @bnoguchi bnoguchi committed Mar 23, 2011
Showing with 43 additions and 14 deletions.
  1. +3 −1 lib/mongoose/document.js
  2. +4 −2 lib/mongoose/model.js
  3. +36 −11 test/model.test.js
View
4 lib/mongoose/document.js
@@ -118,7 +118,9 @@ Document.prototype.init = function (doc, fn) {
doc[i] = {};
init(obj[i], doc[i], path + '.');
} else {
- if (obj[i] !== null && obj[i] !== undefined) {
+ if (obj[i] === null) {
+ doc[i] = null;
+ } else if (obj[i] !== undefined) {
if (schema) {
self.try(function(){
doc[i] = schema.cast(obj[i], self);
View
6 lib/mongoose/model.js
@@ -103,8 +103,10 @@ Model.prototype.save = function (fn) {
, schema = data.schema
, atomics, val, obj;
- // a MongooseArray or MongooseNumber
- if (type._path && type.doAtomics) {
+ if (type === null) {
+ delta[data.path] = null;
+ } else if (type._path && type.doAtomics) {
+ // a MongooseArray or MongooseNumber
atomics = type._atomics;
for (var op in atomics) {
if (op === '$pushAll' || op === '$pullAll') {
View
47 test/model.test.js
@@ -234,16 +234,41 @@ module.exports = {
db.close();
},
-// 'test instantiating a model with a hash that maps to at least 1 undefined value': function () {
-// var db = start()
-// , BlogPost = db.model('BlogPost', collection);
-//
-// var post = new BlogPost({
-// title: undefined
-// });
-// should.strictEqual(undefined, post.title);
-// db.close();
-// },
+ 'saving a model with a null value should perpetuate that null value to the db': function () {
+ var db = start()
+ , BlogPost = db.model('BlogPost', collection);
+
+ var post = new BlogPost({
+ title: null
+ });
+ should.strictEqual(null, post.title);
+ post.save( function (err) {
+ should.strictEqual(err, null);
+ BlogPost.findById(post.id, function (err, found) {
+ db.close();
+ should.strictEqual(err, null);
+ should.strictEqual(found.title, null);
+ });
+ });
+ },
+
+ 'test instantiating a model with a hash that maps to at least 1 undefined value': function () {
+ var db = start()
+ , BlogPost = db.model('BlogPost', collection);
+
+ var post = new BlogPost({
+ title: undefined
+ });
+ should.strictEqual(undefined, post.title);
+ post.save( function (err) {
+ should.strictEqual(null, err);
+ BlogPost.findById(post.id, function (err, found) {
+ db.close();
+ should.strictEqual(err, null);
+ should.strictEqual(found.title, undefined);
+ });
+ });
+ },
'test a model structure when saved': function(){
var db = start()
@@ -738,7 +763,7 @@ module.exports = {
TestP.findOne({_id: f[0]._id}, function (err, found) {
should.strictEqual(err, null);
found.isNew.should.be.false;
- should.strictEqual(found.get('previous'), undefined);
+ should.strictEqual(found.get('previous'), null);
found.validate(function(err){
err.should.be.an.instanceof(MongooseError);

0 comments on commit 2290833

Please sign in to comment.