omar-dulaimi / prisma-trpc-generator

Prisma 2+ generator to emit fully implemented tRPC routers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generator doesn't generate schema from column type decimal

kittipojsr opened this issue · comments

Bug description

Prisma trpc generator doesn't generate some column type from schema.

How to reproduce

Expected behavior

Need to support column type same as prisma

Prisma information

generator client {
  provider               = "prisma-client-js"
  output                 = "./client"
  customPrismaImportPath = "../client"
}

generator trpc {
  provider       = "prisma-trpc-generator"
  withMiddleware = false
  withShield     = false
  contextPath    = "../../../../src/context"
  output         = "./trpc"
}

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

generator zod {
  provider = "zod-prisma"
  output   = "./zod" // (default) the directory where generated zod schemas will be saved

  relationModel = true // (default) Create and export both plain and related models.
  // relationModel         = "default" // Do not export model without relations.
  // relationModel         = false // Do not generate related model

  // modelCase                = "PascalCase" // (default) Output models using pascal case (ex. UserModel, PostModel)
  modelCase = "camelCase" // Output models using camel case (ex. userModel, postModel)

  modelSuffix = "Model" // (default) Suffix to apply to your prisma models when naming Zod schemas

  // useDecimalJs          = false // (default) represent the prisma Decimal type using as a JS number
  useDecimalJs = true // represent the prisma Decimal type using Decimal.js (as Prisma does)

  // https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values
  prismaJsonNullability = true // (default) uses prisma's scheme for JSON field nullability
  // prismaJsonNullability = false // allows null assignment to optional JSON fields
}

model Test {
    amount      Decimal?     @db.Decimal(9, 2)
}

Environment & setup

  • OS: Mac OS
  • Database: MySQL
  • Node.js version: 14.7.3

Prisma Version

prisma                  : 4.1.1
@prisma/client          : 4.1.1
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at ../../node_modules/prisma/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at ../../node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at ../../node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 8d8414deb360336e4698a65aa45a1fbaf1ce13d8 (at ../../node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Default Engines Hash    : 8d8414deb360336e4698a65aa45a1fbaf1ce13d8
Studio                  : 0.469.0

@kittipojsr Please provide a valid schema. Your model is invalid.

this valid schema for test

// -------------------------------------
// WARNING
// THIS FILE IS AUTOGENERATED BY PRISMERGE
// DO NOT MANUALLY EDIT THIS FILE!
// -------------------------------------

generator client {
  provider               = "prisma-client-js"
  output                 = "./client"
  customPrismaImportPath = "../client"
}

generator trpc {
  provider       = "prisma-trpc-generator"
  withMiddleware = false
  withShield     = false
  contextPath    = "../../../../src/context"
  output         = "./trpc"
}

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

generator zod {
  provider = "zod-prisma"
  output   = "./zod" // (default) the directory where generated zod schemas will be saved

  relationModel = true // (default) Create and export both plain and related models.
  // relationModel         = "default" // Do not export model without relations.
  // relationModel         = false // Do not generate related model

  // modelCase                = "PascalCase" // (default) Output models using pascal case (ex. UserModel, PostModel)
  modelCase = "camelCase" // Output models using camel case (ex. userModel, postModel)

  modelSuffix = "Model" // (default) Suffix to apply to your prisma models when naming Zod schemas

  // useDecimalJs          = false // (default) represent the prisma Decimal type using as a JS number
  useDecimalJs = true // represent the prisma Decimal type using Decimal.js (as Prisma does)

  // https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values
  prismaJsonNullability = true // (default) uses prisma's scheme for JSON field nullability
  // prismaJsonNullability = false // allows null assignment to optional JSON fields
}

model Test {
  id        Int      @id @default(autoincrement())
  name      String?
  amount    Decimal? @db.Decimal(9, 2)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

This is some output after generated. It missing amount

import { z } from 'zod'

import type { Prisma } from '../../../client'

const Schema: z.ZodType<Prisma.TestCreateInput> = z
  .object({
    name: z.string().optional().nullable(),
    createdAt: z.date().optional(),
    updatedAt: z.date().optional(),
  })
  .strict()

export const TestCreateInputObjectSchema = Schema

and how to config the generator schema to accept validate date string ?
This is some test

payload {
      data: {
        name: 'Blanca Hyatt',
        createdAt: 2022-08-09T03:58:40.668Z,
        updatedAt: 2022-08-08T18:58:18.021Z
      }
    }
result [
      {
        code: 'invalid_type',
        expected: 'date',
        received: 'string',
        path: [ 'data', 'createdAt' ],
        message: 'Expected date, received string'
      },
      {
        code: 'invalid_type',
        expected: 'date',
        received: 'string',
        path: [ 'data', 'updatedAt' ],
        message: 'Expected date, received string'
      }
    ]

and how to config the generator schema to accept validate date string ?

For dates in the meantime you could use(I think):

new Date('2022-08-09T03:58:40.668Z');

I'll create a feature request to support this by the generator itself eleminating the need to use new Date.

https://github.com/colinhacks/zod#dates