omar-dulaimi / prisma-zod-generator

Prisma 2+ generator to emit Zod schemas from your Prisma schema
MIT License
535 stars 45 forks source link

Prisma 5 compatibility #87

Open jacobclarke92 opened 1 year ago

jacobclarke92 commented 1 year ago

I forked this repo for my own personal use case and stumbled upon some issues when attempting to upgrade to prisma 5.1.1

Just wanted to leave some notes here of findings thus far.
Feel free to take it over.


1.

[...]CountOutputTypeArgs types have been renamed e.g. AddressCountOutputTypeArgs becomes AddressCountOutputTypeDefaultArgs Added this after the isAggregateInputType check in Transformer.generateExportObjectSchemaStatement

    if (name.endsWith('CountOutputTypeArgs')) {
      name = name.replace(/Args$/, 'DefaultArgs')
    }

2. The one I got stuck on after that was the [...]WhereUniqueInput types.
So where previously a type might look like

  export type AssetWhereUniqueInput = {
    id?: number
    uuid?: string
    checksum?: string
  }

Is now wrapped with a AtLeast<> type utility that requires at least one of the values to be present instead of them all being optional. This conflicts with the the ZodType argument in these type of files:

const Schema: z.ZodType<Prisma.AssetWhereUniqueInput> = 
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I narrowed it down to the fact that extendedWhereUnique is enabled by default in prisma 5.
Here's a related issue to watch: https://github.com/prisma/prisma/issues/20619

I don't know if there's an answer to this with zod?


I don't fully understand the code and how it's all laid out so I got a bit overwhelmed and tapped out, but hopefully this helps if you decide to keep supporting this package (which you should! it's great!)

jacobclarke92 commented 1 year ago

Found a potential solution to Prisma's AtLeast type helper in zod:
https://stackoverflow.com/a/73294947
Will try and implement it when I get time, leaving here for later.