Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.
Merged
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
29 changes: 17 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ module.exports = function(schema) {
eachPathRecursive(schema, function(pathname, schemaType) {
if (schemaType.options && schemaType.options.autopopulate) {
var option = schemaType.options.autopopulate;
pathsToPopulate.push({ path: pathname, autopopulate: option });
pathsToPopulate.push({
options: schemaType.options.ref ? {
path: pathname,
model: schemaType.options.ref
} : { path: pathname },
autopopulate: option
});
}
});

var autopopulateHandler = function() {
var numPaths = pathsToPopulate.length;
for (var i = 0; i < numPaths; ++i) {
processOption.call(this,
pathsToPopulate[i].autopopulate, pathsToPopulate[i].path);
pathsToPopulate[i].autopopulate, pathsToPopulate[i].options);
}
};

Expand All @@ -22,35 +28,34 @@ module.exports = function(schema) {
pre('findOne', autopopulateHandler);
};

function processOption(value, path) {
function processOption(value, options) {
switch (typeof value) {
case 'function':
handleFunction.call(this, value, path);
handleFunction.call(this, value, options);
break;
case 'object':
handleObject.call(this, value, path);
handleObject.call(this, value, options);
break;
default:
handlePrimitive.call(this, value, path);
handlePrimitive.call(this, value, options);
break;
}
}

function handlePrimitive(value, path) {
function handlePrimitive(value, options) {
if (value) {
this.populate(path);
this.populate(options);
}
}

function handleObject(value, path) {
var optionsToUse = { path: path };
function handleObject(value, optionsToUse) {
mergeOptions(optionsToUse, value);
this.populate(optionsToUse);
}

function handleFunction(fn, path) {
function handleFunction(fn, options) {
var val = fn.call(this);
processOption.call(this, val, path);
processOption.call(this, val, options);
}

function mergeOptions(destination, source) {
Expand Down
8 changes: 4 additions & 4 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('mongoose-autopopulate:unit', function() {

schemaStub.pre.calls[0].handler.call(queryStub);
assert.equal(1, queryStub.populate.calls.length);
assert.equal('test', queryStub.populate.calls[0]);
assert.equal('test', queryStub.populate.calls[0].path);
});

it('ignores when paths autopopulate option is falsy', function() {
Expand All @@ -40,7 +40,7 @@ describe('mongoose-autopopulate:unit', function() {

schemaStub.pre.calls[0].handler.call(queryStub);
assert.equal(1, queryStub.populate.calls.length);
assert.equal('test', queryStub.populate.calls[0]);
assert.equal('test', queryStub.populate.calls[0].path);
});

it('merges options when autopopulate option is object', function() {
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('mongoose-autopopulate:unit', function() {

schemaStub.pre.calls[0].handler.call(queryStub);
assert.equal(1, queryStub.populate.calls.length);
assert.deepEqual('test', queryStub.populate.calls[0]);
assert.deepEqual('test', queryStub.populate.calls[0].path);
});

it('augments populate options when autopopulate returns object', function() {
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('mongoose-autopopulate:unit', function() {
assert.equal(queryStub.populate.calls.length, 0);

schema.pre.calls[0].handler.call(queryStub);
assert.deepEqual(queryStub.populate.calls[0],
assert.deepEqual(queryStub.populate.calls[0].path,
'nested.test');
});
});
Expand Down