Skip to content
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
30 changes: 21 additions & 9 deletions packages/plugins/openapi/src/rpc-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Inspired by: https://github.com/omar-dulaimi/prisma-trpc-generator

import { PluginError, PluginOptions, analyzePolicies, requireOption, resolvePath } from '@zenstackhq/sdk';
import { DataModel, Model, TypeDef, TypeDefField, TypeDefFieldType, isDataModel, isEnum, isTypeDef } from '@zenstackhq/sdk/ast';
import {
DataModel,
Model,
TypeDef,
TypeDefField,
TypeDefFieldType,
isDataModel,
isEnum,
isTypeDef,
} from '@zenstackhq/sdk/ast';
import {
AggregateOperationSupport,
addMissingInputObjectTypesForAggregate,
Expand Down Expand Up @@ -48,7 +57,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
output = resolvePath(output, this.options);

// input types
this.inputObjectTypes.push(...this.dmmf.schema.inputObjectTypes.prisma);
this.inputObjectTypes.push(...(this.dmmf.schema.inputObjectTypes.prisma ?? []));
this.outputObjectTypes.push(...this.dmmf.schema.outputObjectTypes.prisma);

// add input object types that are missing from Prisma dmmf
Expand Down Expand Up @@ -649,13 +658,13 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
for (const _enum of [...(this.dmmf.schema.enumTypes.model ?? []), ...this.dmmf.schema.enumTypes.prisma]) {
schemas[upperCaseFirst(_enum.name)] = this.generateEnumComponent(_enum);
}

// Also add enums from AST that might not be in DMMF (e.g., only used in TypeDefs)
for (const enumDecl of this.model.declarations.filter(isEnum)) {
if (!schemas[upperCaseFirst(enumDecl.name)]) {
schemas[upperCaseFirst(enumDecl.name)] = {
type: 'string',
enum: enumDecl.fields.map(f => f.name)
enum: enumDecl.fields.map((f) => f.name),
};
}
}
Expand Down Expand Up @@ -765,12 +774,15 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
return result;
}

