@@ -15,18 +15,22 @@ import {
1515 isDelegateModel ,
1616 resolveField ,
1717} from '../../cross' ;
18- import type { CrudContract , DbClientContract } from '../../types' ;
18+ import type { CrudContract , DbClientContract , EnhancementContext } from '../../types' ;
1919import type { InternalEnhancementOptions } from './create-enhancement' ;
2020import { Logger } from './logger' ;
2121import { DefaultPrismaProxyHandler , makeProxy } from './proxy' ;
2222import { QueryUtils } from './query-utils' ;
2323import { formatObject , prismaClientValidationError } from './utils' ;
2424
25- export function withDelegate < DbClient extends object > ( prisma : DbClient , options : InternalEnhancementOptions ) : DbClient {
25+ export function withDelegate < DbClient extends object > (
26+ prisma : DbClient ,
27+ options : InternalEnhancementOptions ,
28+ context : EnhancementContext | undefined
29+ ) : DbClient {
2630 return makeProxy (
2731 prisma ,
2832 options . modelMeta ,
29- ( _prisma , model ) => new DelegateProxyHandler ( _prisma as DbClientContract , model , options ) ,
33+ ( _prisma , model ) => new DelegateProxyHandler ( _prisma as DbClientContract , model , options , context ) ,
3034 'delegate'
3135 ) ;
3236}
@@ -35,7 +39,12 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
3539 private readonly logger : Logger ;
3640 private readonly queryUtils : QueryUtils ;
3741
38- constructor ( prisma : DbClientContract , model : string , options : InternalEnhancementOptions ) {
42+ constructor (
43+ prisma : DbClientContract ,
44+ model : string ,
45+ options : InternalEnhancementOptions ,
46+ private readonly context : EnhancementContext | undefined
47+ ) {
3948 super ( prisma , model , options ) ;
4049 this . logger = new Logger ( prisma ) ;
4150 this . queryUtils = new QueryUtils ( prisma , this . options ) ;
@@ -76,7 +85,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
7685 args = args ? clone ( args ) : { } ;
7786
7887 this . injectWhereHierarchy ( model , args ?. where ) ;
79- this . injectSelectIncludeHierarchy ( model , args ) ;
88+ await this . injectSelectIncludeHierarchy ( model , args ) ;
8089
8190 // discriminator field is needed during post process to determine the
8291 // actual concrete model type
@@ -166,7 +175,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
166175 } ) ;
167176 }
168177
169- private injectSelectIncludeHierarchy ( model : string , args : any ) {
178+ private async injectSelectIncludeHierarchy ( model : string , args : any ) {
170179 if ( ! args || typeof args !== 'object' ) {
171180 return ;
172181 }
@@ -186,7 +195,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
186195 // make sure the payload is an object
187196 args [ kind ] [ field ] = { } ;
188197 }
189- this . injectSelectIncludeHierarchy ( fieldInfo . type , args [ kind ] [ field ] ) ;
198+ await this . injectSelectIncludeHierarchy ( fieldInfo . type , args [ kind ] [ field ] ) ;
190199 }
191200 }
192201
@@ -208,7 +217,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
208217 // make sure the payload is an object
209218 args [ kind ] [ field ] = nextValue = { } ;
210219 }
211- this . injectSelectIncludeHierarchy ( fieldInfo . type , nextValue ) ;
220+ await this . injectSelectIncludeHierarchy ( fieldInfo . type , nextValue ) ;
212221 }
213222 }
214223 }
@@ -220,11 +229,11 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
220229 this . injectBaseIncludeRecursively ( model , args ) ;
221230
222231 // include sub models downwards
223- this . injectConcreteIncludeRecursively ( model , args ) ;
232+ await this . injectConcreteIncludeRecursively ( model , args ) ;
224233 }
225234 }
226235
227- private buildSelectIncludeHierarchy ( model : string , args : any ) {
236+ private async buildSelectIncludeHierarchy ( model : string , args : any ) {
228237 args = clone ( args ) ;
229238 const selectInclude : any = this . extractSelectInclude ( args ) || { } ;
230239
@@ -248,7 +257,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
248257
249258 if ( ! selectInclude . select ) {
250259 this . injectBaseIncludeRecursively ( model , selectInclude ) ;
251- this . injectConcreteIncludeRecursively ( model , selectInclude ) ;
260+ await this . injectConcreteIncludeRecursively ( model , selectInclude ) ;
252261 }
253262 return selectInclude ;
254263 }
@@ -319,7 +328,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
319328 this . injectBaseIncludeRecursively ( base . name , selectInclude . include [ baseRelationName ] ) ;
320329 }
321330
322- private injectConcreteIncludeRecursively ( model : string , selectInclude : any ) {
331+ private async injectConcreteIncludeRecursively ( model : string , selectInclude : any ) {
323332 const modelInfo = getModelInfo ( this . options . modelMeta , model ) ;
324333 if ( ! modelInfo ) {
325334 return ;
@@ -333,13 +342,27 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
333342 for ( const subModel of subModels ) {
334343 // include sub model relation field
335344 const subRelationName = this . makeAuxRelationName ( subModel ) ;
345+ const includePayload : any = { } ;
346+
347+ if ( this . options . processIncludeRelationPayload ) {
348+ // use the callback in options to process the include payload, so enhancements
349+ // like 'policy' can do extra work (e.g., inject policy rules)
350+ await this . options . processIncludeRelationPayload (
351+ this . prisma ,
352+ subModel . name ,
353+ includePayload ,
354+ this . options ,
355+ this . context
356+ ) ;
357+ }
358+
336359 if ( selectInclude . select ) {
337- selectInclude . include = { [ subRelationName ] : { } , ...selectInclude . select } ;
360+ selectInclude . include = { [ subRelationName ] : includePayload , ...selectInclude . select } ;
338361 delete selectInclude . select ;
339362 } else {
340- selectInclude . include = { [ subRelationName ] : { } , ...selectInclude . include } ;
363+ selectInclude . include = { [ subRelationName ] : includePayload , ...selectInclude . include } ;
341364 }
342- this . injectConcreteIncludeRecursively ( subModel . name , selectInclude . include [ subRelationName ] ) ;
365+ await this . injectConcreteIncludeRecursively ( subModel . name , selectInclude . include [ subRelationName ] ) ;
343366 }
344367 }
345368
@@ -480,7 +503,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
480503 args = clone ( args ) ;
481504
482505 await this . injectCreateHierarchy ( model , args ) ;
483- this . injectSelectIncludeHierarchy ( model , args ) ;
506+ await this . injectSelectIncludeHierarchy ( model , args ) ;
484507
485508 if ( this . options . logPrismaQuery ) {
486509 this . logger . info ( `[delegate] \`create\` ${ this . getModelName ( model ) } : ${ formatObject ( args ) } ` ) ;
@@ -702,7 +725,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
702725
703726 args = clone ( args ) ;
704727 this . injectWhereHierarchy ( this . model , ( args as any ) ?. where ) ;
705- this . injectSelectIncludeHierarchy ( this . model , args ) ;
728+ await this . injectSelectIncludeHierarchy ( this . model , args ) ;
706729 if ( args . create ) {
707730 this . doProcessCreatePayload ( this . model , args . create ) ;
708731 }
@@ -721,7 +744,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
721744 args = clone ( args ) ;
722745
723746 await this . injectUpdateHierarchy ( db , model , args ) ;
724- this . injectSelectIncludeHierarchy ( model , args ) ;
747+ await this . injectSelectIncludeHierarchy ( model , args ) ;
725748
726749 if ( this . options . logPrismaQuery ) {
727750 this . logger . info ( `[delegate] \`update\` ${ this . getModelName ( model ) } : ${ formatObject ( args ) } ` ) ;
@@ -915,7 +938,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
915938 }
916939
917940 return this . queryUtils . transaction ( this . prisma , async ( tx ) => {
918- const selectInclude = this . buildSelectIncludeHierarchy ( this . model , args ) ;
941+ const selectInclude = await this . buildSelectIncludeHierarchy ( this . model , args ) ;
919942
920943 // make sure id fields are selected
921944 const idFields = this . getIdFields ( this . model ) ;
@@ -967,6 +990,7 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
967990
968991 private async doDelete ( db : CrudContract , model : string , args : any ) : Promise < unknown > {
969992 this . injectWhereHierarchy ( model , args . where ) ;
993+ await this . injectSelectIncludeHierarchy ( model , args ) ;
970994
971995 if ( this . options . logPrismaQuery ) {
972996 this . logger . info ( `[delegate] \`delete\` ${ this . getModelName ( model ) } : ${ formatObject ( args ) } ` ) ;
0 commit comments