@@ -64,6 +64,12 @@ export class EnhancerGenerator {
64
64
// names for models that use `auth()` in `@default` attribute
65
65
private readonly modelsWithAuthInDefaultCreateInputPattern : RegExp ;
66
66
67
+ // models with JSON type fields
68
+ private readonly modelsWithJsonTypeFields : DataModel [ ] ;
69
+
70
+ // Regex patterns for matching input/output types for models with JSON type fields
71
+ private readonly modelsWithJsonTypeFieldsInputOutputPattern : RegExp [ ] ;
72
+
67
73
constructor (
68
74
private readonly model : Model ,
69
75
private readonly options : PluginOptions ,
@@ -73,9 +79,27 @@ export class EnhancerGenerator {
73
79
const modelsWithAuthInDefault = this . model . declarations . filter (
74
80
( d ) : d is DataModel => isDataModel ( d ) && d . fields . some ( ( f ) => f . attributes . some ( isDefaultWithAuth ) )
75
81
) ;
82
+
76
83
this . modelsWithAuthInDefaultCreateInputPattern = new RegExp (
77
84
`^(${ modelsWithAuthInDefault . map ( ( m ) => m . name ) . join ( '|' ) } )(Unchecked)?Create.*?Input$`
78
85
) ;
86
+
87
+ this . modelsWithJsonTypeFields = this . model . declarations . filter (
88
+ ( d ) : d is DataModel => isDataModel ( d ) && d . fields . some ( ( f ) => isTypeDef ( f . type . reference ?. ref ) )
89
+ ) ;
90
+
91
+ // input/output patterns for models with json type fields
92
+ const relevantTypePatterns = [
93
+ 'GroupByOutputType' ,
94
+ '(Unchecked)?Create(\\S+?)?Input' ,
95
+ '(Unchecked)?Update(\\S+?)?Input' ,
96
+ 'CreateManyInput' ,
97
+ '(Unchecked)?UpdateMany(Mutation)?Input' ,
98
+ ] ;
99
+ // build combination regex with all models with JSON types and the above suffixes
100
+ this . modelsWithJsonTypeFieldsInputOutputPattern = this . modelsWithJsonTypeFields . map (
101
+ ( m ) => new RegExp ( `^(${ m . name } )(${ relevantTypePatterns . join ( '|' ) } )$` )
102
+ ) ;
79
103
}
80
104
81
105
async generate ( ) : Promise < { dmmf : DMMF . Document | undefined ; newPrismaClientDtsPath : string | undefined } > {
@@ -748,9 +772,6 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
748
772
}
749
773
750
774
private fixJsonFieldType ( typeAlias : TypeAliasDeclaration , source : string ) {
751
- const modelsWithTypeField = this . model . declarations . filter (
752
- ( d ) : d is DataModel => isDataModel ( d ) && d . fields . some ( ( f ) => isTypeDef ( f . type . reference ?. ref ) )
753
- ) ;
754
775
const typeName = typeAlias . getName ( ) ;
755
776
756
777
const getTypedJsonFields = ( model : DataModel ) => {
@@ -767,7 +788,7 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
767
788
} ;
768
789
769
790
// fix "$[Model]Payload" type
770
- const payloadModelMatch = modelsWithTypeField . find ( ( m ) => `$${ m . name } Payload` === typeName ) ;
791
+ const payloadModelMatch = this . modelsWithJsonTypeFields . find ( ( m ) => `$${ m . name } Payload` === typeName ) ;
771
792
if ( payloadModelMatch ) {
772
793
const scalars = typeAlias
773
794
. getDescendantsOfKind ( SyntaxKind . PropertySignature )
@@ -783,24 +804,19 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
783
804
}
784
805
785
806
// fix input/output types, "[Model]CreateInput", etc.
786
- const inputOutputModelMatch = modelsWithTypeField . find ( ( m ) => typeName . startsWith ( m . name ) ) ;
787
- if ( inputOutputModelMatch ) {
788
- const relevantTypePatterns = [
789
- 'GroupByOutputType' ,
790
- '(Unchecked)?Create(\\S+?)?Input' ,
791
- '(Unchecked)?Update(\\S+?)?Input' ,
792
- 'CreateManyInput' ,
793
- '(Unchecked)?UpdateMany(Mutation)?Input' ,
794
- ] ;
795
- const typeRegex = modelsWithTypeField . map (
796
- ( m ) => new RegExp ( `^(${ m . name } )(${ relevantTypePatterns . join ( '|' ) } )$` )
797
- ) ;
798
- if ( typeRegex . some ( ( r ) => r . test ( typeName ) ) ) {
799
- const fieldsToFix = getTypedJsonFields ( inputOutputModelMatch ) ;
800
- for ( const field of fieldsToFix ) {
801
- source = replacePrismaJson ( source , field ) ;
802
- }
807
+ for ( const pattern of this . modelsWithJsonTypeFieldsInputOutputPattern ) {
808
+ const match = typeName . match ( pattern ) ;
809
+ if ( ! match ) {
810
+ continue ;
811
+ }
812
+ // first capture group is the model name
813
+ const modelName = match [ 1 ] ;
814
+ const model = this . modelsWithJsonTypeFields . find ( ( m ) => m . name === modelName ) ;
815
+ const fieldsToFix = getTypedJsonFields ( model ! ) ;
816
+ for ( const field of fieldsToFix ) {
817
+ source = replacePrismaJson ( source , field ) ;
803
818
}
819
+ break ;
804
820
}
805
821
806
822
return source ;
0 commit comments