Lädt...

🔧 Splitting Prisma Schema into Multiple Files: A Simple Guide


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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.

  1. Open your schema.prisma file.
  2. Find the generator client part. It usually looks like this:
generator client {
  provider = "prisma-client-js"
}
  1. 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.

  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

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/"
}

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.

  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/"
}

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! 🚀

...

🔧 Splitting Prisma Schema into Multiple Files: A Simple Guide


📈 68.51 Punkte
🔧 Programmierung

🕵️ Apache Solr bis 5.2 Schema Handler schema-browser.js Cross Site Scripting


📈 27.09 Punkte
🕵️ Sicherheitslücken

🕵️ Apache Solr bis 5.2 Schema Handler schema-browser.js Cross Site Scripting


📈 27.09 Punkte
🕵️ Sicherheitslücken

🔧 CYPRESS-AJV-SCHEMA-VALIDATOR VIDEO TUTORIAL: Mastering API Schema Testing in Cypress


📈 27.09 Punkte
🔧 Programmierung

🔧 Is MongoDB Truly Schema-Less? Exploring Schema Flexibility in NoSQL


📈 27.09 Punkte
🔧 Programmierung

🔧 Understanding Star Schema vs. Snowflake Schema


📈 27.09 Punkte
🔧 Programmierung

🔧 Schema Manager: Centralize Schemas in a Repository with Support for Schema Registry Integration


📈 27.09 Punkte
🔧 Programmierung

🔧 Quick fix: com.github.everit-org.json-schema:org.everit.json.schema:jar:1.12.2 was not found


📈 27.09 Punkte
🔧 Programmierung

🔧 Snowflake Schema vs. Star Schema: Pros, Cons, and Use Cases


📈 27.09 Punkte
🔧 Programmierung

🔧 Handling Schema Versioning and Updates in Event Streaming Platforms Without Schema Registries


📈 27.09 Punkte
🔧 Programmierung

🔧 BigQuery Schema Generation Made Easier with PyPI’s bigquery-schema-generator


📈 27.09 Punkte
🔧 Programmierung

🔧 Snowflake Schema vs. Star Schema: Pros, Cons, and Use Cases


📈 27.09 Punkte
🔧 Programmierung

🔧 schema-org-java 0.4.0 released - Java library for working with Schema.org data in JSON-LD format


📈 27.09 Punkte
🔧 Programmierung

🕵️ Added URL schema validation to prevent URL schema hijacking


📈 27.09 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2020-7742: Simpl-schema project Simpl-schema


📈 27.09 Punkte
🕵️ Sicherheitslücken

🔧 Schema + Data migration in Prisma (adding a new mandatory field)


📈 26.78 Punkte
🔧 Programmierung

🔧 A prisma.schema for an advance Social Media Website


📈 26.78 Punkte
🔧 Programmierung

🕵️ Prisma VS Code up to 2.0.26/2.19.x Schema File prismaFmtBinPath code injection


📈 26.78 Punkte
🕵️ Sicherheitslücken

🔧 Automating Neon schema deployments with Prisma and GitLab


📈 26.78 Punkte
🔧 Programmierung

🔧 Common Data Loss Scenarios & Solutions in Prisma Schema Changes


📈 26.78 Punkte
🔧 Programmierung

🔧 Prisma: Could not parse schema engine response


📈 26.78 Punkte
🔧 Programmierung

🔧 Update to Prisma 5.18.0 in typegraphql-prisma-nestjs


📈 26.46 Punkte
🔧 Programmierung

🔧 Prisma Part 1: Your Easy Tutorial to Set up Prisma


📈 26.46 Punkte
🔧 Programmierung

🔧 #143 — Expand One Row into Multiple Rows after Splitting Text


📈 25.33 Punkte
🔧 Programmierung

📰 Linux Mint Devs Work on Splitting Cinnamon into Multiple Processes, Improvements


📈 25.33 Punkte
📰 IT Security Nachrichten

🎥 HPR4077: FFMPEG Series: Joining and Splitting files


📈 21.1 Punkte
🎥 Podcasts

🔧 How to Use part in Dart – Splitting Files for Scoped Access


📈 21.1 Punkte
🔧 Programmierung

🕵️ Vuln: Multiple IBM Products CVE-2015-2017 HTTP Response Splitting Vulnerability


📈 20.35 Punkte
🕵️ Sicherheitslücken

🕵️ Vuln: Multiple IBM Products CVE-2015-2017 HTTP Response Splitting Vulnerability


📈 20.35 Punkte
🕵️ Sicherheitslücken

📰 Investor Tim Draper Pushes Ballot Measure Splitting California Into 3 States


📈 19.9 Punkte
📰 IT Security Nachrichten

matomo