diff --git a/lib/mongoose.js b/lib/mongoose.js index cfac2331ced..d487190e4b3 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -171,6 +171,14 @@ Mongoose.prototype.setDriver = function setDriver(driver) { } _mongoose.__driver = driver; + if (Array.isArray(driver.plugins)) { + for (const plugin of driver.plugins) { + if (typeof plugin === 'function') { + _mongoose.plugin(plugin); + } + } + } + const Connection = driver.Connection; const oldDefaultConnection = _mongoose.connections[0]; _mongoose.connections = [new Connection(_mongoose)]; diff --git a/test/driver.test.js b/test/driver.test.js index 9dffe1c520e..e5005ad13f8 100644 --- a/test/driver.test.js +++ b/test/driver.test.js @@ -34,10 +34,12 @@ describe('driver', function() { } const driver = { Collection, - Connection + Connection, + plugins: [foo] }; m.setDriver(driver); + assert.deepStrictEqual(m.plugins.slice(mongoose.plugins.length), [[foo, undefined]]); await m.connect(); @@ -45,6 +47,8 @@ describe('driver', function() { const res = await Test.findOne(); assert.deepEqual(res.toObject(), { answer: 42 }); + + function foo() {} }); it('multiple drivers (gh-12638)', async function() {