11/* eslint-disable @typescript-eslint/ban-ts-comment */
22import { indentString , isDiscriminatorField , type PluginOptions } from '@zenstackhq/sdk' ;
3- import { DataModel , isDataModel , isEnum , isTypeDef , type Model } from '@zenstackhq/sdk/ast' ;
3+ import { DataModel , Enum , isDataModel , isEnum , isTypeDef , type Model } from '@zenstackhq/sdk/ast' ;
44import { checkModelHasModelRelation , findModelByName , isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers' ;
55import { supportCreateMany , type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma' ;
66import path from 'path' ;
@@ -17,6 +17,7 @@ export default class Transformer {
1717 models : readonly PrismaDMMF . Model [ ] ;
1818 modelOperations : PrismaDMMF . ModelMapping [ ] ;
1919 aggregateOperationSupport : AggregateOperationSupport ;
20+ enumTypes : readonly PrismaDMMF . SchemaEnum [ ] ;
2021
2122 static enumNames : string [ ] = [ ] ;
2223 static rawOpsMap : { [ name : string ] : string } = { } ;
@@ -36,6 +37,7 @@ export default class Transformer {
3637 this . models = params . models ?? [ ] ;
3738 this . modelOperations = params . modelOperations ?? [ ] ;
3839 this . aggregateOperationSupport = params . aggregateOperationSupport ?? { } ;
40+ this . enumTypes = params . enumTypes ?? [ ] ;
3941 this . project = params . project ;
4042 this . inputObjectTypes = params . inputObjectTypes ;
4143 this . zmodel = params . zmodel ;
@@ -51,21 +53,37 @@ export default class Transformer {
5153 }
5254
5355 async generateEnumSchemas ( ) {
54- const enums = this . zmodel . declarations . filter ( isEnum ) ;
55- for ( const enumDecl of enums ) {
56+ const generated : string [ ] = [ ] ;
57+
58+ // generate for enums in DMMF
59+ for ( const enumType of this . enumTypes ) {
60+ const name = upperCaseFirst ( enumType . name ) ;
61+ const filePath = path . join ( Transformer . outputPath , `enums/${ name } .schema.ts` ) ;
62+ const content = `/* eslint-disable */\n${ this . generateImportZodStatement ( ) } \n${ this . generateExportSchemaStatement (
63+ `${ name } ` ,
64+ `z.enum(${ JSON . stringify ( enumType . values ) } )`
65+ ) } `;
66+ this . sourceFiles . push ( this . project . createSourceFile ( filePath , content , { overwrite : true } ) ) ;
67+ generated . push ( enumType . name ) ;
68+ }
69+
70+ // enums not referenced by data models are not in DMMF, deal with them separately
71+ const extraEnums = this . zmodel . declarations . filter ( ( d ) : d is Enum => isEnum ( d ) && ! generated . includes ( d . name ) ) ;
72+ for ( const enumDecl of extraEnums ) {
5673 const name = upperCaseFirst ( enumDecl . name ) ;
5774 const filePath = path . join ( Transformer . outputPath , `enums/${ name } .schema.ts` ) ;
5875 const content = `/* eslint-disable */\n${ this . generateImportZodStatement ( ) } \n${ this . generateExportSchemaStatement (
5976 `${ name } ` ,
6077 `z.enum(${ JSON . stringify ( enumDecl . fields . map ( ( f ) => f . name ) ) } )`
6178 ) } `;
6279 this . sourceFiles . push ( this . project . createSourceFile ( filePath , content , { overwrite : true } ) ) ;
80+ generated . push ( enumDecl . name ) ;
6381 }
6482
6583 this . sourceFiles . push (
6684 this . project . createSourceFile (
6785 path . join ( Transformer . outputPath , `enums/index.ts` ) ,
68- enums . map ( ( enumDecl ) => `export * from './${ upperCaseFirst ( enumDecl . name ) } .schema';` ) . join ( '\n' ) ,
86+ generated . map ( ( name ) => `export * from './${ upperCaseFirst ( name ) } .schema';` ) . join ( '\n' ) ,
6987 { overwrite : true }
7088 )
7189 ) ;
0 commit comments