Skip to content

Commit f93f998

Browse files
author
hersveit
authored
Merge pull request #24 from fullstack-development/20-paginate
Paginate
2 parents c4a9a73 + 36341db commit f93f998

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

api/src/core/__tests__/paginate.spec.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ describe('paginate', () => {
5858

5959
const testCases = [
6060
{
61-
payload: { where: { test: true, page: 1, pageSize: 50 } },
61+
payload: { find: { where: { test: true, page: 1, pageSize: 50 } } },
6262
options: { page: 1, pageSize: 50 },
6363
expectedValue: { where: { test: true }, skip: 0, take: 50 },
6464
},
6565
{
66-
payload: { where: { test: true } },
66+
payload: { find: { where: { test: true } } },
6767
options: { page: 1, pageSize: 5 },
6868
expectedValue: { where: { test: true }, skip: 0, take: 5 },
6969
},
7070
{
71-
payload: { where: { test: true, page: 100, pageSize: 5000 } },
71+
payload: { find: { where: { test: true, page: 100, pageSize: 5000 } } },
7272
options: { page: 2, pageSize: 5 },
7373
expectedValue: { where: { test: true }, skip: 5, take: 5 },
7474
},
7575
{
76-
payload: { where: { test: true }, orderBy: { test: true } },
76+
payload: { find: { where: { test: true }, orderBy: { test: true } } },
7777
options: { page: 10, pageSize: 50 },
7878
expectedValue: {
7979
where: { test: true },
@@ -86,8 +86,9 @@ describe('paginate', () => {
8686

8787
for (const testCase of testCases) {
8888
findMany.mockClear();
89-
await paginate(dao, testCase.payload, testCase.options);
89+
await paginate(dao, testCase.options, testCase.payload);
9090
expect(findMany).toBeCalledWith(testCase.expectedValue);
91+
expect(count).toBeCalledWith({});
9192
}
9293
});
9394

@@ -97,11 +98,41 @@ describe('paginate', () => {
9798
findMany.mockResolvedValueOnce(data);
9899
const result = await paginate(
99100
{ count, findMany },
100-
{ where: { test: true, page: 1, pageSize: 50 } },
101101
{ page: 1, pageSize: 50 },
102+
{ find: { where: { test: true, page: 1, pageSize: 50 } } },
102103
);
103104

104105
expect(findMany).toBeCalledWith({ where: { test: true }, skip: 0, take: 50 });
106+
expect(count).toBeCalledWith({});
107+
expect(result).toEqual({ page: 1, pageSize: 50, count: 3, result: data });
108+
});
109+
110+
it('should success return records with count payload', async () => {
111+
const data = [{ test: true }, { test: true }, { test: true }];
112+
count.mockResolvedValueOnce(3);
113+
findMany.mockResolvedValueOnce(data);
114+
const result = await paginate(
115+
{ count, findMany },
116+
{ page: 1, pageSize: 50 },
117+
{
118+
find: { where: { test: true, page: 1, pageSize: 50 } },
119+
count: { where: { test: true } },
120+
},
121+
);
122+
123+
expect(findMany).toBeCalledWith({ where: { test: true }, skip: 0, take: 50 });
124+
expect(count).toBeCalledWith({ where: { test: true } });
125+
expect(result).toEqual({ page: 1, pageSize: 50, count: 3, result: data });
126+
});
127+
128+
it('should success return records without any payload', async () => {
129+
const data = [{ test: true }, { test: true }, { test: true }];
130+
count.mockResolvedValueOnce(3);
131+
findMany.mockResolvedValueOnce(data);
132+
const result = await paginate({ count, findMany }, { page: 1, pageSize: 50 });
133+
134+
expect(findMany).toBeCalledWith({ skip: 0, take: 50 });
135+
expect(count).toBeCalledWith({});
105136
expect(result).toEqual({ page: 1, pageSize: 50, count: 3, result: data });
106137
});
107138
});

api/src/core/paginate.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dissocPath, mergeRight, Path } from 'ramda';
2-
import { Awaited, FirstArgument } from '../utils/typescript.utils';
2+
import { FirstArgument } from '../utils/typescript.utils';
33

44
type PaginateOptions = {
55
page: number;
@@ -25,28 +25,29 @@ export const paginate = async <
2525
findMany: (...args: Array<unknown>) => U;
2626
count: (...args: Array<unknown>) => Promise<number>;
2727
},
28-
P extends FirstArgument<T['findMany']>,
28+
P extends { find?: FirstArgument<T['findMany']>; count?: FirstArgument<T['count']> },
2929
U = ReturnType<T['findMany']>,
3030
>(
3131
dao: T,
32-
payload: P,
3332
{ page, pageSize }: PaginateOptions,
33+
payload = { count: {}, find: {} } as P,
3434
) => {
35+
const findPayload = payload.find || {};
36+
const countPayload = payload.count || {};
3537
const payloadWithPaginate = mergeRight(
36-
omitPath(['where'], ['page', 'pageSize'], payload as Record<string, unknown>),
38+
omitPath(['where'], ['page', 'pageSize'], findPayload as Record<string, unknown>),
3739
{
3840
skip: (page - 1) * pageSize,
3941
take: pageSize,
4042
},
4143
);
4244

43-
const result = (await dao.findMany(payloadWithPaginate)) as Awaited<U>;
44-
// //TODO: need upgrade ts version for fix this
45+
const result = await dao.findMany(payloadWithPaginate);
4546

4647
return {
4748
page,
4849
pageSize,
49-
count: await dao.count(),
50+
count: await dao.count(countPayload),
5051
result,
5152
};
5253
};

api/src/utils/typescript.utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ export type Nullable<T> = { [P in keyof T]: T[P] | null };
33
export type FirstArgument<T> = T extends (first: infer F, ...args: Array<unknown>) => unknown
44
? F
55
: never;
6-
7-
export type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;

0 commit comments

Comments
 (0)