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
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
2 changes: 1 addition & 1 deletion lib/error/browserMissingSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


class MissingSchemaError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/divergentArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class DivergentArrayError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/eachAsyncMultiError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/invalidSchemaOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class InvalidSchemaOptionError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/missingSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class MissingSchemaError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/notFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const MongooseError = require('./');
const MongooseError = require('./mongooseError');
const util = require('util');

class DocumentNotFoundError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/objectExpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


class ObjectExpectedError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/objectParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class ObjectParameterError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/overwriteModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


class OverwriteModelError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/parallelSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class ParallelSaveError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


class StrictModeError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/strictPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class StrictPopulateError extends MongooseError {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/error/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');


class ValidatorError extends MongooseError {
Expand Down
2 changes: 1 addition & 1 deletion lib/error/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

class VersionError extends MongooseError {
/**
Expand Down
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down