Skip to content

Commit

Permalink
populate; allow setting single populated paths to docs
Browse files Browse the repository at this point in the history
relates to #570
  • Loading branch information
aheckmann committed Feb 27, 2013
1 parent b0c646e commit 3b48689
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ SchemaBuffer.prototype.checkRequired = function (value) {
*/

SchemaBuffer.prototype.cast = function (value, doc, init) {
if (SchemaType._isRef(this, value, init)) return value;
var populated = init || doc && doc.populated && !! doc.populated(this.path);
if (SchemaType._isRef(this, value, populated)) {
return value;
}

// documents with Buffer _ids
if (value && Buffer.isBuffer(value._id)) {
Expand Down
5 changes: 4 additions & 1 deletion lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ SchemaNumber.prototype.max = function (value, message) {
*/

SchemaNumber.prototype.cast = function (value, doc, init) {
if (SchemaType._isRef(this, value, init)) return value;
var populated = init || doc && doc.populated && !! doc.populated(this.path);
if (SchemaType._isRef(this, value, populated)) {
return value;
}

var val = value && value._id
? value._id // documents
Expand Down
9 changes: 6 additions & 3 deletions lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ ObjectId.prototype.checkRequired = function checkRequired (value) {
* Casts to ObjectId
*
* @param {Object} value
* @param {Object} scope
* @param {Object} doc
* @param {Boolean} init whether this is an initialization cast
* @api private
*/

ObjectId.prototype.cast = function (value, scope, init) {
if (SchemaType._isRef(this, value, init)) return value;
ObjectId.prototype.cast = function (value, doc, init) {
var populated = init || doc && doc.populated && !! doc.populated(this.path);
if (SchemaType._isRef(this, value, populated)) {
return value;
}

if (value === null) return value;

Expand Down
5 changes: 3 additions & 2 deletions lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ SchemaString.prototype.checkRequired = function checkRequired (value) {
* @api private
*/

SchemaString.prototype.cast = function (value, scope, init) {
if (SchemaType._isRef(this, value, init)) {
SchemaString.prototype.cast = function (value, doc, init) {
var populated = init || doc && doc.populated && !! doc.populated(this.path);
if (SchemaType._isRef(this, value, populated)) {
return value;
}

Expand Down
9 changes: 9 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2128,6 +2128,15 @@ describe('model: populate:', function(){
assert.equal(doc.fans[7], _id);
}

assert.equal(doc._creator.email, u1.email);

doc._creator = null;
assert.equal(null, doc._creator);

var creator = user('creator');
doc._creator = creator;
assert.equal(doc._creator, creator);

doc.save(function (err) {
assert.ifError(err);
B.findById(b1).exec(function (err, doc) {
Expand Down

0 comments on commit 3b48689

Please sign in to comment.