Skip to content

Commit 38e1e44

Browse files
authored
fix (generator): fix type issue on schemas containing relations (#727)
This PR fixes the problem where the generated client may contain type errors for models containing relations. This is a known problem with Zod and Prisma and is described here: chrishoermann/zod-prisma-types#98. That issue also proposes a solution with a dirty type cast: chrishoermann/zod-prisma-types#98 (comment) which is implemented in this PR. This PR removes the need for pinning the version of Zod (#700).
1 parent d5cdbf1 commit 38e1e44

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

.changeset/weak-years-explode.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@electric-sql/prisma-generator": patch
3+
---
4+
5+
Fix type issue in generated client for DB schemas containing relations.

generator/src/functions/contentWriters/writeOutputObjectType.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { writeSelect } from './writeSelect'
22
import { writeNonScalarType, writeScalarType, writeSpecialType } from '..'
3-
import { ExtendedDMMFSchemaField } from '../../classes'
3+
import { ExtendedDMMF, ExtendedDMMFSchemaField } from '../../classes'
44
import { type ContentWriterOptions } from '../../types'
55

6+
function modelHasRelation(
7+
model: ExtendedDMMFSchemaField['modelType'],
8+
dmmf: ExtendedDMMF
9+
): boolean {
10+
if (typeof model === 'string') {
11+
const maybeModel = dmmf.datamodel.models.find((m) => m.name === model)
12+
return maybeModel?.hasRelationFields ?? false
13+
}
14+
return false
15+
}
16+
617
export const writeOutputObjectType = (
718
{ fileWriter, dmmf, getSingleFileContent = false }: ContentWriterOptions,
819
field: ExtendedDMMFSchemaField
@@ -120,7 +131,14 @@ export const writeOutputObjectType = (
120131
writer.newLine()
121132
})
122133
})
123-
.write(`).strict()`)
134+
.write(`).strict() `)
135+
// There is a typing bug that occurs only with models that have relations.
136+
// a dirty fix here is to typecast the value to the expected type:
137+
// cf. https://github.com/chrishoermann/zod-prisma-types/issues/98#issuecomment-1800112669
138+
.conditionalWrite(
139+
modelHasRelation(field.modelType, dmmf),
140+
`as ${field.customArgType}`
141+
)
124142

125143
if (useMultipleFiles && !getSingleFileContent) {
126144
writer.blankLine().writeLine(`export default ${field.argName}Schema;`)

generator/src/functions/writeSingleFileArgTypeStatements.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export const writeSingleFileArgTypeStatements: WriteStatements = (
1515

1616
fileWriter.writeHeading(`ARGS`, 'FAT')
1717

18-
dmmf.schema.outputObjectTypes.argTypes.forEach((outputType) => {
18+
const types = dmmf.schema.outputObjectTypes
19+
types.argTypes.forEach((outputType) => {
1920
outputType.prismaActionFields.forEach((field) => {
2021
writeOutputObjectType({ dmmf, fileWriter }, field)
2122
})

0 commit comments

Comments
 (0)