Skip to content

Commit

Permalink
fix: adding date() to project in Stages.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Hoogendoorn committed Sep 13, 2024
1 parent 9917d8a commit 2705bb2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/easy-mongo/src/Stages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const stages = {
ifNotEmpty(excludes, es => ({ $project: es.reduce((a: Filter, b: Filter) => ({ ...a, ...(isString(b) ? { [b]: 0 } : b) }), {}) })),
includes: (includes: Record<string, (string | Record<string, 1>)[]>) => new IncludeBuilder(includes),
project: (project?: Filter) => ifDefined(project, $project => ({ $project })),
date: (key: string, format = '%Y-%m-%d') => ({ $toDate: `$${key}`, format }),
},
replaceWith: {
replaceWith: (f?: Filter): Optional<Filter> => ifDefined(f, { $replaceWith: f }),
Expand Down
7 changes: 6 additions & 1 deletion packages/easy-mongo/test/Stages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('Stages', () => {
});

// Project
const { include, exclude, includes, project } = stages.project;
const { include, exclude, includes, project, date: pDate } = stages.project;

test('projection include', () => {
expect(include()).toBeUndefined();
Expand Down Expand Up @@ -411,6 +411,11 @@ describe('Stages', () => {
expect(project({ name: 1, color: 1 })).toStrictEqual({ $project: { name: 1, color: 1 } });
});

test('date', () => {
expect(pDate('created')).toStrictEqual({ $toDate: '$created', format: '%Y-%m-%d' });
expect(pDate('created', '%Y-%m-%d %H:%M:%S')).toStrictEqual({ $toDate: '$created', format: '%Y-%m-%d %H:%M:%S' });
});

// ReplaceWith

const { merge, reroot, concat, rootAnd, currentAnd, replaceWith } = stages.replaceWith;
Expand Down
2 changes: 1 addition & 1 deletion packages/easy/test/types/AnyKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ describe('AnyKey', () => {
expect(t.toValue('name.last')).toBe('Jansen');
expect(t.toValue('name.first')).toBe('Jan');
expect(t.toValue('paid.cents')).toBe(3000);
expect(t.toValue('name.address.city')).toBe('Amsterdam');
expect(t.toValue('name.address.country')).toBe('NL');
});
});

0 comments on commit 2705bb2

Please sign in to comment.