Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mongoose): allow drivers to set global plugins #14682

Merged
merged 1 commit into from
Jun 24, 2024
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
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
Loading