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: generated zod schemas fail to compile when outputting to a custom dir #1611

Merged
merged 1 commit into from
Jul 23, 2024
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
3 changes: 2 additions & 1 deletion packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class EnhancerGenerator {

const enhanceTs = this.project.createSourceFile(
path.join(this.outDir, 'enhance.ts'),
`import { type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
`/* eslint-disable */
import { type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
import { createEnhancement } from '@zenstackhq/runtime/enhancements';
import modelMeta from './model-meta';
import policy from './policy';
Expand Down
6 changes: 4 additions & 2 deletions packages/schema/src/plugins/enhancer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const run: PluginFunction = async (model, options, _dmmf, globalOptions) => {
let prismaClientPath: string | undefined;
if (dmmf) {
// a logical client is generated
if (typeof options.output === 'string') {
if (options.output || globalOptions?.output) {
// handle custom output path

// get the absolute path of the prisma client types
const prismaClientPathAbs = path.resolve(options.output, 'models');
const prismaClientPathAbs = path.resolve(outDir, 'models');

// resolve it relative to the schema path
prismaClientPath = path.relative(path.dirname(options.schemaPath), prismaClientPathAbs);
Expand Down
56 changes: 56 additions & 0 deletions tests/regression/tests/issue-1610.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1610', () => {
it('regular prisma client', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}

model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int
}
`,
{ fullZod: true, output: './lib/zen' }
);
});

it('logical prisma client', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}

model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{ fullZod: true, output: './lib/zen' }
);
});

it('no custom output', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}

model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{ fullZod: true, preserveTsFiles: true }
);
});
});
Loading