Automatically pascal case model names during introspection
See original GitHub issueProblem
Having to manually update model names to conform to Prisma’s naming conventions using the introspection tool is 1) annoying to do for many snake cased table names, and 2) prevents an introspection-only schema approach to using prisma2.
Solution
Automatically change model names from this format:
model my_table {
id String @id @default(cuid())
name String
}
To this:
model MyTable {
id String @id @default(cuid())
name String
@@map("my_table")
}
Alternatives
I could write my own script.
Additional context
Started in discussion at:
https://github.com/prisma/prisma2/discussions/1925
Just going to dump some regex here that might help write that script:
model ([a-z_]+) {
const result = `
model ${pascalCase(name)} {
@@map(${name})
}
`
Issue Analytics
- State:
- Created 3 years ago
- Reactions:41
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Names in the underlying database - Prisma
Mapping names in the Prisma schema allows you to influence the naming in your ... hand has recommended model naming conventions (singular form,...
Read more >Methods and properties should be named in PascalCase
Shared naming conventions allow teams to collaborate efficiently. This rule checks whether or not method and property names are PascalCased. To reduce noise, ......
Read more >What is pascal case? - TheServerSide.com
Pascal case requires that the first letter of a variable be in upper case. In contrast, camel case -- also known as CamelCase...
Read more >Can I tell MVC to separate pascal-cased properties in to words ...
1. According to this answer, you can only apply constants, stackoverflow.com/questions/11654740/… · 2 · Use reflection to get property name from class definition, ......
Read more >Ditching Gson's Field Naming Strategy | by Sebastiano Gottardo
Anyway, back to the codebase. The members of the model classes followed the standard camel case Java/Kotlin convention. There were no ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I wanted to follow standard postgres naming practices (using snake case for column and field names so I don’t have to quote everything when using raw sql), but still end up with a generated client that follows normal Typescript/prisma conventions (Pascal case for models, camelcase for field names, plurals for array relationships).
I wrote a script that handles all of my personal naming issues. Might be easier with access to the AST and there could be cases I’m missing since I’m starting on a new app and so my schema is fairly simple, but works for my use case and shouldn’t be too hard to add additional cases as needed. Didn’t bother trying to handle spacing. Saving the file in vscode with the prisma plugin fixes it. It handles:
Edit: Added check to avoid adding @map to relation fields.
For those looking for fixing the naming convention automatically, I wrote a utility program that utilizes the Prisma schema AST, provided by the internal API
getDMMF
from@prisma/sdk
, to automatically transform the naming ofmodel
andfields
while keeping the mapping correct.https://github.com/IBM/prisma-schema-transformer