sachaos / prisma-case-format

Give your introspected schema.prisma sane naming conventions

Home Page:https://www.npmjs.com/package/prisma-case-format

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

prisma-case-format

As of prisma@2.19.0, prisma introspect will name its model 1:1 with your database's conventions. That's great, but if your db is purely snake_case, or worse if your tables lack any common convention, it can make the autogenerated client code less pleasant to consume. prisma-case-format will format your prisma schema's model and field names to enforce a more conventional experience.

Use-case: as a one-time migration assistant

Use --dry-run to figure out which case conventions are correct for your project. Once things look correct, drop the flag to save changes to the --file (or your local schema.prisma by default).

Use-case: as a small CI/CD component

prisma-case-format now aims to be idempotent, so you can use it to confirm that case conventions in your schema.prisma have not accidentally drifted. prisma-case-format can be applied on-commit or on-push, either in a git commit-hook package or as a CI/CD step. Use --dry-run to diff changes with the original file, or backup the original and compare to the edit.

Usage

❯ prisma-case-format --help
Usage: prisma-case-format [options]

Give your schema.prisma sane naming conventions

Options:
  --file <file>                    cwd-relative path to schema.prisma file (default: "schema.prisma")
  -D, --dry-run                    print changes to console, rather than back to file (default: false)
  --table-case <tableCase>         case convention for table names (default: "pascal")
  --field-case <fieldCase>         case convention for field names (default: "camel")
  --map-table-case <mapTableCase>  case convention for @@map() annotations
  --map-field-case <mapFieldCase>  case convention for @map() annotations
  -p, --pluralize                  optionally pluralize array type fields (default: false)
  -V, --version                    hint: you have v1.3.0
  -h, --help                       display help for command
Supported case conventions: ["pascal", "camel", "snake"]

Example

// schema.prisma before
...
model house_rating {
  id       Int    @id @default(autoincrement())
  house_id String
  house    house  @relation(fields: [house_id], references: [id])
  ...
}
...
model house {
  id  String  @id @default(uuid())
  house_rating house_rating[]
  ...
}
...
❯ prisma-case-format
✨ Done.
// schema.prisma after
...
model HouseRating {
  id       Int    @id @default(autoincrement())
  houseId  String @map("house_id")
  house    House  @relation(fields: [houseId], references: [id])
  ...

  @@map("house_rating")
}
...
model House {
  id           String        @id @default(uuid())
  houseRating HouseRating[]
  ...

  @@map("house")
}
...

Supply the -p or --pluralize argument to get array pluralizations.

❯ prisma-case-format -p
✨ Done.
// schema.prisma after pluralization
...
model House {
  ...
  houseRatings HouseRating[]
  ownerContacts String[] @map("owner_contact")
  ...
}
...

About

Give your introspected schema.prisma sane naming conventions

https://www.npmjs.com/package/prisma-case-format


Languages

Language:TypeScript 78.2%Language:JavaScript 21.8%