Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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();
Expand All @@ -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');
};

/**
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/drivers/node-mongodb-native/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
1 change: 0 additions & 1 deletion test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down