Skip to content

Commit

Permalink
add single stage support instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Oct 15, 2024
1 parent 30355c5 commit bf52c7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
19 changes: 9 additions & 10 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,18 @@ describe('Database', function () {
serviceProviderCursor = stubInterface<ServiceProviderAggCursor>();
});

it('throws if the given argument is not an array', async function () {
let caughtError: MongoshInvalidInputError | undefined;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await database.aggregate({} as any).catch((err) => {
caughtError = err;
});
expect(caughtError?.message).contains(
'Aggregate pipeline argument must be an array'
it('calls serviceProvider.aggregateDb with pipleline and options', async function () {
await database.aggregate([{ $piplelineStage: {} }], { options: true });

expect(serviceProvider.aggregateDb).to.have.been.calledWith(
database._name,
[{ $piplelineStage: {} }],
{ options: true }
);
});

it('calls serviceProvider.aggregateDb with pipleline and options', async function () {
await database.aggregate([{ $piplelineStage: {} }], { options: true });
it('supports a single aggregation stage', async function () {
await database.aggregate({ $piplelineStage: {} }, { options: true });

expect(serviceProvider.aggregateDb).to.have.been.calledWith(
database._name,
Expand Down
14 changes: 13 additions & 1 deletion packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ export default class Database extends ShellApiWithMongoClass {
return await this._runAdminCommand(cmd, {});
}

async aggregate(
singleStage: Document,
options?: Document
): Promise<AggregationCursor>;
async aggregate(
pipeline: Document[],
options?: Document
): Promise<AggregationCursor>;
/**
* Run an aggregation against the db.
*
Expand All @@ -423,14 +431,18 @@ export default class Database extends ShellApiWithMongoClass {
@returnType('AggregationCursor')
@apiVersions([1])
async aggregate(
pipeline: Document[],
pipelineOrSingleStage: Document | Document[],
options?: Document
): Promise<AggregationCursor> {
if ('background' in (options ?? {})) {
await this._instanceState.printWarning(
aggregateBackgroundOptionNotSupportedHelp
);
}
const pipeline: Document[] = Array.isArray(pipelineOrSingleStage)
? pipelineOrSingleStage
: [pipelineOrSingleStage];

assertArgsDefinedType([pipeline], [true], 'Database.aggregate');

if (!Array.isArray(pipeline)) {
Expand Down

0 comments on commit bf52c7f

Please sign in to comment.