Skip to content

Commit

Permalink
Merge pull request #14506 from Automattic/IslandRhythms/list-databases
Browse files Browse the repository at this point in the history
feat: `listDatabases()` function
  • Loading branch information
vkarpov15 authored Apr 8, 2024
2 parents 68a7f6d + f1667e0 commit 237b517
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,30 @@ Connection.prototype.listCollections = async function listCollections() {
return await cursor.toArray();
};

/**
* Helper for MongoDB Node driver's `listDatabases()`.
* Returns an object with a `databases` property that contains an
* array of database objects.
*
* #### Example:
* const { databases } = await mongoose.connection.listDatabases();
* databases; // [{ name: 'mongoose_test', sizeOnDisk: 0, empty: false }]
*
* @method listCollections
* @return {Promise<{ databases: Array<{ name: string }> }>}
* @api public
*/

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();
};

/**
* Helper for `dropDatabase()`. Deletes the given database, including all
* collections, documents, and indexes.
Expand Down
11 changes: 11 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,17 @@ describe('connections:', function() {
});
assert.ok(session);
});
it('listDatabases() should return a list of database objects with a name property (gh-9048)', async function() {
const connection = await mongoose.createConnection(start.uri).asPromise();
// If this test is running in isolation, then the `start.uri` db might not
// exist yet, so create this collection (and the associated db) just in case
await connection.createCollection('tests').catch(() => {});

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() {
it('should create collections for all models on the connection with the createCollections() function (gh-13300)', async function() {
const m = new mongoose.Mongoose();
Expand Down
4 changes: 4 additions & 0 deletions test/types/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ expectType<Promise<string[]>>(
conn.listCollections().then(collections => collections.map(coll => coll.name))
);

expectType<Promise<string[]>>(
conn.listDatabases().then(dbs => dbs.databases.map(db => db.name))
);

export function autoTypedModelConnection() {
const AutoTypedSchema = autoTypedSchema();
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);
Expand Down
6 changes: 6 additions & 0 deletions types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ declare module 'mongoose' {
*/
listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;

/**
* Helper for MongoDB Node driver's `listDatabases()`.
* Returns an array of database names.
*/
listDatabases(): Promise<mongodb.ListDatabasesResult>;

/**
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
* a map from model names to models. Contains all models that have been
Expand Down

0 comments on commit 237b517

Please sign in to comment.