11import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime' ;
2+ import { invariant , upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
23import {
34 PluginError ,
45 getAttribute ,
@@ -26,7 +27,6 @@ import {
2627 type Model ,
2728} from '@zenstackhq/sdk/ast' ;
2829import { getDMMF , getPrismaClientImportSpec , getPrismaVersion , type DMMF } from '@zenstackhq/sdk/prisma' ;
29- import { invariant , upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
3030import fs from 'fs' ;
3131import path from 'path' ;
3232import semver from 'semver' ;
@@ -105,44 +105,122 @@ export class EnhancerGenerator {
105105 }
106106
107107 async generate ( ) : Promise < { dmmf : DMMF . Document | undefined ; newPrismaClientDtsPath : string | undefined } > {
108- let dmmf : DMMF . Document | undefined ;
108+ if ( this . isNewPrismaClientGenerator ) {
109+ // "prisma-client" generator
110+ return this . generateForNewClientGenerator ( ) ;
111+ } else {
112+ // "prisma-client-js" generator
113+ return this . generateForOldClientGenerator ( ) ;
114+ }
115+ }
109116
117+ // logic for "prisma-client" generator
118+ private async generateForNewClientGenerator ( ) {
119+ const needsLogicalClient = this . needsLogicalClient ;
110120 const prismaImport = getPrismaClientImportSpec ( this . outDir , this . options ) ;
111- let prismaTypesFixed = false ;
112- let resultPrismaTypeImport = prismaImport ;
113-
114- if ( this . needsLogicalClient ) {
115- prismaTypesFixed = true ;
116- resultPrismaTypeImport = LOGICAL_CLIENT_GENERATION_PATH ;
117- if ( this . isNewPrismaClientGenerator ) {
118- resultPrismaTypeImport += '/client' ;
119- }
121+ let resultPrismaBaseImport = path . dirname ( prismaImport ) ; // get to the parent folder of "client"
122+ let dmmf : DMMF . Document | undefined ;
123+
124+ if ( needsLogicalClient ) {
125+ // use logical client, note we use the parent of "client" folder here too
126+ resultPrismaBaseImport = LOGICAL_CLIENT_GENERATION_PATH ;
120127 const result = await this . generateLogicalPrisma ( ) ;
121128 dmmf = result . dmmf ;
122129 }
123130
124- // reexport PrismaClient types (original or fixed)
125- const modelsTsContent = `export * from '${ resultPrismaTypeImport } ';${
126- this . isNewPrismaClientGenerator ? "\nexport * from './json-types';" : ''
127- } `;
131+ // `models.ts` for exporting model types
132+ const modelsTsContent = [
133+ `export * from '${ resultPrismaBaseImport } /models';` ,
134+ `export * from './json-types';` ,
135+ ] . join ( '\n' ) ;
136+ const modelsTs = this . project . createSourceFile ( path . join ( this . outDir , 'models.ts' ) , modelsTsContent , {
137+ overwrite : true ,
138+ } ) ;
139+ this . saveSourceFile ( modelsTs ) ;
140+
141+ // `enums.ts` for exporting enums
142+ const enumsTs = this . project . createSourceFile (
143+ path . join ( this . outDir , 'enums.ts' ) ,
144+ `export * from '${ resultPrismaBaseImport } /enums';` ,
145+ {
146+ overwrite : true ,
147+ }
148+ ) ;
149+ this . saveSourceFile ( enumsTs ) ;
150+
151+ // `client.ts` for exporting `PrismaClient` and `Prisma` namespace
152+ const clientTs = this . project . createSourceFile (
153+ path . join ( this . outDir , 'client.ts' ) ,
154+ `export * from '${ resultPrismaBaseImport } /client';` ,
155+ {
156+ overwrite : true ,
157+ }
158+ ) ;
159+ this . saveSourceFile ( clientTs ) ;
160+
161+ // `enhance.ts` and `enhance-edge.ts`
162+ for ( const target of [ 'node' , 'edge' ] as const ) {
163+ this . generateEnhance ( prismaImport , `${ resultPrismaBaseImport } /client` , needsLogicalClient , target ) ;
164+ }
165+
166+ return {
167+ // logical dmmf if there is one
168+ dmmf,
169+ // new client generator doesn't have a barrel .d.ts file
170+ newPrismaClientDtsPath : undefined ,
171+ } ;
172+ }
173+
174+ // logic for "prisma-client-js" generator
175+ private async generateForOldClientGenerator ( ) {
176+ const needsLogicalClient = this . needsLogicalClient ;
177+ const prismaImport = getPrismaClientImportSpec ( this . outDir , this . options ) ;
178+ let resultPrismaClientImport = prismaImport ;
179+ let dmmf : DMMF . Document | undefined ;
180+
181+ if ( needsLogicalClient ) {
182+ // redirect `PrismaClient` import to the logical client
183+ resultPrismaClientImport = LOGICAL_CLIENT_GENERATION_PATH ;
184+ const result = await this . generateLogicalPrisma ( ) ;
185+ dmmf = result . dmmf ;
186+ }
128187
188+ // `models.ts` for exporting model types
189+ const modelsTsContent = `export * from '${ resultPrismaClientImport } ';` ;
129190 const modelsTs = this . project . createSourceFile ( path . join ( this . outDir , 'models.ts' ) , modelsTsContent , {
130191 overwrite : true ,
131192 } ) ;
132193 this . saveSourceFile ( modelsTs ) ;
133194
195+ // `enhance.ts` and `enhance-edge.ts`
196+ for ( const target of [ 'node' , 'edge' ] as const ) {
197+ this . generateEnhance ( prismaImport , resultPrismaClientImport , needsLogicalClient , target ) ;
198+ }
199+
200+ return {
201+ // logical dmmf if there is one
202+ dmmf,
203+ newPrismaClientDtsPath : needsLogicalClient
204+ ? path . resolve ( this . outDir , LOGICAL_CLIENT_GENERATION_PATH , 'index.d.ts' )
205+ : undefined ,
206+ } ;
207+ }
208+
209+ private generateEnhance (
210+ prismaImport : string ,
211+ prismaClientImport : string ,
212+ needsLogicalClient : boolean ,
213+ target : 'node' | 'edge'
214+ ) {
134215 const authDecl = getAuthDecl ( getDataModelAndTypeDefs ( this . model ) ) ;
135216 const authTypes = authDecl ? generateAuthType ( this . model , authDecl ) : '' ;
136217 const authTypeParam = authDecl ? `auth.${ authDecl . name } ` : 'AuthUser' ;
137-
138218 const checkerTypes = this . generatePermissionChecker ? generateCheckerType ( this . model ) : '' ;
139219
140- for ( const target of [ 'node' , 'edge' ] ) {
141- // generate separate `enhance()` for node and edge runtime
142- const outFile = target === 'node' ? 'enhance.ts' : 'enhance-edge.ts' ;
143- const enhanceTs = this . project . createSourceFile (
144- path . join ( this . outDir , outFile ) ,
145- `/* eslint-disable */
220+ const outFile = target === 'node' ? 'enhance.ts' : 'enhance-edge.ts' ;
221+ const enhanceTs = this . project . createSourceFile (
222+ path . join ( this . outDir , outFile ) ,
223+ `/* eslint-disable */
146224import { type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
147225import { createEnhancement } from '@zenstackhq/runtime/enhancements/${ target } ';
148226import modelMeta from './model-meta';
154232}
155233
156234${
157- prismaTypesFixed
158- ? this . createLogicalPrismaImports ( prismaImport , resultPrismaTypeImport , target )
235+ needsLogicalClient
236+ ? this . createLogicalPrismaImports ( prismaImport , prismaClientImport , target )
159237 : this . createSimplePrismaImports ( prismaImport , target )
160238}
161239
@@ -164,23 +242,15 @@ ${authTypes}
164242${ checkerTypes }
165243
166244${
167- prismaTypesFixed
245+ needsLogicalClient
168246 ? this . createLogicalPrismaEnhanceFunction ( authTypeParam )
169247 : this . createSimplePrismaEnhanceFunction ( authTypeParam )
170248}
171249 ` ,
172- { overwrite : true }
173- ) ;
174-
175- this . saveSourceFile ( enhanceTs ) ;
176- }
250+ { overwrite : true }
251+ ) ;
177252
178- return {
179- dmmf,
180- newPrismaClientDtsPath : prismaTypesFixed
181- ? path . resolve ( this . outDir , LOGICAL_CLIENT_GENERATION_PATH , 'index.d.ts' )
182- : undefined ,
183- } ;
253+ this . saveSourceFile ( enhanceTs ) ;
184254 }
185255
186256 private getZodImport ( ) {
210280 return normalizedRelative ( this . outDir , zodAbsPath ) ;
211281 }
212282
213- private createSimplePrismaImports ( prismaImport : string , target : string ) {
283+ private createSimplePrismaImports ( prismaImport : string , target : string | undefined ) {
214284 const prismaTargetImport = target === 'edge' ? `${ prismaImport } /edge` : prismaImport ;
215285
216286 return `import { Prisma, type PrismaClient } from '${ prismaTargetImport } ';
@@ -241,10 +311,10 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh
241311 ` ;
242312 }
243313
244- private createLogicalPrismaImports ( prismaImport : string , prismaClientImport : string , target : string ) {
314+ private createLogicalPrismaImports ( prismaImport : string , prismaClientImport : string , target : string | undefined ) {
245315 const prismaTargetImport = target === 'edge' ? `${ prismaImport } /edge` : prismaImport ;
246316 const runtimeLibraryImport = this . isNewPrismaClientGenerator
247- ? // new generator has these typed only in "@prisma/client"
317+ ? // new generator has these types only in "@prisma/client"
248318 '@prisma/client/runtime/library'
249319 : // old generator has these types generated with the client
250320 `${ prismaImport } /runtime/library` ;
0 commit comments