chrishoermann / zod-prisma-types

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

Empty mutation input schema with missing imports while generating from pivot table #106

Closed pedropmedina closed 1 year ago

pedropmedina commented 1 year ago

Hi @chrishoermann. I'm having this issue with the recent version where not imports are generated for pivot tables and the schema itself is empty which kind of makes sense, but it's causing issue?

model CustomerCompanyContact {
  contact         Int
  contactsCompany Int @map("contacts_company")

  customerRef Customer? @relation(fields: [contactsCompany], references: [id], onDelete: Cascade)
  contactRef  Contact?  @relation(fields: [contact], references: [id], onDelete: Cascade)

  @@id([contact, contactsCompany])
  @@index([contactsCompany])
  @@map("customer_company_contacts")
}
// generated type in @prisma/client looks like this

  export type PartnerCompanyContactUpdateManyMutationInput = {

  }
// The above ^ model generates the following schema. 
// The model itself is just a pivot table. Also notice that not imports where generated either.
// The main problem between prev version 2.0.17 and latest version is that imports up top aren't there
// It's missing Prisma and zod imports causing the build to crash

export const PartnerCompanyContactUpdateManyMutationInputSchema: z.ZodType<Prisma.PartnerCompanyContactUpdateManyMutationInput> = z.object({
}).strict();

export default PartnerCompanyContactUpdateManyMutationInputSchema;
chrishoermann commented 1 year ago

@pedropmedina thanks for the report. I made changes to how the imports are handled in one of the latest Versions so there ist obviously a bug somewhere. can you provide a Schema/config so I can reproduce the issue?

pedropmedina commented 1 year ago

Hey @chrishoermann try this schema. The issue it's with join tables RoleToUser and RolesToResourcesToActions

generator client {
  provider        = "prisma-client-js"
  output          = "../../../../../node_modules/@prisma/client/auth"
  previewFeatures = ["fullTextSearch", "fullTextIndex"]
}

generator zod-prisma-types {
  provider             = "zod-prisma-types"
  output               = "./generated/zod"
  prismaClientPath     = "@prisma/client/auth"
  useMultipleFiles     = true
  useDefaultValidators = false
}

datasource db {
  provider = "mysql"
  url      = env("AUTH_DATABASE_URL")
}

model Application {
  id          String     @id @default(cuid())
  nameId      String     @unique @map("name_id") 
  name        String
  description String?    @db.Text
  url         String?
  resources   Resource[]
  createdAt   DateTime   @default(now()) @map("created_at")
  updatedAt   DateTime   @updatedAt @map("updated_at")

  @@map("applications")
}

model Action {
  id               String                      @id @default(cuid())
  nameId           String                      @unique @map("name_id")
  name             String
  createdAt        DateTime                    @default(now()) @map("created_at")
  updatedAt        DateTime                    @updatedAt @map("updated_at")
  rolesToResources RolesToResourcesToActions[]

  @@map("actions")
}

model Resource {
  id             String                      @id @default(cuid())
  name           String
  description    String?                     @db.Text
  application    Application                 @relation(fields: [applicationId], references: [id], onDelete: Cascade)
  applicationId  String                      @map("application_id")
  createdAt      DateTime                    @default(now()) @map("created_at")
  updatedAt      DateTime                    @updatedAt @map("updated_at")
  rolesToActions RolesToResourcesToActions[]

  @@map("resources")
}

model Role {
  id                 String                      @id @default(cuid())
  nameId             String                      @unique @map("name_id")
  name               String
  isSuperAdmin       Boolean?                    @default(false) @map("is_super_admin")
  isAdmin            Boolean?                    @default(false) @map("is_admin")
  createdAt          DateTime                    @default(now()) @map("created_at")
  updatedAt          DateTime                    @updatedAt @map("updated_at")
  resourcesToActions RolesToResourcesToActions[]
  users              RoleToUser[]

  @@map("roles")
}

model RoleToUser {
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId String @map("user_id")
  role   Role   @relation(fields: [roleId], references: [id], onDelete: Cascade)
  roleId String @map("role_id")

  @@id([userId, roleId])
  @@map("roles_to_users")
}

model RolesToResourcesToActions {
  roleId     String
  role       Role     @relation(fields: [roleId], references: [id], onDelete: Cascade)
  resourceId String
  resource   Resource @relation(fields: [resourceId], references: [id], onDelete: Cascade)
  actionId   String
  action     Action   @relation(fields: [actionId], references: [id], onDelete: Cascade)

  @@id([roleId, resourceId, actionId])
  @@map("roles_to_resources_to_actions")
}

model Department {
  id          String   @id @default(cuid())
  nameId      String   @unique @map("name_id") 
  name        String
  description String?  @db.Text
  users       User[]
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")

  @@map("departments")
}

model UserType {
  id          String   @id @default(cuid())
  nameId      String   @unique @map("name_id") 
  name        String
  description String?  @db.Text
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")
  user        User[]   @relation("user_type")

  @@map("user_types")
}

model User {
  firstName    String?      @map("first_name")
  lastName     String?      @map("last_name")
  phone        String?
  isActive     Boolean?     @default(true) @map("is_active")
  isEmployee   Boolean?     @default(false) @map("is_employee")
  isCustomer   Boolean?     @default(false) @map("is_customer")
  department   Department?  @relation(fields: [departmentId], references: [id])
  departmentId String?      @unique @map("department_id")
  userType     UserType?    @relation(name: "user_type", fields: [userTypeId], references: [id])
  userTypeId   String?      @unique @map("user_type_id")
  createdAt    DateTime     @default(now()) @map("created_at")
  updatedAt    DateTime     @updatedAt @map("updated_at")
  roles        RoleToUser[]
  id            String    @id @default(cuid())
  email         String    @unique

  @@map("users")
}
}
chrishoermann commented 1 year ago

@pedropmedina this should now be fixed in the latest release.