What is Prisma ?
Prisma is an open-source Object-Relational Mapping (ORM) tool that simplifies database access and management for applications. It provides a set of tools and libraries that allow developers to interact with databases using a type-safe and intuitive API.
Prisma acts as a bridge between your application code and the database, abstracting away the complexities of database queries, migrations, and data modeling. It supports various databases such as PostgreSQL, MySQL, SQLite, and SQL Server, allowing you to switch between them easily without changing your application code.
Goal
In the world of software development, creating efficient and maintainable code is a constant pursuit. As projects grow in complexity, developers face the challenge of keeping their codebases organized and scalable. When working with Prisma ORM, a powerful database toolkit, structuring and managing models effectively becomes crucial to ensure the long-term success of the project.
In this tutorial, I will explain how I broke away from the one-file prisma model and opted for a modular solution where each model is in its own file:
Steps
After installing PrismaORM (https://www.prisma.io/docs/getting-started/quickstart)
Create a folder called models
in your prisma
folder, then add our files to it:
mkdir prisma/models
touch prisma/models/config.prisma
touch prisma/models/user.prisma
touch prisma/models/post.prisma
config.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
user.prisma
model User {
id Int @id @default(autoincrement())
username String @unique
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
}
post.prisma
model Post {
id Int @id @default(autoincrement())
title String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User @relation(fields: [authorId], references: [id])
authorId Int
}
The merging step.
This is where the magic happens, thanks to a single command, we will merge our models into the main schema.prisma
file:
cd ./prisma/models && rm -rf ../schema.prisma && cat *.prisma >> ../schema.prisma && cd .. && prisma format
Breakdown of the command:
We change the current dir
cd ./prisma/models
We delete the content of the main file
rm -rf ../schema.prisma
We add the content of our models to the main file
cat *.prisma >> ../schema.prisma
We format and fix errors
prisma format
You can add the following to your package.json
so that you can just run npm run gen:schema
:
"gen:schema": "cd ./prisma/models && rm -rf ../schema.prisma && cat *.prisma >> ../schema.prisma && cd .. && prisma format",
After this step, just run
npx prisma migrate dev --name init
to apply your changes to your database, and voila ! :)
Top comments (1)
Please check my blog and script here for advanced things:
New Trick – Organizing Prisma Models with the MERN Stack
Github Repo
This script could be help in both model split into multiple files and also for enum & types of Prisma ORM. And there is also a way to import predefined data into database too.
Hope, it would be best & helpful to solve this problem for everyone.
Thanks