Skip to content

Commit 1ee6a5a

Browse files
authored
fix: make ".zenstack/models" reexport the entire original PrismaClient module (#2070)
2 parents 4608cb9 + 4c69a7c commit 1ee6a5a

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

packages/schema/src/plugins/enhancer/enhance/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,25 @@ export class EnhancerGenerator {
110110

111111
const prismaImport = getPrismaClientImportSpec(this.outDir, this.options);
112112
let prismaTypesFixed = false;
113-
let resultPrismaImport = prismaImport;
113+
let resultPrismaTypeImport = prismaImport;
114114

115115
if (this.needsLogicalClient) {
116116
prismaTypesFixed = true;
117-
resultPrismaImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`;
117+
resultPrismaTypeImport = `${LOGICAL_CLIENT_GENERATION_PATH}/index-fixed`;
118118
const result = await this.generateLogicalPrisma();
119119
dmmf = result.dmmf;
120120
}
121121

122122
// reexport PrismaClient types (original or fixed)
123-
const prismaDts = this.project.createSourceFile(
123+
const modelsDts = this.project.createSourceFile(
124124
path.join(this.outDir, 'models.d.ts'),
125-
`export type * from '${resultPrismaImport}';`,
125+
`export type * from '${resultPrismaTypeImport}';`,
126126
{ overwrite: true }
127127
);
128-
await prismaDts.save();
128+
await modelsDts.save();
129+
130+
// reexport values from the original PrismaClient (enums, etc.)
131+
fs.writeFileSync(path.join(this.outDir, 'models.js'), `module.exports = require('${prismaImport}');`);
129132

130133
const authDecl = getAuthDecl(getDataModelAndTypeDefs(this.model));
131134
const authTypes = authDecl ? generateAuthType(this.model, authDecl) : '';
@@ -151,7 +154,7 @@ ${
151154
152155
${
153156
prismaTypesFixed
154-
? this.createLogicalPrismaImports(prismaImport, resultPrismaImport, target)
157+
? this.createLogicalPrismaImports(prismaImport, resultPrismaTypeImport, target)
155158
: this.createSimplePrismaImports(prismaImport, target)
156159
}
157160
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue [...]', () => {
4+
it('regression', async () => {
5+
const { zodSchemas } = await loadSchema(
6+
`
7+
enum FooType {
8+
Bar
9+
Baz
10+
}
11+
12+
type Meta {
13+
test String?
14+
}
15+
16+
model Foo {
17+
id String @id @db.Uuid @default(uuid())
18+
type FooType
19+
meta Meta @json
20+
21+
@@validate(type == Bar, "FooType must be Bar")
22+
}
23+
`,
24+
{
25+
provider: 'postgresql',
26+
pushDb: false,
27+
}
28+
);
29+
expect(zodSchemas.models.FooSchema).toBeTruthy();
30+
});
31+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue [...]', () => {
4+
it('regression', async () => {
5+
const { zodSchemas } = await loadSchema(
6+
`
7+
enum FooType {
8+
Bar
9+
Baz
10+
}
11+
type Meta {
12+
test String?
13+
}
14+
model Foo {
15+
id String @id @db.Uuid @default(uuid())
16+
type FooType
17+
meta Meta @json
18+
@@validate(type == Bar, "FooType must be Bar")
19+
}
20+
`,
21+
{
22+
provider: 'postgresql',
23+
pushDb: false,
24+
}
25+
);
26+
expect(
27+
zodSchemas.models.FooSchema.safeParse({
28+
id: '123e4567-e89b-12d3-a456-426614174000',
29+
type: 'Bar',
30+
meta: { test: 'test' },
31+
})
32+
).toMatchObject({
33+
success: true,
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)