Skip to content

Commit

Permalink
BREAKING CHANGE: remove skipInit parameter to mongoose.model()
Browse files Browse the repository at this point in the history
Fix #4625
  • Loading branch information
vkarpov15 committed Apr 26, 2020
1 parent ba7acd0 commit 1e18e5d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
26 changes: 6 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,11 @@ Mongoose.prototype.pluralize = function(fn) {
* @param {String|Function} name model name or class extending Model
* @param {Schema} [schema] the schema to use.
* @param {String} [collection] name (optional, inferred from model name)
* @param {Boolean} [skipInit] whether to skip initialization (defaults to false)
* @return {Model} The model associated with `name`. Mongoose will create the model if it doesn't already exist.
* @api public
*/

Mongoose.prototype.model = function(name, schema, collection, skipInit) {
Mongoose.prototype.model = function(name, schema, collection, options) {
const _mongoose = this instanceof Mongoose ? this : mongoose;

if (typeof schema === 'string') {
Expand All @@ -468,19 +467,8 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
'schema or a POJO');
}

if (typeof collection === 'boolean') {
skipInit = collection;
collection = null;
}

// handle internal options from connection.model()
let options;
if (skipInit && utils.isObject(skipInit)) {
options = skipInit;
skipInit = true;
} else {
options = {};
}
options = options || {};

if (_mongoose.models.hasOwnProperty(name)) {
if (schema != null &&
Expand All @@ -502,7 +490,7 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
throw new _mongoose.Error.MissingSchemaError(name);
}

const model = _mongoose._model(name, schema, collection, options, skipInit);
const model = _mongoose._model(name, schema, collection, options);

_mongoose.connection.models[name] = model;
_mongoose.models[name] = model;
Expand All @@ -514,7 +502,7 @@ Mongoose.prototype.model = function(name, schema, collection, skipInit) {
* ignore
*/

Mongoose.prototype._model = function(name, schema, collection, options, skipInit) {
Mongoose.prototype._model = function(name, schema, collection, options) {
const _mongoose = this instanceof Mongoose ? this : mongoose;

let model;
Expand Down Expand Up @@ -546,10 +534,8 @@ Mongoose.prototype._model = function(name, schema, collection, options, skipInit
const connection = options.connection || _mongoose.connection;
model = _mongoose.Model.compile(model || name, schema, collection, connection, _mongoose);

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

return model;
};
Expand Down
2 changes: 1 addition & 1 deletion test/types.documentarray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ describe('types.documentarray', function() {
next();
});
const schema = new Schema({ children: [child] });
const M = db.model('Test', schema, 'edarecast-' + random());
const M = db.model('Test', schema);
const m = new M;
m.save(function(err) {
assert.ifError(err);
Expand Down

0 comments on commit 1e18e5d

Please sign in to comment.