@@ -70,6 +70,9 @@ export class EnhancerGenerator {
70
70
// Regex patterns for matching input/output types for models with JSON type fields
71
71
private readonly modelsWithJsonTypeFieldsInputOutputPattern : RegExp [ ] ;
72
72
73
+ // a mapping from shortened names to full names
74
+ private reversedShortNameMap = new Map < string , string > ( ) ;
75
+
73
76
constructor (
74
77
private readonly model : Model ,
75
78
private readonly options : PluginOptions ,
@@ -322,7 +325,7 @@ export type Enhanced<Client> =
322
325
323
326
// calculate a relative output path to output the logical prisma client into enhancer's output dir
324
327
const prismaClientOutDir = path . join ( path . relative ( zmodelDir , this . outDir ) , LOGICAL_CLIENT_GENERATION_PATH ) ;
325
- await prismaGenerator . generate ( {
328
+ const generateResult = await prismaGenerator . generate ( {
326
329
provider : '@internal' , // doesn't matter
327
330
schemaPath : this . options . schemaPath ,
328
331
output : logicalPrismaFile ,
@@ -331,6 +334,11 @@ export type Enhanced<Client> =
331
334
customAttributesAsComments : true ,
332
335
} ) ;
333
336
337
+ // reverse direction of shortNameMap and store for future lookup
338
+ this . reversedShortNameMap = new Map < string , string > (
339
+ Array . from ( generateResult . shortNameMap . entries ( ) ) . map ( ( [ key , value ] ) => [ value , key ] )
340
+ ) ;
341
+
334
342
// generate the prisma client
335
343
336
344
// only run prisma client generator for the logical schema
@@ -390,7 +398,7 @@ export type Enhanced<Client> =
390
398
const createInputPattern = new RegExp ( `^(.+?)(Unchecked)?Create.*Input$` ) ;
391
399
for ( const inputType of dmmf . schema . inputObjectTypes . prisma ) {
392
400
const match = inputType . name . match ( createInputPattern ) ;
393
- const modelName = match ?. [ 1 ] ;
401
+ const modelName = this . resolveName ( match ?. [ 1 ] ) ;
394
402
if ( modelName ) {
395
403
const dataModel = this . model . declarations . find (
396
404
( d ) : d is DataModel => isDataModel ( d ) && d . name === modelName
@@ -673,7 +681,7 @@ export type Enhanced<Client> =
673
681
674
682
const match = typeName . match ( concreteCreateUpdateInputRegex ) ;
675
683
if ( match ) {
676
- const modelName = match [ 1 ] ;
684
+ const modelName = this . resolveName ( match [ 1 ] ) ;
677
685
const dataModel = this . model . declarations . find (
678
686
( d ) : d is DataModel => isDataModel ( d ) && d . name === modelName
679
687
) ;
@@ -724,8 +732,9 @@ export type Enhanced<Client> =
724
732
return source ;
725
733
}
726
734
727
- const nameTuple = match [ 3 ] ; // [modelName]_[relationFieldName]_[concreteModelName]
728
- const [ modelName , relationFieldName , _ ] = nameTuple . split ( '_' ) ;
735
+ // [modelName]_[relationFieldName]_[concreteModelName]
736
+ const nameTuple = this . resolveName ( match [ 3 ] , true ) ;
737
+ const [ modelName , relationFieldName , _ ] = nameTuple ! . split ( '_' ) ;
729
738
730
739
const fieldDef = this . findNamedProperty ( typeAlias , relationFieldName ) ;
731
740
if ( fieldDef ) {
@@ -769,13 +778,28 @@ export type Enhanced<Client> =
769
778
return source ;
770
779
}
771
780
781
+ // resolves a potentially shortened name back to the original
782
+ private resolveName ( name : string | undefined , withDelegateAuxPrefix = false ) {
783
+ if ( ! name ) {
784
+ return name ;
785
+ }
786
+ const shortNameLookupKey = withDelegateAuxPrefix ? `${ DELEGATE_AUX_RELATION_PREFIX } _${ name } ` : name ;
787
+ if ( this . reversedShortNameMap . has ( shortNameLookupKey ) ) {
788
+ name = this . reversedShortNameMap . get ( shortNameLookupKey ) ! ;
789
+ if ( withDelegateAuxPrefix ) {
790
+ name = name . substring ( DELEGATE_AUX_RELATION_PREFIX . length + 1 ) ;
791
+ }
792
+ }
793
+ return name ;
794
+ }
795
+
772
796
private fixDefaultAuthType ( typeAlias : TypeAliasDeclaration , source : string ) {
773
797
const match = typeAlias . getName ( ) . match ( this . modelsWithAuthInDefaultCreateInputPattern ) ;
774
798
if ( ! match ) {
775
799
return source ;
776
800
}
777
801
778
- const modelName = match [ 1 ] ;
802
+ const modelName = this . resolveName ( match [ 1 ] ) ;
779
803
const dataModel = this . model . declarations . find ( ( d ) : d is DataModel => isDataModel ( d ) && d . name === modelName ) ;
780
804
if ( dataModel ) {
781
805
for ( const fkField of dataModel . fields . filter ( ( f ) => f . attributes . some ( isDefaultWithAuth ) ) ) {
@@ -831,7 +855,7 @@ export type Enhanced<Client> =
831
855
continue ;
832
856
}
833
857
// first capture group is the model name
834
- const modelName = match [ 1 ] ;
858
+ const modelName = this . resolveName ( match [ 1 ] ) ;
835
859
const model = this . modelsWithJsonTypeFields . find ( ( m ) => m . name === modelName ) ;
836
860
const fieldsToFix = getTypedJsonFields ( model ! ) ;
837
861
for ( const field of fieldsToFix ) {
0 commit comments