Skip to content

Commit 237b517

Browse files
authored
Merge pull request #14506 from Automattic/IslandRhythms/list-databases
feat: `listDatabases()` function
2 parents 68a7f6d + f1667e0 commit 237b517

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

lib/connection.js

+24
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,30 @@ Connection.prototype.listCollections = async function listCollections() {
647647
return await cursor.toArray();
648648
};
649649

650+
/**
651+
* Helper for MongoDB Node driver's `listDatabases()`.
652+
* Returns an object with a `databases` property that contains an
653+
* array of database objects.
654+
*
655+
* #### Example:
656+
* const { databases } = await mongoose.connection.listDatabases();
657+
* databases; // [{ name: 'mongoose_test', sizeOnDisk: 0, empty: false }]
658+
*
659+
* @method listCollections
660+
* @return {Promise<{ databases: Array<{ name: string }> }>}
661+
* @api public
662+
*/
663+
664+
Connection.prototype.listDatabases = async function listDatabases() {
665+
if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
666+
await new Promise(resolve => {
667+
this._queue.push({ fn: resolve });
668+
});
669+
}
670+
671+
return await this.db.admin().listDatabases();
672+
};
673+
650674
/**
651675
* Helper for `dropDatabase()`. Deletes the given database, including all
652676
* collections, documents, and indexes.

test/connection.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,17 @@ describe('connections:', function() {
15801580
});
15811581
assert.ok(session);
15821582
});
1583+
it('listDatabases() should return a list of database objects with a name property (gh-9048)', async function() {
1584+
const connection = await mongoose.createConnection(start.uri).asPromise();
1585+
// If this test is running in isolation, then the `start.uri` db might not
1586+
// exist yet, so create this collection (and the associated db) just in case
1587+
await connection.createCollection('tests').catch(() => {});
1588+
1589+
const { databases } = await connection.listDatabases();
1590+
assert.ok(connection.name);
1591+
console.log(databases);
1592+
assert.ok(databases.map(database => database.name).includes(connection.name));
1593+
});
15831594
describe('createCollections()', function() {
15841595
it('should create collections for all models on the connection with the createCollections() function (gh-13300)', async function() {
15851596
const m = new mongoose.Mongoose();

test/types/connection.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ expectType<Promise<string[]>>(
7878
conn.listCollections().then(collections => collections.map(coll => coll.name))
7979
);
8080

81+
expectType<Promise<string[]>>(
82+
conn.listDatabases().then(dbs => dbs.databases.map(db => db.name))
83+
);
84+
8185
export function autoTypedModelConnection() {
8286
const AutoTypedSchema = autoTypedSchema();
8387
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);

types/connection.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ declare module 'mongoose' {
132132
*/
133133
listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;
134134

135+
/**
136+
* Helper for MongoDB Node driver's `listDatabases()`.
137+
* Returns an array of database names.
138+
*/
139+
listDatabases(): Promise<mongodb.ListDatabasesResult>;
140+
135141
/**
136142
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
137143
* a map from model names to models. Contains all models that have been

0 commit comments

Comments
 (0)