Skip to content

Commit

Permalink
fix(zod): createMany types shouldn't be generated when Prisma version…
Browse files Browse the repository at this point in the history
… doesn't support it
  • Loading branch information
ymc9 committed May 11, 2024
1 parent 2c7c82b commit c0c54ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
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

0 comments on commit c0c54ff

Please sign in to comment.