diff --git a/packages/schema/src/plugins/enhancer/enhance/index.ts b/packages/schema/src/plugins/enhancer/enhance/index.ts index 8bc1c393..eaaa198a 100644 --- a/packages/schema/src/plugins/enhancer/enhance/index.ts +++ b/packages/schema/src/plugins/enhancer/enhance/index.ts @@ -110,22 +110,25 @@ export class EnhancerGenerator { const prismaImport = getPrismaClientImportSpec(this.outDir, this.options); let prismaTypesFixed = false; - let resultPrismaImport = prismaImport; + let resultPrismaTypeImport = prismaImport; if (this.needsLogicalClient) { prismaTypesFixed = true; - resultPrismaImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`; + resultPrismaTypeImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`; const result = await this.generateLogicalPrisma(); dmmf = result.dmmf; } // reexport PrismaClient types (original or fixed) - const prismaDts = this.project.createSourceFile( + const modelsDts = this.project.createSourceFile( path.join(this.outDir, 'models.d.ts'), - `export type * from '${resultPrismaImport}';`, + `export type * from '${resultPrismaTypeImport}';`, { overwrite: true } ); - await prismaDts.save(); + await modelsDts.save(); + + // reexport values from the original PrismaClient (enums, etc.) + fs.writeFileSync(path.join(this.outDir, 'models.js'), `module.exports = require('${prismaImport}');`); const authDecl = getAuthDecl(getDataModelAndTypeDefs(this.model)); const authTypes = authDecl ? generateAuthType(this.model, authDecl) : ''; @@ -151,7 +154,7 @@ ${ ${ prismaTypesFixed - ? this.createLogicalPrismaImports(prismaImport, resultPrismaImport, target) + ? this.createLogicalPrismaImports(prismaImport, resultPrismaTypeImport, target) : this.createSimplePrismaImports(prismaImport, target) } diff --git a/tests/regression/tests/issue-2065.test.ts b/tests/regression/tests/issue-2065.test.ts new file mode 100644 index 00000000..cf186b4f --- /dev/null +++ b/tests/regression/tests/issue-2065.test.ts @@ -0,0 +1,31 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('issue [...]', () => { + it('regression', async () => { + const { zodSchemas } = await loadSchema( + ` + enum FooType { + Bar + Baz + } + + type Meta { + test String? + } + + model Foo { + id String @id @db.Uuid @default(uuid()) + type FooType + meta Meta @json + + @@validate(type == Bar, "FooType must be Bar") + } + `, + { + provider: 'postgresql', + pushDb: false, + } + ); + expect(zodSchemas.models.FooSchema).toBeTruthy(); + }); +}); diff --git a/tests/regression/tests/issue-foo.test.ts b/tests/regression/tests/issue-foo.test.ts new file mode 100644 index 00000000..19cf246e --- /dev/null +++ b/tests/regression/tests/issue-foo.test.ts @@ -0,0 +1,36 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('issue [...]', () => { + it('regression', async () => { + const { zodSchemas } = await loadSchema( + ` + enum FooType { + Bar + Baz + } + type Meta { + test String? + } + model Foo { + id String @id @db.Uuid @default(uuid()) + type FooType + meta Meta @json + @@validate(type == Bar, "FooType must be Bar") + } + `, + { + provider: 'postgresql', + pushDb: false, + } + ); + expect( + zodSchemas.models.FooSchema.safeParse({ + id: '123e4567-e89b-12d3-a456-426614174000', + type: 'Bar', + meta: { test: 'test' }, + }) + ).toMatchObject({ + success: true, + }); + }); +});