Skip to content
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
23 changes: 22 additions & 1 deletion packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1346,16 +1346,37 @@ describe('Collection', () => {
);
});

it('accepts an explicit options object as its first command for legacy compatibility', async() => {
await collection.runCommand({ someCommand: 'differenttestns' });

expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
collection._database._name,
{
someCommand: 'differenttestns'
}
);
});

it('throws an error if commandName is not a string', async() => {
const e = await collection.runCommand(
{} as any
42 as any
).catch(e => e);

expect(e).to.be.instanceOf(MongoshInvalidInputError);
expect(e.message).to.include('type string');
expect(e.code).to.equal(CommonErrors.InvalidArgument);
});

it('throws an error if both arguments are options objects', async() => {
const e = await collection.runCommand(
{}, {}
).catch(e => e);

expect(e).to.be.instanceOf(MongoshInvalidInputError);
expect(e.message).to.include('takes a command string as its first arugment');
expect(e.code).to.equal(CommonErrors.InvalidArgument);
});

it('throws an error if commandName is passed as option', async() => {
const e = await collection.runCommand(
'commandName', { commandName: 1 } as any
Expand Down
34 changes: 20 additions & 14 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,27 +1312,33 @@ export default class Collection extends ShellApiClass {
}

@returnsPromise
async runCommand(commandName: string, options?: RunCommandOptions): Promise<Document> {
assertArgsDefinedType([commandName], ['string'], 'Collection.runCommand');

if (options && commandName in options) {
throw new MongoshInvalidInputError(
'The "commandName" argument cannot be passed as an option to "runCommand".',
CommonErrors.InvalidArgument
);
async runCommand(commandName: string | Document, options?: RunCommandOptions): Promise<Document> {
assertArgsDefinedType([commandName], [['string', 'object']], 'Collection.runCommand');
if (options) {
if (typeof commandName !== 'string') {
throw new MongoshInvalidInputError(
'Collection.runCommand takes a command string as its first arugment',
CommonErrors.InvalidArgument
);
} else if (commandName in options) {
throw new MongoshInvalidInputError(
'The "commandName" argument cannot be passed as an option to "runCommand".',
CommonErrors.InvalidArgument
);
}
}


const hiddenCommands = new RegExp(HIDDEN_COMMANDS);
if (!hiddenCommands.test(commandName)) {
if (typeof commandName === 'string' && !hiddenCommands.test(commandName)) {
this._emitCollectionApiCall('runCommand', { commandName });
}
const cmd = typeof commandName === 'string' ? {
[commandName]: this._name,
...options
} : commandName;
return await this._mongo._serviceProvider.runCommandWithCheck(
this._database._name,
{
[commandName]: this._name,
...options
},
cmd,
this._database._baseOptions
);
}
Expand Down