22import deepmerge , { type ArrayMergeOptions } from 'deepmerge' ;
33import { isPlainObject } from 'is-plain-object' ;
44import { lowerCaseFirst } from 'lower-case-first' ;
5+ import traverse from 'traverse' ;
56import { DELEGATE_AUX_RELATION_PREFIX } from '../../constants' ;
67import {
78 FieldInfo ,
@@ -77,6 +78,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
7778 this . injectWhereHierarchy ( model , args ?. where ) ;
7879 this . injectSelectIncludeHierarchy ( model , args ) ;
7980
81+ // discriminator field is needed during post process to determine the
82+ // actual concrete model type
83+ this . ensureDiscriminatorSelection ( model , args ) ;
84+
8085 if ( args . orderBy ) {
8186 // `orderBy` may contain fields from base types
8287 this . injectWhereHierarchy ( this . model , args . orderBy ) ;
@@ -94,6 +99,23 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
9499 }
95100 }
96101
102+ private ensureDiscriminatorSelection ( model : string , args : any ) {
103+ const modelInfo = getModelInfo ( this . options . modelMeta , model ) ;
104+ if ( ! modelInfo ?. discriminator ) {
105+ return ;
106+ }
107+
108+ if ( args . select && typeof args . select === 'object' ) {
109+ args . select [ modelInfo . discriminator ] = true ;
110+ return ;
111+ }
112+
113+ if ( args . omit && typeof args . omit === 'object' ) {
114+ args . omit [ modelInfo . discriminator ] = false ;
115+ return ;
116+ }
117+ }
118+
97119 private injectWhereHierarchy ( model : string , where : any ) {
98120 if ( ! where || ! isPlainObject ( where ) ) {
99121 return ;
@@ -337,6 +359,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
337359 ) ;
338360 }
339361
362+ this . sanitizeMutationPayload ( args . data ) ;
363+
340364 if ( isDelegateModel ( this . options . modelMeta , this . model ) ) {
341365 throw prismaClientValidationError (
342366 this . prisma ,
@@ -352,6 +376,24 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
352376 return this . doCreate ( this . prisma , this . model , args ) ;
353377 }
354378
379+ private sanitizeMutationPayload ( data : any ) {
380+ if ( ! data ) {
381+ return ;
382+ }
383+
384+ const prisma = this . prisma ;
385+ const prismaModule = this . options . prismaModule ;
386+ traverse ( data ) . forEach ( function ( ) {
387+ if ( this . key ?. startsWith ( DELEGATE_AUX_RELATION_PREFIX ) ) {
388+ throw prismaClientValidationError (
389+ prisma ,
390+ prismaModule ,
391+ `Auxiliary relation field "${ this . key } " cannot be set directly`
392+ ) ;
393+ }
394+ } ) ;
395+ }
396+
355397 override createMany ( args : { data : any ; skipDuplicates ?: boolean } ) : Promise < { count : number } > {
356398 if ( ! args ) {
357399 throw prismaClientValidationError ( this . prisma , this . options . prismaModule , 'query argument is required' ) ;
@@ -364,6 +406,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
364406 ) ;
365407 }
366408
409+ this . sanitizeMutationPayload ( args . data ) ;
410+
367411 if ( ! this . involvesDelegateModel ( this . model ) ) {
368412 return super . createMany ( args ) ;
369413 }
@@ -403,6 +447,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
403447 ) ;
404448 }
405449
450+ this . sanitizeMutationPayload ( args . data ) ;
451+
406452 if ( ! this . involvesDelegateModel ( this . model ) ) {
407453 return super . createManyAndReturn ( args ) ;
408454 }
@@ -589,6 +635,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
589635 ) ;
590636 }
591637
638+ this . sanitizeMutationPayload ( args . data ) ;
639+
592640 if ( ! this . involvesDelegateModel ( this . model ) ) {
593641 return super . update ( args ) ;
594642 }
@@ -608,6 +656,8 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
608656 ) ;
609657 }
610658
659+ this . sanitizeMutationPayload ( args . data ) ;
660+
611661 if ( ! this . involvesDelegateModel ( this . model ) ) {
612662 return super . updateMany ( args ) ;
613663 }
@@ -635,6 +685,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
635685 ) ;
636686 }
637687
688+ this . sanitizeMutationPayload ( args . update ) ;
689+ this . sanitizeMutationPayload ( args . create ) ;
690+
638691 if ( isDelegateModel ( this . options . modelMeta , this . model ) ) {
639692 throw prismaClientValidationError (
640693 this . prisma ,
0 commit comments