Skip to content

Commit

Permalink
Merge pull request #277 from jiho-kr/feature/seach-default-order
Browse files Browse the repository at this point in the history
should be defined `default order` in 'Search'
  • Loading branch information
jiho-kr committed Aug 23, 2023
2 parents 71fb8f0 + 237fbe5 commit 0f033f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion spec/custom-entity/custom-entity.controller.search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('CustomEntity - Search', () => {
.send({ where: [{ uuid: { operator: '=', operand: '1' } }, { uuid: { operator: '=', operand: '10' } }] });
expect(response.statusCode).toEqual(HttpStatus.OK);
expect(response.body.data).toHaveLength(2);
expect(response.body.data.map((d: { uuid: string }) => d.uuid)).toEqual(['1', '10']);
expect(response.body.data.map((d: { uuid: string }) => d.uuid)).toEqual(expect.arrayContaining(['1', '10']));
});

it('should return entities filtered by null', async () => {
Expand Down
17 changes: 17 additions & 0 deletions spec/search/search-cursor-pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ describe('Search Cursor Pagination', () => {
nextCursor: expect.any(String),
},
});

const { body: searchInBody } = await request(app.getHttpServer())
.post('/base/search')
.send({ where: [{ col1: { operator: 'IN', operand: ['col1_19', 'col1_21', 'col1_23', 'col1_25', 'col1_27'] } }], take: 2 })
.expect(HttpStatus.OK);
expect(searchInBody.data).toEqual([
{ col1: 'col1_27', col2: 27, col3: null },
{ col1: 'col1_25', col2: 25, col3: null },
]);
const { body: nextInBody } = await request(app.getHttpServer())
.post('/base/search')
.send({ nextCursor: searchInBody.metadata.nextCursor })
.expect(HttpStatus.OK);
expect(nextInBody.data).toEqual([
{ col1: 'col1_23', col2: 23, col3: 27 },
{ col1: 'col1_21', col2: 21, col3: 29 },
]);
});

it('should be use empty body', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/interceptor/search-request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ export function SearchRequestInterceptor(crudOptions: CrudOptions, factoryOption
)
: {};

const primaryKeys = factoryOption.primaryKeys ?? [];
requestSearchDto.order ??= primaryKeys.reduce((acc, { name }) => ({ ...acc, [name]: CRUD_POLICY[method].default.sort }), {});

const crudReadManyRequest: CrudReadManyRequest<typeof crudOptions.entity> = new CrudReadManyRequest<typeof crudOptions.entity>()
.setPrimaryKey(factoryOption.primaryKeys ?? [])
.setPrimaryKey(primaryKeys)
.setPagination(pagination)
.setSelect(requestSearchDto.select)
.setWhere(where)
Expand Down

0 comments on commit 0f033f3

Please sign in to comment.