chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
579 stars 43 forks source link

Prisma has no exported member named '***DefaultArgs' #171

Closed samrocksc closed 8 months ago

samrocksc commented 11 months ago

Overview

I am having an issue with Args vs. DefaultArgs

Versions used

Schema

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["linux-musl", "debian-openssl-3.0.x", "debian-openssl-1.1.x"]
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator zod {
  provider                         = "zod-prisma-types"
  createRelationValuesTypes        = true
  writeNullishInModelTypes         = true
  createOptionalDefaultValuesTypes = true
  useMultipleFiles                 = true // default is false
  writeBarrelFiles                 = false // default is true
  createInputTypes                 = true // default is true
  createModelTypes                 = true // default is true
  addInputTypeValidation           = true // default is true
  addIncludeType                   = true // default is true
  addSelectType                    = true // default is true
  validateWhereUniqueInput         = false // default is false
  createPartialTypes               = false // default is false
  useDefaultValidators             = true // default is true
  coerceDate                       = true // default is true
}

model User {
 id        String   @id @default(auto()) @map("_id") @db.ObjectId
  name  String
  email String @unique
  posts Post[]
}

model Post {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  title    String
  content  String
  author   User   @relation(fields: [authorId], references: [id])
  authorId String @db.ObjectId
}

Unexpected Behavior

image

code causing this

// Error is flagged on `PostDefaultArgs`
export const PostArgsSchema: z.ZodType<Prisma.PostDefaultArgs> = z.object({
  select: z.lazy(() => PostSelectSchema).optional(),
  include: z.lazy(() => PostIncludeSchema).optional(),
}).strict();

I am just not familiar enough with how things are getting written to really be able to fix this right now. I'll try and dig into it this weekend.

Does anyone have a work around or anything? Really excited about the library, but this bug kind of makes it unusable with barrelfiles or in a single file unfortunately.

I found the culprit here: I'll have to dig into it and find out

https://github.com/chrishoermann/zod-prisma-types/blob/ac87ff3a1a8bebf121176c6229877c6aeb23115f/packages/generator/src/functions/contentWriters/writeArgs.ts#L36

Now what's interesting is I can just use PostArgs and everything works as intended! So we're already close to the fix.

chrishoermann commented 11 months ago

@samrocksc are you on prisma 5.1.1? They introduced some type renaming in one of the latest updates.

samrocksc commented 11 months ago

Prisma 4.16.1 and then I tried with 5, but the same issue happens, i can try a few other configurations.

chrishoermann commented 11 months ago

did you specifically try prisma 5.1.1? this fixed the issue in this case

chrishoermann commented 11 months ago

It seems prisma introduced more breaking changes in their typings from 4.x.x. to 5.1.1 that I need to be aware of and that need to be stated in the docs.

EDIT: It is mentioned in the 5.1 release that they changed the name auf some args to DefaultArgs as mentioned in the release notes

samrocksc commented 11 months ago

I need to catch up on how everything works, would be nice to be able to contribute back on this...it's a really nice application. I'll lock into 5.0 next time

chrishoermann commented 11 months ago

I just realized that changing the behaviour to meet the type requirements of prisma 5.1.1 introduced bugs in prior versions. I need to dig into this a bit on the weekend to get this sorted out.

samrocksc commented 11 months ago

Updating versions I've tried this with and whether it fails or not for you.

samrocksc commented 11 months ago

In the code referenced above, is the version of prisma passed into that function anywhere? We could just do a version check and apply backwards. I still haven't had a chance to peak at this.

chrishoermann commented 11 months ago

I implemented a version check yesterday in 2.7.9 but haven't testes it with a monorepo yet. it shoud work in a standard project where the package.json is located in the working directory.

samrocksc commented 10 months ago

works fine ;o) By the way, just tested it. In the future I'll be able to PR against these.

chrishoermann commented 8 months ago

closing since this should not be a problem anymore with the current versions of the generator and prisma