Skip to content
Open
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
45 changes: 32 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
module.exports = function(schema) {
const pathsToPopulate = getPathsToPopulate(schema);

const autopopulateHandler = function(filter) {
if (this._mongooseOptions &&
this._mongooseOptions.lean &&
// If lean and user didn't explicitly do `lean({ autopulate: true })`,
// skip it. See gh-27, gh-14, gh-48
!this._mongooseOptions.lean.autopopulate) {
return;
}

const options = this.options || {};
function _autopopulateHandler(options, filter) {
if (options.autopopulate === false) {
return;
return [];
}

if (options.autopopulate && options.autopopulate.maxDepth) {
Expand All @@ -24,9 +15,11 @@ module.exports = function(schema) {
const depth = options._depth != null ? options._depth : 0;

if (options.maxDepth > 0 && depth >= options.maxDepth) {
return;
return [];
}

const toPopulate = [];

const numPaths = pathsToPopulate.length;
for (let i = 0; i < numPaths; ++i) {
pathsToPopulate[i].options = pathsToPopulate[i].options || {};
Expand All @@ -42,9 +35,27 @@ module.exports = function(schema) {
const optionsToUse = processOption.call(this,
pathsToPopulate[i].autopopulate, pathsToPopulate[i].options);
if (optionsToUse) {
this.populate(optionsToUse);
toPopulate.push(optionsToUse);
}
}

return toPopulate;
}

const autopopulateHandler = function(filter) {
if (this._mongooseOptions &&
this._mongooseOptions.lean &&
// If lean and user didn't explicitly do `lean({ autopulate: true })`,
// skip it. See gh-27, gh-14, gh-48
!this._mongooseOptions.lean.autopopulate) {
return;
}

const options = this.options || {};
const toPopulate = _autopopulateHandler(options, filter);
for (const pop of toPopulate) {
this.populate(pop);
}
};

schema.
Expand All @@ -68,6 +79,14 @@ module.exports = function(schema) {
});

return this.execPopulate();
}).
pre('insertMany', function(next, docs, options) {
options = options || {};
const toPopulate = _autopopulateHandler(options);
if (toPopulate.length > 0) {
options.populate = toPopulate;
}
next();
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"nyc": "11.7.3"
},
"peerDependencies": {
"mongoose": "4.x || 5.x"
"mongoose": ">= 5.11.10"
},
"author": "Valeri Karpov <[email protected]>",
"license": "Apache 2.0",
Expand Down
28 changes: 28 additions & 0 deletions test/bugs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,32 @@ describe('bug fixes', function() {
assert.deepEqual(doc.toObject().state, []);
});
});

it('supports insertMany (gh-79)', function() {
const User = db.model('User', Schema({ name: String }));
const GameSchema = new Schema({
players: [{
type: 'ObjectId',
ref: 'User',
autopopulate: true
}]
});
GameSchema.plugin(autopopulate);
const Game = db.model('Game', GameSchema);

return co(function*() {
const player = yield User.create({ name: 'test' });
let docs = yield Game.insertMany([{ players: [player._id] }], {});

assert.equal(docs.length, 1);
assert.equal(docs[0].players.length, 1);
assert.equal(docs[0].players[0].name, 'test');

docs = yield Game.insertMany([{ players: [player._id] }], { autopopulate: false });

assert.equal(docs.length, 1);
assert.equal(docs[0].players.length, 1);
assert.equal(docs[0].players[0].toHexString(), player._id.toHexString());
});
});
});