Skip to content

Commit

Permalink
fixed; lean population
Browse files Browse the repository at this point in the history
should be lean() throughout all subdocs as well

closes #1382
  • Loading branch information
aheckmann committed Mar 12, 2013
1 parent 129bc78 commit c5527ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ Query.prototype.execFind = function (callback) {
: completeMany(model, docs, fields, self, null, promise);
}

var pop = utils.object.vals(self.options.populate);
var pop = preparePopulationOptions(self, options);
model.populate(docs, pop, function (err, docs) {
if (err) return promise.error(err);
return true === options.lean
Expand Down Expand Up @@ -1729,7 +1729,7 @@ Query.prototype.findOne = function (callback) {
: completeOne(model, doc, fields, self, null, promise);
}

var pop = utils.object.vals(self.options.populate);
var pop = preparePopulationOptions(self, options);
model.populate(doc, pop, function (err, doc) {
if (err) return promise.error(err);
return true === options.lean
Expand All @@ -1741,6 +1741,34 @@ Query.prototype.findOne = function (callback) {
return this;
}

/*!
* Prepare a set of path options for query population.
*
* @param {Query} query
* @param {Object} options
* @return {Array}
*/

function preparePopulationOptions (query, options) {
var pop = utils.object.vals(query.options.populate);

// lean options should trickle through all queries
if (options.lean) pop.forEach(makeLean);

return pop;
}

/*!
* Set each path query option to lean
*
* @param {Object} option
*/

function makeLean (option) {
option.options || (option.options = {});
option.options.lean = true;
}

/*!
* hydrates a document
*
Expand Down
5 changes: 5 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ describe('model: populate:', function(){
assert.ok(utils.isObject(post._creator));
assert.equal(post._creator.name, 'Guillermo');
assert.equal(post._creator.email, 'rauchg@gmail.com');
assert.equal('undefined', typeof post._creator.update);
done();
});
});
Expand Down Expand Up @@ -2041,13 +2042,17 @@ describe('model: populate:', function(){

assert.equal(blogposts[0].fans[0].name,'Fan 1');
assert.equal(blogposts[0].fans[0].email,'fan1@learnboost.com');
assert.equal('undefined', typeof blogposts[0].fans[0].update);
assert.equal(blogposts[0].fans[1].name,'Fan 2');
assert.equal(blogposts[0].fans[1].email,'fan2@learnboost.com');
assert.equal('undefined', typeof blogposts[0].fans[1].update);

assert.equal(blogposts[1].fans[0].name,'Fan 2');
assert.equal(blogposts[1].fans[0].email,'fan2@learnboost.com');
assert.equal('undefined', typeof blogposts[1].fans[0].update);
assert.equal(blogposts[1].fans[1].name,'Fan 1');
assert.equal(blogposts[1].fans[1].email,'fan1@learnboost.com');
assert.equal('undefined', typeof blogposts[1].fans[1].update);
done();
});
});
Expand Down

0 comments on commit c5527ba

Please sign in to comment.