diff --git a/lib/error/browserMissingSchema.js b/lib/error/browserMissingSchema.js index 3f271499d4d..608cfd983e4 100644 --- a/lib/error/browserMissingSchema.js +++ b/lib/error/browserMissingSchema.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class MissingSchemaError extends MongooseError { diff --git a/lib/error/divergentArray.js b/lib/error/divergentArray.js index 6bb527d0205..f266dbde449 100644 --- a/lib/error/divergentArray.js +++ b/lib/error/divergentArray.js @@ -5,7 +5,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class DivergentArrayError extends MongooseError { /** diff --git a/lib/error/eachAsyncMultiError.js b/lib/error/eachAsyncMultiError.js index 9c04020312b..b14156a09a3 100644 --- a/lib/error/eachAsyncMultiError.js +++ b/lib/error/eachAsyncMultiError.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); /** diff --git a/lib/error/invalidSchemaOption.js b/lib/error/invalidSchemaOption.js index 2ab1aa9497e..089dc6a03ef 100644 --- a/lib/error/invalidSchemaOption.js +++ b/lib/error/invalidSchemaOption.js @@ -5,7 +5,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class InvalidSchemaOptionError extends MongooseError { /** diff --git a/lib/error/missingSchema.js b/lib/error/missingSchema.js index 50c81054a90..2b3bf242526 100644 --- a/lib/error/missingSchema.js +++ b/lib/error/missingSchema.js @@ -5,7 +5,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class MissingSchemaError extends MongooseError { /** diff --git a/lib/error/notFound.js b/lib/error/notFound.js index e1064bb89d9..19a22f3a101 100644 --- a/lib/error/notFound.js +++ b/lib/error/notFound.js @@ -4,7 +4,7 @@ * Module dependencies. */ -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); const util = require('util'); class DocumentNotFoundError extends MongooseError { diff --git a/lib/error/objectExpected.js b/lib/error/objectExpected.js index 6506f60656a..9f7a8116618 100644 --- a/lib/error/objectExpected.js +++ b/lib/error/objectExpected.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class ObjectExpectedError extends MongooseError { diff --git a/lib/error/objectParameter.js b/lib/error/objectParameter.js index 295582e2484..b3f5b80849d 100644 --- a/lib/error/objectParameter.js +++ b/lib/error/objectParameter.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class ObjectParameterError extends MongooseError { /** diff --git a/lib/error/overwriteModel.js b/lib/error/overwriteModel.js index 1ff180b0498..8904e4e74b3 100644 --- a/lib/error/overwriteModel.js +++ b/lib/error/overwriteModel.js @@ -5,7 +5,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class OverwriteModelError extends MongooseError { diff --git a/lib/error/parallelSave.js b/lib/error/parallelSave.js index e0628576de7..57ac458238d 100644 --- a/lib/error/parallelSave.js +++ b/lib/error/parallelSave.js @@ -4,7 +4,7 @@ * Module dependencies. */ -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class ParallelSaveError extends MongooseError { /** diff --git a/lib/error/strict.js b/lib/error/strict.js index 393ca6e1fc7..6cf4cf91141 100644 --- a/lib/error/strict.js +++ b/lib/error/strict.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class StrictModeError extends MongooseError { diff --git a/lib/error/strictPopulate.js b/lib/error/strictPopulate.js index f7addfa5287..288799897bc 100644 --- a/lib/error/strictPopulate.js +++ b/lib/error/strictPopulate.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class StrictPopulateError extends MongooseError { /** diff --git a/lib/error/validator.js b/lib/error/validator.js index 4ca7316d7bf..f7ee2ef4761 100644 --- a/lib/error/validator.js +++ b/lib/error/validator.js @@ -4,7 +4,7 @@ 'use strict'; -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class ValidatorError extends MongooseError { diff --git a/lib/error/version.js b/lib/error/version.js index b357fb16ca3..85f2921a517 100644 --- a/lib/error/version.js +++ b/lib/error/version.js @@ -4,7 +4,7 @@ * Module dependencies. */ -const MongooseError = require('./'); +const MongooseError = require('./mongooseError'); class VersionError extends MongooseError { /** diff --git a/lib/index.js b/lib/index.js index 6247d6a4d5c..67534e9e793 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/lib/mongoose.js b/lib/mongoose.js index 915720b59f7..49392bfd70f 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -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