Search code examples
reactjstypescriptnext.jsprisma

how to change case of Nextjs generated prisma types from snake_case to camelCase


i have fullstack application build with nextjs and prisma ORM

"next": "12.3.0"
"prisma": "^4.5.0"

basically i want to change the case of my types from snake_case to caselCase to match front-end part. also would be nice to make types not nullable, but that not the main issue here

in the example you can see prisma.schema file model

model match {
  id                              Int
  tournament_id                   Int?
  player1_id                      Int?
  player2_id                      Int?
  comment                         String?
  is_completed                    Boolean?
  stage                           Int?
  start_date                      DateTime?   @db.Date
  winner_id                       String?
  score                           String?
  player3_id                      Int?
  player4_id                      Int?
}

and corresponding ts file looks like this

export type match = {
id: number
tournament_id: number | null
player1_id: number | null
player2_id: number | null
comment: string | null
is_completed: boolean | null
stage: number | null
start_date: Date | null
winner_id: string | null
score: string | null
player3_id: number | null
player4_id: number | null
}

match = { id: number tournamentId: number player1Id: number player2Id: number comment: string isCompleted: boolean stage: number startDate: Date winnerId: string score: string | null player3id: number player4Id: number }


Solution

  • You can achieve this by using @map

    model match {
      id           Int       @unique @map("id")
      tournamentId Int?      @map("tournament_id")
      player1Id    Int?      @map("player1_id")
      player2Id    Int?      @map("player2_id")
      comment      String?   @map("comment")
      isCompleted  Boolean?  @map("is_completed")
      stage        Int?      @map("stage")
      startDate    DateTime? @map("start_date") @db.Date
      winnerId     String?   @map("winner_id")
      score        String?   @map("score")
      player3Id    Int?      @map("player3_id")
      player4Id    Int?      @map("player4_id")
    }