@@ -180,14 +180,28 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
180180            return ; 
181181        } 
182182
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 
183193        const  selectors  =  [ 
194+             // regular select: { select: { field: true }  } 
184195            ( payload : any )  =>  ( {  data : payload . select ,  kind : 'select'  as  const ,  isCount : false  } ) , 
196+             // regular include: { include: { field: true }  } 
185197            ( payload : any )  =>  ( {  data : payload . include ,  kind : 'include'  as  const ,  isCount : false  } ) , 
198+             // select _count: { select: { _count: { select: { field: true }  }  }  } 
186199            ( payload : any )  =>  ( { 
187200                data : payload . select ?. _count ?. select , 
188201                kind : 'select'  as  const , 
189202                isCount : true , 
190203            } ) , 
204+             // include _count: { include: { _count: { select: { field: true }  }  }  } 
191205            ( payload : any )  =>  ( { 
192206                data : payload . include ?. _count ?. select , 
193207                kind : 'include'  as  const , 
@@ -232,18 +246,24 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
232246
233247                    let  injected  =  false ; 
234248                    if  ( ! isCount )  { 
249+                         // regular select/include injection 
235250                        injected  =  await  this . injectBaseFieldSelect ( model ,  field ,  fieldValue ,  args ,  kind ) ; 
236251                        if  ( injected )  { 
252+                             // if injected, remove the field from the original payload 
237253                            delete  data [ field ] ; 
238254                        } 
239255                    }  else  { 
256+                         // _count select/include injection, inject into an empty payload and then merge to the proper location 
240257                        const  injectTarget  =  {  [ kind ] : { }  } ; 
241258                        injected  =  await  this . injectBaseFieldSelect ( model ,  field ,  fieldValue ,  injectTarget ,  kind ,  true ) ; 
242259                        if  ( injected )  { 
260+                             // if injected, remove the field from the original payload 
243261                            delete  data [ field ] ; 
244262                            if  ( Object . keys ( data ) . length  ===  0 )  { 
263+                                 // if the original "_count" payload becomes empty, remove it 
245264                                delete  args [ kind ] [ '_count' ] ; 
246265                            } 
266+                             // finally merge the injection into the original payload 
247267                            const  merged  =  deepmerge ( args [ kind ] ,  injectTarget [ kind ] ) ; 
248268                            args [ kind ]  =  merged ; 
249269                        } 
@@ -308,7 +328,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
308328        value : any , 
309329        selectInclude : any , 
310330        context : 'select'  |  'include' , 
311-         forCount  =  false 
331+         forCount  =  false   // if the injection is for a "{ _count: { select: { field: true }   }   }" payload 
312332    )  { 
313333        const  fieldInfo  =  resolveField ( this . options . modelMeta ,  model ,  field ) ; 
314334        if  ( ! fieldInfo ?. inheritedFrom )  { 
@@ -336,6 +356,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
336356                    thisLayer [ baseRelationName ]  =  {  [ context ] : { }  } ; 
337357                } 
338358                if  ( forCount )  { 
359+                     // { _count: { select: { field: true }  }  } => { delegate_aux_[Base]: { select: { _count: { select: { field: true }  }  }  }  } 
339360                    if  ( 
340361                        ! thisLayer [ baseRelationName ] [ context ] [ '_count' ]  || 
341362                        typeof  thisLayer [ baseRelationName ] [ context ]  !==  'object' 
@@ -347,6 +368,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
347368                        {  select : {  [ field ] : value  }  } 
348369                    ) ; 
349370                }  else  { 
371+                     // { select: { field: true }  } => { delegate_aux_[Base]: { select: { field: true }  }  } 
350372                    thisLayer [ baseRelationName ] [ context ] [ field ]  =  value ; 
351373                } 
352374                break ; 
0 commit comments