omar-dulaimi / prisma-zod-generator

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

Added support for BigInt #14

Closed carloschida closed 2 years ago

carloschida commented 2 years ago

Description

Currently Prisma's BigInt is not parsed. For instance if we extend the Post model with likes:

model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
  likes     BigInt
}

One of the results is:

const Schema: z.ZodType<Prisma.PostCreateInput> = z
  .object({
    createdAt: z.bigint().optional(),
    updatedAt: z.bigint().optional(),
    title: z.string(),
    content: z.union([z.string(), z.bigint()]).optional().nullable(),
    published: z.bigint().optional(),
    viewCount: z.number().optional(),
    author: z.bigint().optional(),
  })
  .strict();

export const PostCreateInputObjectSchema = Schema;

With the proposed fix, the schema now includes likes:

const Schema: z.ZodType<Prisma.PostCreateInput> = z
  .object({
   // ...
    author: z.bigint().optional(),
    likes: z.bigint(),
  })
  .strict();

To test the fix, simply run npm run gen-example and inspect the file prisma/generated/schemas/objects/PostCreateInput.schema.ts.

omar-dulaimi commented 2 years ago

Thank you for the contribution @carloschida LGTM!

omar-dulaimi commented 2 years ago

Released in https://github.com/omar-dulaimi/prisma-zod-generator/releases/tag/0.5.0

eglove commented 2 years ago

tsc postinstall causes fail

R4hNciG

omar-dulaimi commented 2 years ago

@eglove Please try again with 0.5.1

Also, make sure to star this project if you find it useful.