DEV Community

Cover image for How I Separated My Prisma ORM Models: Enhancing Modularity and Scalability
Alexandre de Pape
Alexandre de Pape

Posted on

How I Separated My Prisma ORM Models: Enhancing Modularity and Scalability

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
Enter fullscreen mode Exit fullscreen mode

config.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Enter fullscreen mode Exit fullscreen mode

user.prisma

model User {
  id        Int      @id @default(autoincrement())
  username  String   @unique
  email     String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  posts     Post[]
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Breakdown of the command:

We change the current dir

cd ./prisma/models
Enter fullscreen mode Exit fullscreen mode

We delete the content of the main file

rm -rf ../schema.prisma 
Enter fullscreen mode Exit fullscreen mode

We add the content of our models to the main file

cat *.prisma >> ../schema.prisma 
Enter fullscreen mode Exit fullscreen mode

We format and fix errors

prisma format 
Enter fullscreen mode Exit fullscreen mode

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",
Enter fullscreen mode Exit fullscreen mode

After this step, just run

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

to apply your changes to your database, and voila ! :)

Top comments (0)