From ebe7cbdb9f08fdad41b56376d45b59e318d78496 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 6 Jan 2021 15:38:31 -0500 Subject: [PATCH] feat: support `insertMany()` Still has an issue where we can't modify `options` if user doesn't pass options, so we'll need to figure out how to fix that. Re: #79 --- index.js | 45 ++++++++++++++++++++++++++++++++------------- package.json | 2 +- test/bugs.test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a98714f..ba051c3 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 || {}; @@ -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. @@ -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(); }); }; diff --git a/package.json b/package.json index 40b87c0..dd3b296 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "nyc": "11.7.3" }, "peerDependencies": { - "mongoose": "4.x || 5.x" + "mongoose": ">= 5.11.10" }, "author": "Valeri Karpov ", "license": "Apache 2.0", diff --git a/test/bugs.test.js b/test/bugs.test.js index ce1f3da..b927e7c 100644 --- a/test/bugs.test.js +++ b/test/bugs.test.js @@ -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()); + }); + }); });