diff --git a/spec/custom-entity/custom-entity.controller.search.spec.ts b/spec/custom-entity/custom-entity.controller.search.spec.ts index f2b230b..a32435e 100644 --- a/spec/custom-entity/custom-entity.controller.search.spec.ts +++ b/spec/custom-entity/custom-entity.controller.search.spec.ts @@ -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 () => { diff --git a/spec/search/search-cursor-pagination.spec.ts b/spec/search/search-cursor-pagination.spec.ts index 5e46322..6b6d8ab 100644 --- a/spec/search/search-cursor-pagination.spec.ts +++ b/spec/search/search-cursor-pagination.spec.ts @@ -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 () => { diff --git a/src/lib/interceptor/search-request.interceptor.ts b/src/lib/interceptor/search-request.interceptor.ts index 7e0d81e..ab785da 100644 --- a/src/lib/interceptor/search-request.interceptor.ts +++ b/src/lib/interceptor/search-request.interceptor.ts @@ -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 = new CrudReadManyRequest() - .setPrimaryKey(factoryOption.primaryKeys ?? []) + .setPrimaryKey(primaryKeys) .setPagination(pagination) .setSelect(requestSearchDto.select) .setWhere(where)