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' ;
3
3
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' ;
7
7
8
- import { snakeToCamel } from '../../../../../helper' ;
8
+ import { snakeToCamel } from '../../../../../helper' ;
9
9
10
10
export async function getAll < T > (
11
11
this : TypeormMixinService < T > ,
12
12
options : ServiceOptions < T >
13
13
) : Promise < ResourceObject < T > > {
14
14
const startTime = Date . now ( ) ;
15
- const { filter, include, sort, page, fields } = options . query ;
15
+ const { filter, include, sort, page, fields} = options . query ;
16
16
if ( this . config . requiredSelectField && fields === null ) {
17
17
throw new BadRequestException ( [
18
18
{
19
- source : { parameter : '/fields' } ,
19
+ source : { parameter : '/fields' } ,
20
20
detail : 'Fields params in query is required' ,
21
21
} ,
22
22
] ) ;
@@ -54,7 +54,7 @@ export async function getAll<T>(
54
54
fieldsSelect . add ( `${ rel } .${ propsName } ` ) ;
55
55
}
56
56
57
- const { target, ...other } = fields ;
57
+ const { target, ...other } = fields ;
58
58
const targetArray =
59
59
[ ...target , this . repository . metadata . primaryColumns [ 0 ] . propertyName ] ||
60
60
[ ] ;
@@ -69,7 +69,7 @@ export async function getAll<T>(
69
69
} ) ;
70
70
}
71
71
72
- const { target, relation } = filter ;
72
+ const { target, relation} = filter ;
73
73
74
74
const expressionObjectForRelation = relation
75
75
? this . UtilsMethode . applyQueryFilterRelation (
@@ -94,14 +94,14 @@ export async function getAll<T>(
94
94
const joinForCommonQuery = { } ;
95
95
96
96
for ( let i = 0 ; i < expressionObjectLength ; i ++ ) {
97
- const { expression, params, selectInclude } = expressionObject [ i ] ;
97
+ const { expression, params, selectInclude} = expressionObject [ i ] ;
98
98
if ( selectInclude ) {
99
99
joinForCommonQuery [ `${ preparedResourceName } .${ selectInclude } ` ] =
100
100
selectInclude ;
101
101
}
102
102
builder [ i === 0 ? 'where' : 'andWhere' ] ( expression ) ;
103
103
if ( params ) {
104
- builder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
104
+ builder . setParameters ( params ? { [ params . name ] : params . val } : { } ) ;
105
105
}
106
106
}
107
107
for ( let i = 0 ; i < includeLength ; i ++ ) {
@@ -116,7 +116,7 @@ export async function getAll<T>(
116
116
builder . select ( `${ preparedResourceName } .${ primaryColumn } ` , subQueryIdAlias ) ;
117
117
118
118
if ( sort ) {
119
- const { target, ...otherSort } = sort ;
119
+ const { target, ...otherSort } = sort ;
120
120
const targetOrder = Object . entries < SortType > (
121
121
target || { }
122
122
) . reduce < OrderByCondition > ( ( acum , [ key , val ] ) => {
@@ -172,19 +172,27 @@ export async function getAll<T>(
172
172
173
173
const resultBuilder = resultBuilderQuery
174
174
. 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
+ }
176
179
177
180
for ( let i = 0 ; i < expressionObjectForRelation . length ; i ++ ) {
178
- const { expression, params, selectInclude } =
181
+ const { expression, params, selectInclude} =
179
182
expressionObjectForRelation [ i ] ;
180
- if ( selectInclude ) {
183
+ if ( selectInclude && ! include . includes ( selectInclude as any ) ) {
181
184
resultBuilder . leftJoin (
182
185
`${ preparedResourceName } .${ selectInclude } ` ,
183
186
selectInclude
184
187
) ;
185
188
}
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 } : { } ) ;
188
196
}
189
197
190
198
const result = await resultBuilder . getRawMany ( ) ;
@@ -206,7 +214,7 @@ export async function getAll<T>(
206
214
pageNumber : page . number ,
207
215
totalItems : count ,
208
216
pageSize : page . size ,
209
- ...( this . config . debug ? { debug } : { } ) ,
217
+ ...( this . config . debug ? { debug} : { } ) ,
210
218
} ,
211
219
data,
212
220
included,
0 commit comments