Skip to content

Commit

Permalink
BREAKING CHANGE: if model is registered on a non-default connection, …
Browse files Browse the repository at this point in the history
…don't register it on mongoose global

Re: #5758

Tests still failing, pushing to help debug using travis
  • Loading branch information
vkarpov15 committed Apr 12, 2020
1 parent 70b4096 commit 6865fea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 50 deletions.
4 changes: 0 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,6 @@ Connection.prototype.model = function(name, schema, collection) {
return sub;
}

// lookup model in mongoose module
model = this.base.models[name];

if (!model) {
throw new MongooseError.MissingSchemaError(name);
}
Expand Down Expand Up @@ -1142,7 +1139,6 @@ Connection.prototype.deleteModel = function(name) {
const collectionName = model.collection.name;
delete this.models[name];
delete this.collections[collectionName];
delete this.base.modelSchemas[name];
} else if (name instanceof RegExp) {
const pattern = name;
const names = this.modelNames();
Expand Down
58 changes: 12 additions & 46 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ require('./helpers/printJestWarning');
function Mongoose(options) {
this.connections = [];
this.models = {};
this.modelSchemas = {};
// default global options
this.options = Object.assign({
pluralization: true
Expand Down Expand Up @@ -493,55 +492,22 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
options = {};
}

// look up schema for the collection.
if (!_mongoose.modelSchemas[name]) {
if (schema) {
// cache it so we only apply plugins once
_mongoose.modelSchemas[name] = schema;
} else {
throw new mongoose.Error.MissingSchemaError(name);
if (_mongoose.models.hasOwnProperty(name)) {
if (schema != null &&
schema.instanceOfSchema &&
schema !== _mongoose.models[name].schema) {
throw new _mongoose.Error.OverwriteModelError(name);
}
return _mongoose.models[name];
}

const originalSchema = schema;
if (schema) {
if (_mongoose.get('cloneSchemas')) {
schema = schema.clone();
}
_mongoose._applyPlugins(schema);
}

let sub;

// connection.model() may be passing a different schema for
// an existing model name. in this case don't read from cache.
if (_mongoose.models[name] && options.cache !== false) {
if (originalSchema &&
originalSchema.instanceOfSchema &&
originalSchema !== _mongoose.models[name].schema) {
throw new _mongoose.Error.OverwriteModelError(name);
}

if (collection && collection !== _mongoose.models[name].collection.name) {
// subclass current model with alternate collection
model = _mongoose.models[name];
schema = model.prototype.schema;
sub = model.__subclass(_mongoose.connection, schema, collection);
// do not cache the sub model
return sub;
}

return _mongoose.models[name];
}

// ensure a schema exists
if (!schema) {
schema = this.modelSchemas[name];
if (!schema) {
throw new mongoose.Error.MissingSchemaError(name);
}
}

// Apply relevant "global" options to the schema
if (!('pluralization' in schema.options)) {
schema.options.pluralization = _mongoose.options.pluralization;
Expand All @@ -554,18 +520,17 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {

const connection = options.connection || _mongoose.connection;
model = _mongoose.Model.compile(model || name, schema, collection, connection, _mongoose);
if (connection === _mongoose.connection) {
connection.models[name] = model;
_mongoose.models[name] = model;
}

if (!skipInit) {
// Errors handled internally, so safe to ignore error
model.init(function $modelInitNoop() {});
}

if (options.cache === false) {
return model;
}

_mongoose.models[name] = model;
return _mongoose.models[name];
return model;
};

/**
Expand Down Expand Up @@ -596,6 +561,7 @@ Mongoose.prototype.deleteModel = function(name) {
const _mongoose = this instanceof Mongoose ? this : mongoose;

_mongoose.connection.deleteModel(name);
delete _mongoose.models[name];
return _mongoose;
};

Expand Down

0 comments on commit 6865fea

Please sign in to comment.