Skip to content

Commit

Permalink
Merge pull request #14682 from Automattic/vkarpov15/driver-plugins
Browse files Browse the repository at this point in the history
feat(mongoose): allow drivers to set global plugins
  • Loading branch information
vkarpov15 authored Jun 24, 2024
2 parents cd11105 + 69487ea commit 87c57e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
6 changes: 5 additions & 1 deletion test/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ 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();

const Test = m.model('Test', m.Schema({ answer: Number }));

const res = await Test.findOne();
assert.deepEqual(res.toObject(), { answer: 42 });

function foo() {}
});

it('multiple drivers (gh-12638)', async function() {
Expand Down

0 comments on commit 87c57e6

Please sign in to comment.