From c994e0eff786f292de73b4221fbf82919510161c Mon Sep 17 00:00:00 2001 From: Alex H Date: Tue, 25 Apr 2023 16:46:50 +0600 Subject: [PATCH] fix(json-api-nestjs): fix incorrect condition - if result empty do not need call query --- .../typeorm/methods/get-all/get-all.spec.ts | 2 +- .../typeorm/methods/get-all/get-all.ts | 29 ++++++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.spec.ts b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.spec.ts index a67c2731..491b31da 100644 --- a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.spec.ts +++ b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.spec.ts @@ -358,7 +358,7 @@ describe('GetAll methode test', () => { expect(result.meta.totalItems).toBe(0); expect(result.meta.pageSize).toBe(page.size); - expect(resultJoinSpy).toBeCalledTimes(4); + expect(resultJoinSpy).toBeCalledTimes(2); expect(resultJoinSpy).toHaveBeenNthCalledWith( 1, `${aliasString}.${include[0]}`, diff --git a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.ts b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.ts index f1b47077..b891b2ac 100644 --- a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.ts +++ b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.ts @@ -173,29 +173,24 @@ export async function getAll( const resultBuilder = resultBuilderQuery .select([...fieldsSelect]) const ids = resultIds.map((i) => i[`${countAlias}_${primaryColumn}`]); + let result = []; if (ids.length > 0) { resultBuilder.whereInIds(resultIds.map((i) => i[`${countAlias}_${primaryColumn}`])); - } - - for (let i = 0; i < expressionObjectForRelation.length; i++) { - const {expression, params, selectInclude} = - expressionObjectForRelation[i]; - if (selectInclude && !(include || []).includes(selectInclude as any)) { - resultBuilder.leftJoin( - `${preparedResourceName}.${selectInclude}`, - selectInclude - ); - } - if (i === 0 && ids.length === 0) { - resultBuilder.where(expression); - } else { + for (let i = 0; i < expressionObjectForRelation.length; i++) { + const {expression, params, selectInclude} = + expressionObjectForRelation[i]; + if (selectInclude && !(include || []).includes(selectInclude as any)) { + resultBuilder.leftJoin( + `${preparedResourceName}.${selectInclude}`, + selectInclude + ); + } resultBuilder.andWhere(expression); + resultBuilder.setParameters(params ? {[params.name]: params.val} : {}); } - - resultBuilder.setParameters(params ? {[params.name]: params.val} : {}); } - const result = await resultBuilder.getRawMany(); + result = await resultBuilder.getRawMany(); const callQuery = Date.now() - startTime;