Skip to content

Commit

Permalink
fix(model): allow calling Model.init() again after calling `dropDat…
Browse files Browse the repository at this point in the history
…abase()`

Fix #6967
  • Loading branch information
vkarpov15 committed Sep 15, 2018
1 parent 4108366 commit 1ba6ca7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/connection.js
Expand Up @@ -59,6 +59,9 @@ function Connection(base) {
this._readyState = STATES.disconnected;
this._closeCalled = false;
this._hasOpened = false;

this.$internalEmitter = new EventEmitter();
this.$internalEmitter.setMaxListeners(0);
}

/*!
Expand Down Expand Up @@ -327,6 +330,7 @@ Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(co
*/

Connection.prototype.dropDatabase = _wrapConnHelper(function dropDatabase(cb) {
this.$internalEmitter.emit('dropDatabase');
this.db.dropDatabase(cb);
});

Expand Down
10 changes: 9 additions & 1 deletion lib/model.js
Expand Up @@ -982,14 +982,22 @@ for (const i in EventEmitter.prototype) {
Model.init = function init(callback) {
this.schema.emit('init', this);

if (this.$init) {
if (this.$init != null) {
if (callback) {
this.$init.then(() => callback(), err => callback(err));
return null;
}
return this.$init;
}

// If `dropDatabase()` is called, this model's collection will not be
// init-ed. It is sufficiently common to call `dropDatabase()` after
// `mongoose.connect()` but before creating models that we want to
// support this. See gh-6967
this.db.$internalEmitter.once('dropDatabase', () => {
delete this.$init;
});

const Promise = PromiseProvider.get();
const autoIndex = this.schema.options.autoIndex;
this.$init = new Promise((resolve, reject) => {
Expand Down
12 changes: 6 additions & 6 deletions lib/statemachine.js
Expand Up @@ -84,9 +84,9 @@ StateMachine.prototype._changeState = function _changeState(path, nextState) {
*/

StateMachine.prototype.clear = function clear(state) {
let keys = Object.keys(this.states[state]),
i = keys.length,
path;
const keys = Object.keys(this.states[state]);
let i = keys.length;
let path;

while (i--) {
path = keys[i];
Expand Down Expand Up @@ -122,9 +122,9 @@ StateMachine.prototype.some = function some() {

StateMachine.prototype._iter = function _iter(iterMethod) {
return function() {
let numArgs = arguments.length,
states = utils.args(arguments, 0, numArgs - 1),
callback = arguments[numArgs - 1];
const numArgs = arguments.length;
let states = utils.args(arguments, 0, numArgs - 1);
const callback = arguments[numArgs - 1];

if (!states.length) states = this.stateNames;

Expand Down
4 changes: 3 additions & 1 deletion test/model.test.js
Expand Up @@ -5864,6 +5864,8 @@ describe('Model', function() {
});

it('dropDatabase() after init allows re-init (gh-6967)', function() {
const db = mongoose.createConnection(start.uri + '_6967');

const Model = db.model('gh6640', new Schema({
name: { type: String, index: true }
}));
Expand All @@ -5876,7 +5878,7 @@ describe('Model', function() {
assert.ok(!Model.$init);

let threw = false;

try {
yield Model.listIndexes();
} catch (err) {
Expand Down

0 comments on commit 1ba6ca7

Please sign in to comment.