@@ -180,14 +180,28 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
180
180
return ;
181
181
}
182
182
183
+ // there're two cases where we need to inject polymorphic base hierarchy for fields
184
+ // defined in base models
185
+ // 1. base fields mentioned in select/include clause
186
+ // { select: { fieldFromBase: true } } => { select: { delegate_aux_[Base]: { fieldFromBase: true } } }
187
+ // 2. base fields mentioned in _count select/include clause
188
+ // { select: { _count: { select: { fieldFromBase: true } } } } => { select: { delegate_aux_[Base]: { select: { _count: { select: { fieldFromBase: true } } } } } }
189
+ //
190
+ // Note that although structurally similar, we need to correctly deal with different injection location of the "delegate_aux" hierarchy
191
+
192
+ // selectors for the above two cases
183
193
const selectors = [
194
+ // regular select: { select: { field: true } }
184
195
( payload : any ) => ( { data : payload . select , kind : 'select' as const , isCount : false } ) ,
196
+ // regular include: { include: { field: true } }
185
197
( payload : any ) => ( { data : payload . include , kind : 'include' as const , isCount : false } ) ,
198
+ // select _count: { select: { _count: { select: { field: true } } } }
186
199
( payload : any ) => ( {
187
200
data : payload . select ?. _count ?. select ,
188
201
kind : 'select' as const ,
189
202
isCount : true ,
190
203
} ) ,
204
+ // include _count: { include: { _count: { select: { field: true } } } }
191
205
( payload : any ) => ( {
192
206
data : payload . include ?. _count ?. select ,
193
207
kind : 'include' as const ,
@@ -232,18 +246,24 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
232
246
233
247
let injected = false ;
234
248
if ( ! isCount ) {
249
+ // regular select/include injection
235
250
injected = await this . injectBaseFieldSelect ( model , field , fieldValue , args , kind ) ;
236
251
if ( injected ) {
252
+ // if injected, remove the field from the original payload
237
253
delete data [ field ] ;
238
254
}
239
255
} else {
256
+ // _count select/include injection, inject into an empty payload and then merge to the proper location
240
257
const injectTarget = { [ kind ] : { } } ;
241
258
injected = await this . injectBaseFieldSelect ( model , field , fieldValue , injectTarget , kind , true ) ;
242
259
if ( injected ) {
260
+ // if injected, remove the field from the original payload
243
261
delete data [ field ] ;
244
262
if ( Object . keys ( data ) . length === 0 ) {
263
+ // if the original "_count" payload becomes empty, remove it
245
264
delete args [ kind ] [ '_count' ] ;
246
265
}
266
+ // finally merge the injection into the original payload
247
267
const merged = deepmerge ( args [ kind ] , injectTarget [ kind ] ) ;
248
268
args [ kind ] = merged ;
249
269
}
@@ -308,7 +328,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
308
328
value : any ,
309
329
selectInclude : any ,
310
330
context : 'select' | 'include' ,
311
- forCount = false
331
+ forCount = false // if the injection is for a "{ _count: { select: { field: true } } }" payload
312
332
) {
313
333
const fieldInfo = resolveField ( this . options . modelMeta , model , field ) ;
314
334
if ( ! fieldInfo ?. inheritedFrom ) {
@@ -336,6 +356,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
336
356
thisLayer [ baseRelationName ] = { [ context ] : { } } ;
337
357
}
338
358
if ( forCount ) {
359
+ // { _count: { select: { field: true } } } => { delegate_aux_[Base]: { select: { _count: { select: { field: true } } } } }
339
360
if (
340
361
! thisLayer [ baseRelationName ] [ context ] [ '_count' ] ||
341
362
typeof thisLayer [ baseRelationName ] [ context ] !== 'object'
@@ -347,6 +368,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
347
368
{ select : { [ field ] : value } }
348
369
) ;
349
370
} else {
371
+ // { select: { field: true } } => { delegate_aux_[Base]: { select: { field: true } } }
350
372
thisLayer [ baseRelationName ] [ context ] [ field ] = value ;
351
373
}
352
374
break ;
0 commit comments