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

handle initially null driver when instantiating Mongoose for Rollup support #14577

Merged
merged 2 commits into from
May 9, 2024
Merged
Changes from 1 commit
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
Next Next commit
fix(mongoose): handle initially null driver when instantiating Mongoo…
…se for rollup

Fix #12335
vkarpov15 committed May 7, 2024
commit a928cee0f105a530b7bb3c30c97a0be669ce232b
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -4,10 +4,14 @@
* Module dependencies.
*/

require('./driver').set(require('./drivers/node-mongodb-native'));
const mongodbDriver = require('./drivers/node-mongodb-native');

require('./driver').set(mongodbDriver);

const mongoose = require('./mongoose');

mongoose.setDriver(mongodbDriver);

mongoose.Mongoose.prototype.mongo = require('mongodb');

module.exports = mongoose;
7 changes: 5 additions & 2 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
@@ -68,8 +68,8 @@ function Mongoose(options) {
autoCreate: true,
autoSearchIndex: false
}, options);
const createInitialConnection = utils.getOption('createInitialConnection', this.options);
if (createInitialConnection == null || createInitialConnection) {
const createInitialConnection = utils.getOption('createInitialConnection', this.options) ?? true;
if (createInitialConnection && this.__driver != null) {
const conn = this.createConnection(); // default connection
conn.models = this.models;
}
@@ -169,6 +169,9 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
const oldDefaultConnection = _mongoose.connections[0];
_mongoose.connections = [new Connection(_mongoose)];
_mongoose.connections[0].models = _mongoose.models;
if (oldDefaultConnection == null) {
return _mongoose;
}

// Update all models that pointed to the old default connection to
// the new default connection, including collections