From f4f35cbba673e7063691865074d17d392af6228a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 14 May 2021 18:56:06 +0200 Subject: [PATCH] feat(shell-api): add mongo.getDBs() and .getDBNames() MONGOSH-766 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These were present in the legacy shell, and while they were undocumented, I feel like they are obvious candidates for inclusion in the API, so I’m just going ahead and opening a PR. --- packages/i18n/src/locales/en_US.ts | 9 +++++++++ packages/shell-api/src/integration.spec.ts | 12 ++++++++++++ packages/shell-api/src/mongo.ts | 20 ++++++++++++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/i18n/src/locales/en_US.ts b/packages/i18n/src/locales/en_US.ts index 87c16c2c46..4106b5d79b 100644 --- a/packages/i18n/src/locales/en_US.ts +++ b/packages/i18n/src/locales/en_US.ts @@ -1660,6 +1660,15 @@ const translations: Catalog = { link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDB', description: 'Returns the specified Database of the Mongo object.' }, + getDBs: { + link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDBs', + description: 'Returns information about all databases. Uses the listDatabases command.' + }, + getDBNames: { + link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDBs', + description: 'Returns an array of all database names. Uses the listDatabases command.', + example: 'db.getMongo().getDBNames().map(name => db.getSiblingDB(name).getCollectionNames())' + }, connect: { link: 'https://docs.mongodb.com/manual/reference/method/connect', description: 'Creates a connection to a MongoDB instance and returns the reference to the database.' diff --git a/packages/shell-api/src/integration.spec.ts b/packages/shell-api/src/integration.spec.ts index f0f5a0e071..374059875e 100644 --- a/packages/shell-api/src/integration.spec.ts +++ b/packages/shell-api/src/integration.spec.ts @@ -1744,6 +1744,18 @@ describe('Shell API (integration)', function() { expect(internalState.mongos).to.deep.equal([mongo]); }); }); + describe('getDBs', () => { + it('lists all databases', async() => { + const result = await mongo.getDBs(); + expect(result.ok).to.equal(1); + expect(result.databases.find(db => db.name === 'admin').sizeOnDisk).to.be.a('number'); + }); + }); + describe('getDBNames', () => { + it('lists all database names', async() => { + expect(await mongo.getDBNames()).to.include('admin'); + }); + }); }); describe('PlanCache', () => { describe('list', () => { diff --git a/packages/shell-api/src/mongo.ts b/packages/shell-api/src/mongo.ts index 23da7a757a..c9ba0eb0d3 100644 --- a/packages/shell-api/src/mongo.ts +++ b/packages/shell-api/src/mongo.ts @@ -218,7 +218,7 @@ export default class Mongo extends ShellApiClass { return `switched to db ${db}`; } - async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{name: string, sizeOnDisk: number, empty: boolean}[]> { + async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> { const result = await this._serviceProvider.listDatabases('admin', { ...opts }); if (!('databases' in result)) { const err = new MongoshRuntimeError('Got invalid result from "listDatabases"', CommonErrors.CommandFailed); @@ -226,13 +226,13 @@ export default class Mongo extends ShellApiClass { throw err; } this._cachedDatabaseNames = result.databases.map((db: any) => db.name); - return result.databases; + return result as any; } async _getDatabaseNamesForCompletion(): Promise { return await Promise.race([ (async() => { - return (await this._listDatabases({ readPreference: 'primaryPreferred' })).map(db => db.name); + return (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases.map(db => db.name); })(), (async() => { // See the comment in _getCollectionNamesForCompletion/database.ts @@ -243,6 +243,18 @@ export default class Mongo extends ShellApiClass { ]); } + @returnsPromise + async getDBs(options: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> { + this._emitMongoApiCall('getDBs', { options }); + return await this._listDatabases(options); + } + + @returnsPromise + async getDBNames(options: ListDatabasesOptions = {}): Promise { + this._emitMongoApiCall('getDBNames', { options }); + return (await this._listDatabases(options)).databases.map(db => db.name); + } + @returnsPromise async show(cmd: string, arg?: string): Promise { this._internalState.messageBus.emit('mongosh:show', { method: `show ${cmd}` }); @@ -250,7 +262,7 @@ export default class Mongo extends ShellApiClass { switch (cmd) { case 'databases': case 'dbs': - const result = await this._listDatabases({ readPreference: 'primaryPreferred' }); + const result = (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases; return new CommandResult('ShowDatabasesResult', result); case 'collections': case 'tables':