Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listDatabases() function #14506

Merged
merged 3 commits into from
Apr 8, 2024
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
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