88 clone ,
99 enumerate ,
1010 getFields ,
11+ getTypeDefInfo ,
1112 requireField ,
1213} from '../../cross' ;
1314import { DbClientContract , EnhancementContext } from '../../types' ;
@@ -70,6 +71,11 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
7071 const processCreatePayload = ( model : string , data : any ) => {
7172 const fields = getFields ( this . options . modelMeta , model ) ;
7273 for ( const fieldInfo of Object . values ( fields ) ) {
74+ if ( fieldInfo . isTypeDef ) {
75+ this . setDefaultValueForTypeDefData ( fieldInfo . type , data [ fieldInfo . name ] ) ;
76+ continue ;
77+ }
78+
7379 if ( fieldInfo . name in data ) {
7480 // create payload already sets field value
7581 continue ;
@@ -80,10 +86,10 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
8086 continue ;
8187 }
8288
83- const authDefaultValue = this . getDefaultValueFromAuth ( fieldInfo ) ;
84- if ( authDefaultValue !== undefined ) {
89+ const defaultValue = this . getDefaultValue ( fieldInfo ) ;
90+ if ( defaultValue !== undefined ) {
8591 // set field value extracted from `auth()`
86- this . setAuthDefaultValue ( fieldInfo , model , data , authDefaultValue ) ;
92+ this . setDefaultValueForModelData ( fieldInfo , model , data , defaultValue ) ;
8793 }
8894 }
8995 } ;
@@ -109,7 +115,7 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
109115 return newArgs ;
110116 }
111117
112- private setAuthDefaultValue ( fieldInfo : FieldInfo , model : string , data : any , authDefaultValue : unknown ) {
118+ private setDefaultValueForModelData ( fieldInfo : FieldInfo , model : string , data : any , authDefaultValue : unknown ) {
113119 if ( fieldInfo . isForeignKey && fieldInfo . relationField && fieldInfo . relationField in data ) {
114120 // if the field is a fk, and the relation field is already set, we should not override it
115121 return ;
@@ -155,7 +161,7 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
155161 return entry ?. [ 0 ] ;
156162 }
157163
158- private getDefaultValueFromAuth ( fieldInfo : FieldInfo ) {
164+ private getDefaultValue ( fieldInfo : FieldInfo ) {
159165 if ( ! this . userContext ) {
160166 throw prismaClientValidationError (
161167 this . prisma ,
@@ -165,4 +171,34 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
165171 }
166172 return fieldInfo . defaultValueProvider ?.( this . userContext ) ;
167173 }
174+
175+ private setDefaultValueForTypeDefData ( type : string , data : any ) {
176+ if ( ! data || ( typeof data !== 'object' && ! Array . isArray ( data ) ) ) {
177+ return ;
178+ }
179+
180+ const typeDef = getTypeDefInfo ( this . options . modelMeta , type ) ;
181+ if ( ! typeDef ) {
182+ return ;
183+ }
184+
185+ enumerate ( data ) . forEach ( ( item ) => {
186+ if ( ! item || typeof item !== 'object' ) {
187+ return ;
188+ }
189+
190+ for ( const fieldInfo of Object . values ( typeDef . fields ) ) {
191+ if ( fieldInfo . isTypeDef ) {
192+ // recurse
193+ this . setDefaultValueForTypeDefData ( fieldInfo . type , item [ fieldInfo . name ] ) ;
194+ } else if ( ! ( fieldInfo . name in item ) ) {
195+ // set default value if the payload doesn't set the field
196+ const defaultValue = this . getDefaultValue ( fieldInfo ) ;
197+ if ( defaultValue !== undefined ) {
198+ item [ fieldInfo . name ] = defaultValue ;
199+ }
200+ }
201+ }
202+ } ) ;
203+ }
168204}
0 commit comments