Skip to content

Commit 22f7705

Browse files
committed
Add db.serverStatus()
1 parent 5ed7ee5 commit 22f7705

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

packages/i18n/src/locales/en_US.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,11 @@ const translations = {
966966
description: 'returns the db serverBuildInfo. uses the buildInfo command',
967967
example: 'db.serverBuildInfo()',
968968
},
969+
serverStatus: {
970+
link: 'https://docs.mongodb.com/manual/reference/method/db.serverStatus',
971+
description: 'returns the server stats. uses the serverStatus command',
972+
example: 'db.serverStatus(<opts>)',
973+
},
969974
stats: {
970975
link: 'https://docs.mongodb.com/manual/reference/method/db.stats',
971976
description: 'returns the db stats. uses the dbStats command',

packages/shell-api/src/database.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,44 @@ describe('Database', () => {
16021602
});
16031603
});
16041604

1605+
describe('serverStatus', () => {
1606+
it('calls serviceProvider.runCommand on the database with options', async() => {
1607+
await database.serverStatus({ repl: 0, metrics: 0, locks: 0 });
1608+
1609+
expect(serviceProvider.runCommand).to.have.been.calledWith(
1610+
ADMIN_DB,
1611+
{
1612+
serverStatus: 1, repl: 0, metrics: 0, locks: 0
1613+
}
1614+
);
1615+
});
1616+
it('calls serviceProvider.runCommand on the database without options', async() => {
1617+
await database.serverStatus();
1618+
1619+
expect(serviceProvider.runCommand).to.have.been.calledWith(
1620+
ADMIN_DB,
1621+
{
1622+
serverStatus: 1
1623+
}
1624+
);
1625+
});
1626+
1627+
it('returns whatever serviceProvider.runCommand returns', async() => {
1628+
const expectedResult = { ok: 1 };
1629+
serviceProvider.runCommand.resolves(expectedResult);
1630+
const result = await database.serverStatus();
1631+
expect(result).to.deep.equal(expectedResult);
1632+
});
1633+
1634+
it('throws if serviceProvider.runCommand rejects', async() => {
1635+
const expectedError = new Error();
1636+
serviceProvider.runCommand.rejects(expectedError);
1637+
const catchedError = await database.serverStatus()
1638+
.catch(e => e);
1639+
expect(catchedError).to.equal(expectedError);
1640+
});
1641+
});
1642+
16051643
describe('hostInfo', () => {
16061644
it('calls serviceProvider.runCommand on the database with options', async() => {
16071645
await database.hostInfo();

packages/shell-api/src/database.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,21 @@ export default class Database extends ShellApiClass {
688688
return result;
689689
}
690690

691+
@returnsPromise
692+
async serverStatus(opts = {}): Promise<any> {
693+
this._emitDatabaseApiCall('serverStatus', { options: opts });
694+
const result = await this._mongo._serviceProvider.runCommand(
695+
ADMIN_DB,
696+
{
697+
serverStatus: 1, ...opts
698+
}
699+
);
700+
if (!result || !result.ok) {
701+
throw new MongoshRuntimeError(`Error running command serverStatus ${result ? result.errmsg || '' : ''}`);
702+
}
703+
return result;
704+
}
705+
691706
@returnsPromise
692707
async stats(scale: any): Promise<any> {
693708
assertArgsDefined(scale);

0 commit comments

Comments
 (0)