DEV Community

Tanzim Hossain
Tanzim Hossain

Posted on

1

Splitting Prisma Schema into Multiple Files: A Simple Guide

Hey buddy! Today, I want to share a solution I found for a common problem with Prisma. By default, Prisma uses just one file called schema.prisma to store all your database models. This can get messy when your project grows. I figured out how to split it into multiple files, and I'll show you how to do it step by step.


What's the Problem?

When you start using Prisma, your folder looks like this:

prisma
 schema.prisma
Enter fullscreen mode Exit fullscreen mode

Everything—your models, settings, all of it—lives in that one schema.prisma file. For small projects, that's okay. But when you add more models, like users, addresses, or tests, it becomes a big, confusing file. I wanted to split it up like this:

prisma
 📂 schema
  📜 user.prisma
  📜 address.prisma
  📜 schema.prisma
  📜 test.prisma
Enter fullscreen mode Exit fullscreen mode

This way, each model gets its own file, and the project stays clean and easy to manage. Here's how I did it.


What You Need Before Starting

  • Prisma installed: You should already have Prisma set up in your project. If not, check the official Prisma guide to install it.
  • Basic Prisma knowledge: You should know what schema.prisma does—like how it defines your database models.

Step-by-Step Guide

Step 1: Turn On the Special Feature

Prisma has a feature that lets you use multiple schema files, but it's not on by default. We need to turn it on.

  1. Open your schema.prisma file.
  2. Find the generator client part. It usually looks like this:
generator client {
  provider = "prisma-client-js"
}
Enter fullscreen mode Exit fullscreen mode
  1. Add one line to it like this:
generator client {
  provider = "prisma-client-js"
  previewFeatures = ["prismaSchemaFolder"]
}
Enter fullscreen mode Exit fullscreen mode

The previewFeatures = ["prismaSchemaFolder"] line tells Prisma, "Hey, let me use multiple files!" Here's what my full schema.prisma looks like after this:

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["prismaSchemaFolder"]
}

datasource db {
  provider = "mongodb"
  url = env("DATABASE_URL")
}
Enter fullscreen mode Exit fullscreen mode

Note: This feature is in "preview," so it might change later. Keep an eye on the Prisma docs for updates.


Step 2: Make a New Folder for Your Files

Now, let's organize your models into separate files.

  1. Inside the prisma folder, make a new folder called schema.
  2. Move your schema.prisma file into this new schema folder.
  3. Create new files for each model—like user.prisma, address.prisma, or test.prisma—inside the schema folder.

After this, your folder should look like:

prisma
 📂 schema
  📜 user.prisma
  📜 address.prisma
  📜 schema.prisma
  📜 test.prisma
Enter fullscreen mode Exit fullscreen mode

The schema.prisma file still holds the generator and datasource parts, while the other files can hold your models (like user or address).


Step 3: Tell Prisma Where Your Files Are

Prisma needs to know where you put your schema files. We'll update the package.json file for this.

  1. Open your package.json file.
  2. Add this part to it:
"prisma": {
  "schema": "./prisma/schema/"
}
Enter fullscreen mode Exit fullscreen mode

Here's what my package.json looks like after adding it:

{
  "name": "@repo/database",
  "version": "0.0.1",
  "dependencies": {
    "@prisma/client": "5.15.0"
  },
  "devDependencies": {
    "prisma": "5.15.0"
  },
  "prisma": {
    "schema": "./prisma/schema/"
  }
}
Enter fullscreen mode Exit fullscreen mode

This line says, "Prisma, look in ./prisma/schema/ for my files." Double-check the path to make sure it matches your folder setup.


Step 4: Update Your Scripts

The last step is to update the commands you run for Prisma, so they know where your files are. We do this in package.json too.

  1. Find the "scripts" section in package.json.
  2. Add the -schema=./prisma/schema/ flag to each Prisma command. Here's an example for MongoDB:
"scripts": {
  "db:generate": "prisma generate -schema=./prisma/schema/",
  "db:push": "prisma db push -schema=./prisma/schema/",
  "db:pull": "prisma db pull -schema=./prisma/schema/",
  "db:studio": "prisma studio -schema=./prisma/schema/"
}
Enter fullscreen mode Exit fullscreen mode

My full package.json now looks like this:

{
  "name": "@repo/database",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "db:generate": "prisma generate -schema=./prisma/schema/",
    "db:push": "prisma db push -schema=./prisma/schema/",
    "db:pull": "prisma db pull -schema=./prisma/schema/",
    "db:studio": "prisma studio -schema=./prisma/schema/"
  },
  "dependencies": {
    "@prisma/client": "5.15.0"
  },
  "devDependencies": {
    "prisma": "5.15.0"
  },
  "prisma": {
    "schema": "./prisma/schema/"
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: I use MongoDB, so my commands fit that. If you use a different database (like PostgreSQL or MySQL), change the db:* commands to match your setup. Just keep the -schema part the same.


Check If It Works

To make sure everything is okay, run a command like:

npx prisma generate
Enter fullscreen mode Exit fullscreen mode

Or, if you use pnpm:

pnpm run db:generate
Enter fullscreen mode Exit fullscreen mode

If it runs without errors, you're all set! Prisma is now using your split files.


Why This is Awesome

Splitting your Prisma schema into multiple files keeps your project neat. Each model has its own file, so it's easy to find and fix things. I hope this guide was simple and clear for you. Try it out in your project, and let me know how it goes in the comments below!

Happy coding, friends! 🚀

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay