Hello guys, so in this post, I would be writing about using regex to manipulate the Prisma schema file.
It all started by seeing a complaint on adjusting the fields in a schema file from snake_case to camel_case.
Well, the reason behind this is that in your SQL tables, you might have a column with the title, “first_name” and after running the Prisma introspect command you would see a field with the title, “first_name” in your schema file.
You might not really want to use that to query your database with the Prisma client API. So rather than doing that, you can make the API to use something else of preference, for instance, “firstName” with the help of the “@map” attribute for fields and “@@map” attribute for models.
You can see what I want to convert in the code below:-
So I wrote a small script that converts a Prisma schema file from using snake case models or fields to using camel case.
After writing this script, I showed it to one of my friends, Ahmed, he has created various Prisma tools including a CLI tool that converts your schema file from using snake-case models and fields to using camel-case. He told me that it won’t satisfy all cases and he listed the extra cases that I should implement. At the end of the post, I would leave a link to his Prisma tools.
He told me it won’t satisfy cases like “users_posts Post @relation(fields:[user_id, user_name])”
So I went further to make adjustments to the script and came up with this.
Thanks for reading and you can check out his awesome tools here. I highly recommend you use his tool if you need to integrate it into your build chain as this post is mainly to give insight on what the code could look like.
I decided to create a vs-code plugin that performs this function. You can check it out here