private generateField(def: { kind: DMMF.FieldKind; type: string; isList: boolean; isRequired: boolean; name?: string }, modelName?: string) {
private generateField(
def: { kind: DMMF.FieldKind; type: string; isList: boolean; isRequired: boolean; name?: string },
modelName?: string
) {
// For Json fields, check if there's a corresponding TypeDef in the original model
if (def.kind === 'scalar' && def.type === 'Json' && modelName && def.name) {
const dataModel = this.model.declarations.find(d => isDataModel(d) && d.name === modelName) as DataModel;
const dataModel = this.model.declarations.find((d) => isDataModel(d) && d.name === modelName) as DataModel;
if (dataModel) {
const field = dataModel.fields.find(f => f.name === def.name);
const field = dataModel.fields.find((f) => f.name === def.name);
if (field?.type.reference?.ref && isTypeDef(field.type.reference.ref)) {
// This Json field references a TypeDef
return this.wrapArray(
Expand Down Expand Up @@ -876,7 +888,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
if (type.reference?.ref) {
return this.ref(type.reference.ref.name, true);
}

// For scalar types, reuse the existing mapping logic
// Note: Json type is handled as empty schema for consistency
return match(type.type)
Expand Down Expand Up @@ -913,7 +925,7 @@ export class RPCOpenAPIGenerator extends OpenAPIGeneratorBase {
.with(P.union('JSON', 'Json'), () => {
// For Json fields, check if there's a specific TypeDef reference
// Otherwise, return empty schema for arbitrary JSON
const isTypeDefType = this.model.declarations.some(d => isTypeDef(d) && d.name === type);
const isTypeDefType = this.model.declarations.some((d) => isTypeDef(d) && d.name === type);
return isTypeDefType ? this.ref(type, false) : {};
})
.otherwise((type) => this.ref(type.toString(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@prisma/client": "6.14.x",
"@prisma/client": "6.15.x",
"@trpc/client": "^10.45.2",
"@trpc/server": "^10.45.2",
"nuxt": "^3.14.1592",
Expand All @@ -21,7 +21,7 @@
},
"devDependencies": {
"esbuild": "^0.24.0",
"prisma": "6.14.x",
"prisma": "6.15.x",
"typescript": "^5.6.2",
"vue-tsc": "^2.1.10"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@prisma/client": "6.14.x",
"@prisma/client": "6.15.x",
"@trpc/client": "^11.0.0-rc.563",
"@trpc/server": "^11.0.0-rc.563",
"nuxt": "^3.14.1592",
Expand All @@ -21,7 +21,7 @@
},
"devDependencies": {
"esbuild": "^0.24.0",
"prisma": "6.14.x",
"prisma": "6.15.x",
"typescript": "^5.6.2",
"vue-tsc": "^2.1.10"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/trpc/tests/projects/t3-trpc-v11/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start": "next start"
},
"dependencies": {
"@prisma/client": "6.14.x",
"@prisma/client": "6.15.x",
"@t3-oss/env-nextjs": "^0.10.1",
"@tanstack/react-query": "^5.50.0",
"@trpc/client": "^11.0.0-rc.446",
Expand All @@ -39,7 +39,7 @@
"@typescript-eslint/parser": "^8.1.0",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.4",
"prisma": "6.14.x",
"prisma": "6.15.x",
"typescript": "^5.5.3"
},
"ct3aMetadata": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"zod-validation-error": "catalog:"
},
"peerDependencies": {
"@prisma/client": "5.0.0 - 6.14.x",
"@prisma/client": "5.0.0 - 6.15.x",
"zod": "catalog:"
},
"author": {
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
"zod-validation-error": "catalog:"
},
"peerDependencies": {
"prisma": "5.0.0 - 6.14.x",
"prisma": "5.0.0 - 6.15.x",
"zod": "catalog:"
},
"devDependencies": {
"@prisma/client": "6.14.x",
"@prisma/client": "6.15.x",
"@types/async-exit-hook": "^2.0.0",
"@types/pluralize": "^0.0.29",
"@types/semver": "^7.3.13",
Expand Down
6 changes: 3 additions & 3 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export type Enhanced<Client> =
// they may be incorrectly represented as required, we need to fix that for input types
// also, if a FK field is of such case, its corresponding relation field should be optional
const createInputPattern = new RegExp(`^(.+?)(Unchecked)?Create.*Input$`);
for (const inputType of dmmf.schema.inputObjectTypes.prisma) {
for (const inputType of dmmf.schema.inputObjectTypes.prisma ?? []) {
const match = inputType.name.match(createInputPattern);
const modelName = this.resolveName(match?.[1]);
if (modelName) {
Expand Down Expand Up @@ -570,8 +570,8 @@ export type Enhanced<Client> =
const project = new Project();

// remove delegate_aux_* fields from the prismaNamespace.ts
const internalFilename = `${prismaClientDir}/internal/prismaNamespace.ts`
const internalFilenameFixed = `${prismaClientDir}/internal/prismaNamespace-fixed.ts`
const internalFilename = `${prismaClientDir}/internal/prismaNamespace.ts`;
const internalFilenameFixed = `${prismaClientDir}/internal/prismaNamespace-fixed.ts`;
const internalSf = project.addSourceFileAtPath(internalFilename);
const syntaxList = internalSf.getChildren()[0];
if (!Node.isSyntaxList(syntaxList)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ZodSchemaGenerator {
(o) => !excludeModels.find((e) => e === o.model)
);

const inputObjectTypes = prismaClientDmmf.schema.inputObjectTypes.prisma.filter(
const inputObjectTypes = (prismaClientDmmf.schema.inputObjectTypes.prisma ?? []).filter(
(type) =>
!excludeModels.some((e) => type.name.toLowerCase().startsWith(e.toLocaleLowerCase())) &&
// exclude delegate aux related types
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"author": "",
"license": "MIT",
"dependencies": {
"@prisma/generator-helper": "6.14.x",
"@prisma/internals": "6.14.x",
"@prisma/generator-helper": "6.15.x",
"@prisma/internals": "6.15.x",
"@zenstackhq/language": "workspace:*",
"@zenstackhq/runtime": "workspace:*",
"langium": "1.3.1",
Expand Down
Loading
Loading