From 35c18330273f169395a94a3879e4829539ccf033 Mon Sep 17 00:00:00 2001 From: Alex H Date: Mon, 24 Apr 2023 18:54:47 +0600 Subject: [PATCH] fix(json-api-nestjs): fix test for prev commit - incorrect condition for relation: user?photo.userId=1 - add suuport several condition to one field: user?date<2022-12-12&date>2022-12-12 --- .../typeorm/methods/get-all/get-all.spec.ts | 20 ++++++-- .../typeorm/methods/get-all/get-all.ts | 46 +++++++++++-------- .../typeorm/utils/utils-methode.spec.ts | 3 +- .../service/typeorm/utils/utils-methode.ts | 36 +++++---------- 4 files changed, 55 insertions(+), 50 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 1790f0ad..a67c2731 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 @@ -317,14 +317,19 @@ describe('GetAll methode test', () => { }, }); - expect(joinSpy).toBeCalledTimes(2); + expect(joinSpy).toBeCalledTimes(3); expect(joinSpy).toHaveBeenNthCalledWith( 1, + `${aliasString}.comments`, + 'comments' + ); + expect(joinSpy).toHaveBeenNthCalledWith( + 2, `${aliasString}.manager`, 'manager' ); expect(joinSpy).toHaveBeenNthCalledWith( - 2, + 3, `${aliasString}.addresses`, 'addresses' ); @@ -353,7 +358,7 @@ describe('GetAll methode test', () => { expect(result.meta.totalItems).toBe(0); expect(result.meta.pageSize).toBe(page.size); - expect(resultJoinSpy).toBeCalledTimes(2); + expect(resultJoinSpy).toBeCalledTimes(4); expect(resultJoinSpy).toHaveBeenNthCalledWith( 1, `${aliasString}.${include[0]}`, @@ -469,14 +474,19 @@ describe('GetAll methode test', () => { }, }); - expect(joinSpy).toBeCalledTimes(2); + expect(joinSpy).toBeCalledTimes(3); expect(joinSpy).toHaveBeenNthCalledWith( 1, + `${alisString}.comments`, + 'comments' + ); + expect(joinSpy).toHaveBeenNthCalledWith( + 2, `${alisString}.manager`, 'manager' ); expect(joinSpy).toHaveBeenNthCalledWith( - 2, + 3, `${alisString}.addresses`, 'addresses' ); 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 bc4d2925..67ceaa4d 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 @@ -1,22 +1,22 @@ -import { BadRequestException } from '@nestjs/common'; -import { OrderByCondition } from 'typeorm/find-options/OrderByCondition'; +import {BadRequestException} from '@nestjs/common'; +import {OrderByCondition} from 'typeorm/find-options/OrderByCondition'; -import { TypeormMixinService } from '../../typeorm.mixin'; -import { ServiceOptions, SortType } from '../../../../../types'; -import { ResourceObject } from '../../../../../types-common'; +import {TypeormMixinService} from '../../typeorm.mixin'; +import {ServiceOptions, SortType} from '../../../../../types'; +import {ResourceObject} from '../../../../../types-common'; -import { snakeToCamel } from '../../../../../helper'; +import {snakeToCamel} from '../../../../../helper'; export async function getAll( this: TypeormMixinService, options: ServiceOptions ): Promise> { const startTime = Date.now(); - const { filter, include, sort, page, fields } = options.query; + const {filter, include, sort, page, fields} = options.query; if (this.config.requiredSelectField && fields === null) { throw new BadRequestException([ { - source: { parameter: '/fields' }, + source: {parameter: '/fields'}, detail: 'Fields params in query is required', }, ]); @@ -54,7 +54,7 @@ export async function getAll( fieldsSelect.add(`${rel}.${propsName}`); } - const { target, ...other } = fields; + const {target, ...other} = fields; const targetArray = [...target, this.repository.metadata.primaryColumns[0].propertyName] || []; @@ -69,7 +69,7 @@ export async function getAll( }); } - const { target, relation } = filter; + const {target, relation} = filter; const expressionObjectForRelation = relation ? this.UtilsMethode.applyQueryFilterRelation( @@ -94,14 +94,14 @@ export async function getAll( const joinForCommonQuery = {}; for (let i = 0; i < expressionObjectLength; i++) { - const { expression, params, selectInclude } = expressionObject[i]; + const {expression, params, selectInclude} = expressionObject[i]; if (selectInclude) { joinForCommonQuery[`${preparedResourceName}.${selectInclude}`] = selectInclude; } builder[i === 0 ? 'where' : 'andWhere'](expression); if (params) { - builder.setParameters(params ? { [params.name]: params.val } : {}); + builder.setParameters(params ? {[params.name]: params.val} : {}); } } for (let i = 0; i < includeLength; i++) { @@ -116,7 +116,7 @@ export async function getAll( builder.select(`${preparedResourceName}.${primaryColumn}`, subQueryIdAlias); if (sort) { - const { target, ...otherSort } = sort; + const {target, ...otherSort} = sort; const targetOrder = Object.entries( target || {} ).reduce((acum, [key, val]) => { @@ -172,19 +172,27 @@ export async function getAll( const resultBuilder = resultBuilderQuery .select([...fieldsSelect]) - .whereInIds(resultIds.map((i) => i[`${countAlias}_${primaryColumn}`])); + const ids = resultIds.map((i) => i[`${countAlias}_${primaryColumn}`]); + if (ids.length > 0) { + resultBuilder.whereInIds(resultIds.map((i) => i[`${countAlias}_${primaryColumn}`])); + } for (let i = 0; i < expressionObjectForRelation.length; i++) { - const { expression, params, selectInclude } = + const {expression, params, selectInclude} = expressionObjectForRelation[i]; - if (selectInclude) { + if (selectInclude && !include.includes(selectInclude as any)) { resultBuilder.leftJoin( `${preparedResourceName}.${selectInclude}`, selectInclude ); } - resultBuilder.andWhere(expression); - resultBuilder.setParameters(params ? { [params.name]: params.val } : {}); + if (i === 0 && ids.length === 0) { + resultBuilder.where(expression); + } else { + resultBuilder.andWhere(expression); + } + + resultBuilder.setParameters(params ? {[params.name]: params.val} : {}); } const result = await resultBuilder.getRawMany(); @@ -206,7 +214,7 @@ export async function getAll( pageNumber: page.number, totalItems: count, pageSize: page.size, - ...(this.config.debug ? { debug } : {}), + ...(this.config.debug ? {debug} : {}), }, data, included, diff --git a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.spec.ts b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.spec.ts index fe898520..afe8936c 100644 --- a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.spec.ts +++ b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.spec.ts @@ -483,7 +483,8 @@ describe('Utils methode test', () => { )}` ) .getQuery(); - const check = `${alias}.id IN (${resultQuery})`; + // const check = `${alias}.id IN (${resultQuery})`; + const check = `comments.kind = :params_comments.kind_0`; expect(expression[i].expression).toBe(check); expect(expression[i].params.val).toBe( filter[relName][fieldName][operand] diff --git a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.ts b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.ts index 470cf1c4..32c8b674 100644 --- a/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.ts +++ b/libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.ts @@ -267,32 +267,18 @@ export class UtilsMethode { break; } case 'one-to-many': { - if (paramsField !== null) { - resultExpression.push({ - expression: `${relationProperty}.${relationFieldProperty.toString()} ${currentOperandMap[ - operand - ].replace('EXPRESSION', paramsField)}`, - params: { - val: value, - name: paramsField, - }, - selectInclude: relationProperty, - }); - break; - } - const query = builder - .subQuery() - .select(`${resourceName}.${inverseSidePropertyPath}`) - .from(target, resourceName) - .where( - `${resourceName}.${relationFieldProperty.toString()} ${currentOperandMap[ - operand - ].replace('EXPRESSION', paramsField)}` - ) - .getQuery(); resultExpression.push({ - expression: `${preparedResourceName}.id IN ${query}`, - params: null, + expression: `${relationProperty}.${relationFieldProperty.toString()} ${currentOperandMap[ + operand + ].replace('EXPRESSION', paramsField)}`, + params: + paramsField === null + ? null + : { + val: value, + name: paramsField, + }, + selectInclude: relationProperty, }); break; }