Skip to content

Commit

Permalink
fixed; default application of selected fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Apr 28, 2012
1 parent 1cf4712 commit d2875c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/document.js
Expand Up @@ -152,9 +152,13 @@ Document.prototype.buildDoc = function (fields) {
self._activePaths.default(p);
}

} else {
// do nothing. only the fields specified in
// the query will be populated
} else if (p in fields) {
// selected field
def = type.getDefault(self);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self._activePaths.default(p);
}
}
} else {
def = type.getDefault(self);
Expand Down
28 changes: 25 additions & 3 deletions test/model.querying.test.js
Expand Up @@ -612,10 +612,10 @@ module.exports = {
BlogPostB.create({title: 'subset 1'}, function (err, created) {
should.strictEqual(err, null);
BlogPostB.findOne({title: 'subset 1'}, {title: 1, _id: 0}, function (err, found) {
db.close();
should.strictEqual(err, null);
should.strictEqual(undefined, found._id);
found.title.should.equal('subset 1');
db.close();
});
});
},
Expand All @@ -626,13 +626,13 @@ module.exports = {
BlogPostB.create({title: 'subset 1', author: 'me'}, function (err, created) {
should.strictEqual(err, null);
BlogPostB.find({title: 'subset 1'}, {title: 1, _id: 0}, function (err, found) {
db.close();
should.strictEqual(err, null);
should.strictEqual(undefined, found[0]._id);
found[0].title.should.equal('subset 1');
should.strictEqual(undefined, found[0].def);
should.strictEqual(undefined, found[0].author);
should.strictEqual(false, Array.isArray(found[0].comments));
db.close();
});
});
},
Expand Down Expand Up @@ -661,7 +661,6 @@ module.exports = {
});
},


'exluded fields should be undefined': function () {
var db = start()
, BlogPostB = db.model('BlogPostB', collection)
Expand Down Expand Up @@ -709,6 +708,29 @@ module.exports = {
});
},

// gh-870
'included fields should have defaults applied when no value exists in db': function () {
var db = start()
, BlogPostB = db.model('BlogPostB', collection)
, id = new DocumentObjectId

BlogPostB.collection.insert(
{ _id: id, title: 'issue 870'}, function (err) {
should.strictEqual(err, null);

BlogPostB.findById(id, 'def comments', function (err, found) {
db.close();
should.strictEqual(err, null);
found._id.should.eql(id);
should.strictEqual(undefined, found.title);
should.strictEqual('kandinsky', found.def);
should.strictEqual(undefined, found.author);
should.strictEqual(true, Array.isArray(found.comments));
found.comments.length.should.equal(0);
});
});
},

'test for find where partial initialization': function () {
var db = start()
, BlogPostB = db.model('BlogPostB', collection)
Expand Down

0 comments on commit d2875c2

Please sign in to comment.