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(zod): createMany types shouldn't be generated when Prisma version doesn't support it #1434

Merged
merged 1 commit into from
May 11, 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
10 changes: 10 additions & 0 deletions packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isFromStdlib,
parseOptionAsStrings,
resolvePath,
supportCreateMany,
} from '@zenstackhq/sdk';
import { DataModel, EnumField, Model, isDataModel, isEnum } from '@zenstackhq/sdk/ast';
import { addMissingInputObjectTypes, resolveAggregateOperationSupport } from '@zenstackhq/sdk/dmmf-helpers';
Expand Down Expand Up @@ -106,6 +107,7 @@ export class ZodSchemaGenerator {
aggregateOperationSupport,
project: this.project,
inputObjectTypes,
zmodel: this.model,
});
await transformer.generateInputSchemas(this.options, this.model);
this.sourceFiles.push(...transformer.sourceFiles);
Expand Down Expand Up @@ -200,6 +202,7 @@ export class ZodSchemaGenerator {
enumTypes,
project: this.project,
inputObjectTypes: [],
zmodel: this.model,
});
await transformer.generateEnumSchemas();
this.sourceFiles.push(...transformer.sourceFiles);
Expand All @@ -213,14 +216,21 @@ export class ZodSchemaGenerator {
for (let i = 0; i < inputObjectTypes.length; i += 1) {
const fields = inputObjectTypes[i]?.fields;
const name = inputObjectTypes[i]?.name;

if (!generateUnchecked && name.includes('Unchecked')) {
continue;
}

if (name.includes('CreateMany') && !supportCreateMany(this.model)) {
continue;
}

const transformer = new Transformer({
name,
fields,
project: this.project,
inputObjectTypes,
zmodel: this.model,
});
const moduleName = transformer.generateObjectSchema(generateUnchecked, this.options);
moduleNames.push(moduleName);
Expand Down
6 changes: 6 additions & 0 deletions packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Transformer {
private project: Project;
private inputObjectTypes: PrismaDMMF.InputType[];
public sourceFiles: SourceFile[] = [];
private zmodel: Model;

constructor(params: TransformerParams) {
this.originalName = params.name ?? '';
Expand All @@ -38,6 +39,7 @@ export default class Transformer {
this.enumTypes = params.enumTypes ?? [];
this.project = params.project;
this.inputObjectTypes = params.inputObjectTypes;
this.zmodel = params.zmodel;
}

static setOutputPath(outPath: string) {
Expand Down Expand Up @@ -119,6 +121,10 @@ export default class Transformer {
return result;
}

if (inputType.type.includes('CreateMany') && !supportCreateMany(this.zmodel)) {
return result;
}

// TODO: unify the following with `schema-gen.ts`

if (inputType.type === 'String') {
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/plugins/zod/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Model } from '@zenstackhq/sdk/ast';
import type { DMMF as PrismaDMMF } from '@zenstackhq/sdk/prisma';
import { Project } from 'ts-morph';

Expand All @@ -12,6 +13,7 @@ export type TransformerParams = {
prismaClientOutputPath?: string;
project: Project;
inputObjectTypes: PrismaDMMF.InputType[];
zmodel: Model;
};

export type AggregateOperationSupport = {
Expand Down
Loading