Skip to content

Commit 2f8fbc1

Browse files
committed
fixes
1 parent a5257ee commit 2f8fbc1

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

packages/schema/src/plugins/zod/transformer.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
import { 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';
44
import { checkModelHasModelRelation, findModelByName, isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers';
55
import { supportCreateMany, type DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma';
66
import 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

Comments
 (0)