1- import { BadRequestException } from '@nestjs/common' ;
2- import { OrderByCondition } from 'typeorm/find-options/OrderByCondition' ;
1+ import { BadRequestException } from '@nestjs/common' ;
2+ import { OrderByCondition } from 'typeorm/find-options/OrderByCondition' ;
33
4- import { TypeormMixinService } from '../../typeorm.mixin' ;
5- import { ServiceOptions , SortType } from '../../../../../types' ;
6- import { ResourceObject } from '../../../../../types-common' ;
4+ import { TypeormMixinService } from '../../typeorm.mixin' ;
5+ import { ServiceOptions , SortType } from '../../../../../types' ;
6+ import { ResourceObject } from '../../../../../types-common' ;
77
8- import { snakeToCamel } from '../../../../../helper' ;
8+ import { snakeToCamel } from '../../../../../helper' ;
99
1010export async function getAll < T > (
1111 this : TypeormMixinService < T > ,
1212 options : ServiceOptions < T >
1313) : Promise < ResourceObject < T > > {
1414 const startTime = Date . now ( ) ;
15- const { filter, include, sort, page, fields } = options . query ;
15+ const { filter, include, sort, page, fields} = options . query ;
1616 if ( this . config . requiredSelectField && fields === null ) {
1717 throw new BadRequestException ( [
1818 {
19- source : { parameter : '/fields' } ,
19+ source : { parameter : '/fields' } ,
2020 detail : 'Fields params in query is required' ,
2121 } ,
2222 ] ) ;
@@ -54,7 +54,7 @@ export async function getAll<T>(
5454 fieldsSelect . add ( `${ rel } .${ propsName } ` ) ;
5555 }
5656
57- const { target, ...other } = fields ;
57+ const { target, ...other } = fields ;
5858 const targetArray =
5959 [ ...target , this . repository . metadata . primaryColumns [ 0 ] . propertyName ] ||
6060 [ ] ;
@@ -69,7 +69,7 @@ export async function getAll<T>(
6969 } ) ;
7070 }
7171
72- const { target, relation } = filter ;
72+ const { target, relation} = filter ;
7373
7474 const expressionObjectForRelation = relation
7575 ? this . UtilsMethode . applyQueryFilterRelation (
@@ -94,14 +94,14 @@ export async function getAll<T>(
9494 const joinForCommonQuery = { } ;
9595
9696 for ( let i = 0 ; i < expressionObjectLength ; i ++ ) {
97- const { expression, params, selectInclude } = expressionObject [ i ] ;
97+ const { expression, params, selectInclude} = expressionObject [ i ] ;
9898 if ( selectInclude ) {
9999 joinForCommonQuery [ `${ preparedResourceName } .${ selectInclude } ` ] =
100100 selectInclude ;
101101 }
102102 builder [ i === 0 ? 'where' : 'andWhere' ] ( expression ) ;
103103 if ( params ) {
104- builder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
104+ builder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
105105 }
106106 }
107107 for ( let i = 0 ; i < includeLength ; i ++ ) {
@@ -116,7 +116,7 @@ export async function getAll<T>(
116116 builder . select ( `${ preparedResourceName } .${ primaryColumn } ` , subQueryIdAlias ) ;
117117
118118 if ( sort ) {
119- const { target, ...otherSort } = sort ;
119+ const { target, ...otherSort } = sort ;
120120 const targetOrder = Object . entries < SortType > (
121121 target || { }
122122 ) . reduce < OrderByCondition > ( ( acum , [ key , val ] ) => {
@@ -172,19 +172,27 @@ export async function getAll<T>(
172172
173173 const resultBuilder = resultBuilderQuery
174174 . select ( [ ...fieldsSelect ] )
175- . whereInIds ( resultIds . map ( ( i ) => i [ `${ countAlias } _${ primaryColumn } ` ] ) ) ;
175+ const ids = resultIds . map ( ( i ) => i [ `${ countAlias } _${ primaryColumn } ` ] ) ;
176+ if ( ids . length > 0 ) {
177+ resultBuilder . whereInIds ( resultIds . map ( ( i ) => i [ `${ countAlias } _${ primaryColumn } ` ] ) ) ;
178+ }
176179
177180 for ( let i = 0 ; i < expressionObjectForRelation . length ; i ++ ) {
178- const { expression, params, selectInclude } =
181+ const { expression, params, selectInclude} =
179182 expressionObjectForRelation [ i ] ;
180- if ( selectInclude ) {
183+ if ( selectInclude && ! include . includes ( selectInclude as any ) ) {
181184 resultBuilder . leftJoin (
182185 `${ preparedResourceName } .${ selectInclude } ` ,
183186 selectInclude
184187 ) ;
185188 }
186- resultBuilder . andWhere ( expression ) ;
187- resultBuilder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
189+ if ( i === 0 && ids . length === 0 ) {
190+ resultBuilder . where ( expression ) ;
191+ } else {
192+ resultBuilder . andWhere ( expression ) ;
193+ }
194+
195+ resultBuilder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
188196 }
189197
190198 const result = await resultBuilder . getRawMany ( ) ;
@@ -206,7 +214,7 @@ export async function getAll<T>(
206214 pageNumber : page . number ,
207215 totalItems : count ,
208216 pageSize : page . size ,
209- ...( this . config . debug ? { debug } : { } ) ,
217+ ...( this . config . debug ? { debug} : { } ) ,
210218 } ,
211219 data,
212220 included,
0 commit comments