@@ -121,11 +121,13 @@ export class EnhancerGenerator {
121
121
}
122
122
123
123
// reexport PrismaClient types (original or fixed)
124
- const modelsTs = this . project . createSourceFile (
125
- path . join ( this . outDir , 'models.ts' ) ,
126
- `export * from '${ resultPrismaTypeImport } ';` ,
127
- { overwrite : true }
128
- ) ;
124
+ const modelsTsContent = `export * from '${ resultPrismaTypeImport } ';${
125
+ this . isNewPrismaClientGenerator ? "\nexport * from './json-types';" : ''
126
+ } `;
127
+
128
+ const modelsTs = this . project . createSourceFile ( path . join ( this . outDir , 'models.ts' ) , modelsTsContent , {
129
+ overwrite : true ,
130
+ } ) ;
129
131
this . saveSourceFile ( modelsTs ) ;
130
132
131
133
const authDecl = getAuthDecl ( getDataModelAndTypeDefs ( this . model ) ) ;
@@ -241,7 +243,7 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh
241
243
private createLogicalPrismaImports ( prismaImport : string , prismaClientImport : string , target : string ) {
242
244
const prismaTargetImport = target === 'edge' ? `${ prismaImport } /edge` : prismaImport ;
243
245
return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${ prismaTargetImport } ';
244
- import type { InternalArgs, DynamicClientExtensionThis } from '${ prismaImport } /runtime/library';
246
+ import type { InternalArgs, DynamicClientExtensionThis } from '@prisma/client /runtime/library';
245
247
import type * as _P from '${ prismaClientImport } ';
246
248
import type { Prisma, PrismaClient } from '${ prismaClientImport } ';
247
249
export type { PrismaClient };
@@ -490,6 +492,14 @@ export type Enhanced<Client> =
490
492
private async processClientTypesNewPrismaGenerator ( prismaClientDir : string , delegateInfo : DelegateInfo ) {
491
493
const project = new Project ( ) ;
492
494
495
+ // Create a shared file for all JSON fields type definitions
496
+ const jsonFieldsFile = project . createSourceFile ( path . join ( this . outDir , 'json-types.ts' ) , undefined , {
497
+ overwrite : true ,
498
+ } ) ;
499
+
500
+ this . generateExtraTypes ( jsonFieldsFile ) ;
501
+ await saveSourceFile ( jsonFieldsFile ) ;
502
+
493
503
for ( const d of this . model . declarations . filter ( isDataModel ) ) {
494
504
const fileName = `${ prismaClientDir } /models/${ d . name } .ts` ;
495
505
const sf = project . addSourceFileAtPath ( fileName ) ;
@@ -502,6 +512,23 @@ export type Enhanced<Client> =
502
512
throw new PluginError ( name , `Unexpected syntax list structure in ${ fileName } ` ) ;
503
513
}
504
514
515
+ sfNew . addStatements ( 'import $Types = runtime.Types;' ) ;
516
+
517
+ // Add import for json-types if this model has JSON type fields
518
+ const modelWithJsonFields = this . modelsWithJsonTypeFields . find ( ( m ) => m . name === d . name ) ;
519
+ if ( modelWithJsonFields ) {
520
+ // Get the specific types that are used in this model
521
+ const getTypedJsonFields = ( model : DataModel ) => {
522
+ return model . fields . filter ( ( f ) => isTypeDef ( f . type . reference ?. ref ) ) ;
523
+ } ;
524
+ const jsonFieldTypes = getTypedJsonFields ( modelWithJsonFields ) ;
525
+ const typeNames = [ ...new Set ( jsonFieldTypes . map ( ( field ) => field . type . reference ! . $refText ) ) ] ;
526
+
527
+ if ( typeNames . length > 0 ) {
528
+ sfNew . addStatements ( `import type { ${ typeNames . join ( ', ' ) } } from "../../json-types";` ) ;
529
+ }
530
+ }
531
+
505
532
syntaxList . getChildren ( ) . forEach ( ( node ) => {
506
533
if ( Node . isInterfaceDeclaration ( node ) ) {
507
534
sfNew . addInterface ( this . transformInterface ( node , delegateInfo ) ) ;
@@ -913,7 +940,7 @@ export type Enhanced<Client> =
913
940
return source ;
914
941
}
915
942
916
- private async generateExtraTypes ( sf : SourceFile ) {
943
+ private generateExtraTypes ( sf : SourceFile ) {
917
944
for ( const decl of this . model . declarations ) {
918
945
if ( isTypeDef ( decl ) ) {
919
946
generateTypeDefType ( sf , decl ) ;
0 commit comments