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
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
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.
- Open your
schema.prisma
file. - Find the
generator client
part. It usually looks like this:
generator client {
provider = "prisma-client-js"
}
- Add one line to it like this:
generator client {
provider = "prisma-client-js"
previewFeatures = ["prismaSchemaFolder"]
}
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")
}
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.
- Inside the
prisma
folder, make a new folder calledschema
. - Move your
schema.prisma
file into this newschema
folder. - Create new files for each model—like
user.prisma
,address.prisma
, ortest.prisma
—inside theschema
folder.
After this, your folder should look like:
prisma
┗ 📂 schema
┣ 📜 user.prisma
┣ 📜 address.prisma
┣ 📜 schema.prisma
┗ 📜 test.prisma
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.
- Open your
package.json
file. - Add this part to it:
"prisma": {
"schema": "./prisma/schema/"
}
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/"
}
}
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.
- Find the
"scripts"
section inpackage.json
. - 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/"
}
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/"
}
}
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
Or, if you use pnpm:
pnpm run db:generate
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! 🚀
Top comments (0)