Skip to content

Commit

Permalink
Merge pull request #14577 from Automattic/vkarpov15/gh-12335
Browse files Browse the repository at this point in the history
handle initially null driver when instantiating Mongoose for Rollup support
  • Loading branch information
vkarpov15 committed May 9, 2024
2 parents db29c08 + 19e744a commit 55564e1
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 17 deletions.
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

0 comments on commit 55564e1

Please sign in to comment.