diff --git a/lib/connection.js b/lib/connection.js index 48ea6bb2450..44c5cef0e63 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -398,11 +398,7 @@ Connection.prototype.createCollection = async function createCollection(collecti throw new MongooseError('Connection.prototype.createCollection() no longer accepts a callback'); } - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { - await new Promise(resolve => { - this._queue.push({ fn: resolve }); - }); - } + await this._waitForConnect(); return this.db.createCollection(collection, options); }; @@ -494,11 +490,7 @@ Connection.prototype.startSession = async function startSession(options) { throw new MongooseError('Connection.prototype.startSession() no longer accepts a callback'); } - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { - await new Promise(resolve => { - this._queue.push({ fn: resolve }); - }); - } + await this._waitForConnect(); const session = this.client.startSession(options); return session; @@ -618,13 +610,24 @@ Connection.prototype.dropCollection = async function dropCollection(collection) throw new MongooseError('Connection.prototype.dropCollection() no longer accepts a callback'); } + await this._waitForConnect(); + + return this.db.dropCollection(collection); +}; + +/** + * Waits for connection to be established, so the connection has a `client` + * + * @return Promise + * @api private + */ + +Connection.prototype._waitForConnect = async function _waitForConnect() { if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { await new Promise(resolve => { this._queue.push({ fn: resolve }); }); } - - return this.db.dropCollection(collection); }; /** @@ -637,11 +640,7 @@ Connection.prototype.dropCollection = async function dropCollection(collection) */ Connection.prototype.listCollections = async function listCollections() { - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { - await new Promise(resolve => { - this._queue.push({ fn: resolve }); - }); - } + await this._waitForConnect(); const cursor = this.db.listCollections(); return await cursor.toArray(); @@ -662,13 +661,8 @@ Connection.prototype.listCollections = async function listCollections() { */ Connection.prototype.listDatabases = async function listDatabases() { - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { - await new Promise(resolve => { - this._queue.push({ fn: resolve }); - }); - } - - return await this.db.admin().listDatabases(); + // Implemented in `lib/drivers/node-mongodb-native/connection.js` + throw new MongooseError('listDatabases() not implemented by driver'); }; /** @@ -691,11 +685,7 @@ Connection.prototype.dropDatabase = async function dropDatabase() { throw new MongooseError('Connection.prototype.dropDatabase() no longer accepts a callback'); } - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { - await new Promise(resolve => { - this._queue.push({ fn: resolve }); - }); - } + await this._waitForConnect(); // If `dropDatabase()` is called, this model's collection will not be // init-ed. It is sufficiently common to call `dropDatabase()` after diff --git a/lib/drivers/node-mongodb-native/connection.js b/lib/drivers/node-mongodb-native/connection.js index c0f3261595f..3c64ff2216f 100644 --- a/lib/drivers/node-mongodb-native/connection.js +++ b/lib/drivers/node-mongodb-native/connection.js @@ -197,6 +197,19 @@ NativeConnection.prototype.doClose = async function doClose(force) { return this; }; +/** + * Implementation of `listDatabases()` for MongoDB driver + * + * @return Promise + * @api public + */ + +NativeConnection.prototype.listDatabases = async function listDatabases() { + await this._waitForConnect(); + + return await this.db.admin().listDatabases(); +}; + /*! * ignore */ diff --git a/test/connection.test.js b/test/connection.test.js index 82df2ee4c3a..77ec8ac64d1 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1588,7 +1588,6 @@ describe('connections:', function() { const { databases } = await connection.listDatabases(); assert.ok(connection.name); - console.log(databases); assert.ok(databases.map(database => database.name).includes(connection.name)); }); describe('createCollections()', function() {