How I Separated My Prisma ORM Models: Enhancing Modularity and Scalability
Tags: prisma, webdev, tutorial
Published: 2023-05-10 13:44:27

##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:

```javascript
mkdir prisma/models
touch prisma/models/config.prisma
touch prisma/models/user.prisma
touch prisma/models/post.prisma
```
###config.prisma

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

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

```
###user.prisma

```sql
model User {
  id        Int      @id @default(autoincrement())
  username  String   @unique
  email     String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  posts     Post[]
}
```

###post.prisma

```sql
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

```js
cd ./prisma/models
```
We delete the content of the main file

```js
rm -rf ../schema.prisma 
```
We add the content of our models to the main file
```js
cat *.prisma >> ../schema.prisma 
```
We format and fix errors
```js
prisma format 
```

You can add the following to your `package.json` so that you can just run `npm run gen:schema`:

```bash
"gen:schema": "cd ./prisma/models && rm -rf ../schema.prisma && cat *.prisma >> ../schema.prisma && cd .. && prisma format",
```

After this step, just run 

```bash 
npx prisma migrate dev --name init
```
to apply your changes to your database, and voila ! :)