Skip to content

Commit

Permalink
fix: added multiply to Stages.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Hoogendoorn committed Jul 31, 2024
1 parent d273543 commit a5bbc5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/easy-mongo/src/Stages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { toMongoType } from './Utils';

export const asc = 1;
export const desc = -1;
export type Accumulators = '$sum' | '$count' | '$avg' | '$first' | '$last' | '$min' | '$max' | '$push' | '$addToSet';
export type Accumulators = '$sum' | '$count' | '$multiply' | '$avg' | '$first' | '$last' | '$min' | '$max' | '$push' | '$addToSet';
export type Accumulator = PartialRecord<Accumulators, Filter>;

export class FilterBuilder<Options> {
Expand Down Expand Up @@ -116,6 +116,7 @@ export const stages = {
count: (): Accumulator => ({ $count: {} }),
sum: (from?: string): Accumulator => (isDefined(from) ? { $sum: `$${from}` } : { $sum: 1 }),
avg: (from?: string) => ({ $avg: `$${from}` }),
multiply: (...multiply: string[]) => ({ $multiply: multiply.map(m => `$${m}`) }),
first: (from?: string): Accumulator => ({ $first: `$${from}` }),
last: (from?: string): Accumulator => ({ $last: `$${from}` }),
min: (from?: string): Accumulator => ({ $min: `$${from}` }),
Expand Down
12 changes: 11 additions & 1 deletion packages/easy-mongo/test/Stages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('Stages', () => {
});

// Group
const { group, addToSet, count, avg, sum, first, last, min, max, date, push } = stages.group;
const { group, addToSet, multiply, count, avg, sum, first, last, min, max, date, push } = stages.group;

test('filter undefined', () => {
expect(decode.fields({ total: count(), remove: undefined })).toStrictEqual({ total: { $count: {} } });
Expand Down Expand Up @@ -269,6 +269,16 @@ describe('Stages', () => {
expect(g).toStrictEqual({ $group: { _id: '$brandId', total: { $addToSet: '$count' } } });
});

test('groupBy string id and multiply', () => {
const g = group({ total: multiply('count', 'unitPrice') }).by('brandId');
expect(g).toStrictEqual({ $group: { _id: '$brandId', total: { $multiply: ['$count', '$unitPrice'] } } });
});

test('groupBy string id and multiply in $sum', () => {
const g = group({ total: { $sum: multiply('count', 'unitPrice') } }).by('brandId');
expect(g).toStrictEqual({ $group: { _id: '$brandId', total: { $sum: { $multiply: ['$count', '$unitPrice'] } } } });
});

test('groupBy filter id and single field', () => {
const g = group({ count: count() }).by({ 'created.when': date() });
expect(g).toStrictEqual({
Expand Down

0 comments on commit a5bbc5e

Please sign in to comment.