Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make ".zenstack/models" reexport the entire original PrismaClient module #2070

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : '';
Expand All @@ -151,7 +154,7 @@ ${

${
prismaTypesFixed
? this.createLogicalPrismaImports(prismaImport, resultPrismaImport, target)
? this.createLogicalPrismaImports(prismaImport, resultPrismaTypeImport, target)
: this.createSimplePrismaImports(prismaImport, target)
}

Expand Down
31 changes: 31 additions & 0 deletions tests/regression/tests/issue-2065.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
36 changes: 36 additions & 0 deletions tests/regression/tests/issue-foo.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
Loading