Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split Prisma Model into separate file?

I'm learning Prisma ORM from video tutorials and official docs. They are explain and write All model code in one file called schema.prisma. It's ok but, when application grow it became messy. So, how should I separate my Model definition into separate file?

like image 921
Hasibur Rahman Avatar asked Nov 21 '25 14:11

Hasibur Rahman


1 Answers

At this point in time Prisma doesn't support file segmentation. I can recommend 3 solutions though.

Option 1: Prismix

Prismix utilizes models and enums to create relations across files for your Prisma schema via a prismix configuration file.

{
  "mixers": [
    {
        "input": [
            "base.prisma",
            "./modules/auth/auth.prisma", 
            "./modules/posts/posts.prisma",
        ],
        "output": "prisma/schema.prisma"
    }
  ]
}

Placing this inside of a prismix.config.json file which will define how you'd like to merge your Prisma segmentations.

Option 2: Schemix

Schemix Utilizes Typescript configurations to handle schema segmenting.

For example:

// _schema.ts
import { createSchema } from "schemix";

export const PrismaSchema = createSchema({
  datasource: {
    provider: "postgresql",
    url: {
      env: "DATABASE_URL"
    },
  },
  generator: {
    provider: "prisma-client-js",
  },
});

export const UserModel = PrismaSchema.createModel("User");

import "./models/User.model";

PrismaSchema.export("./", "schema");

Inside of User.model

// models/User.model.ts

import { UserModel, PostModel, PostTypeEnum } from "../_schema";

UserModel
  .string("id", { id: true, default: { uuid: true } })
  .int("registrantNumber", { default: { autoincrement: true } })
  .boolean("isBanned", { default: false })
  .relation("posts", PostModel, { list: true })
  .raw('@@map("service_user")');

This will then generate your prisma/schema.prisma containing your full schema. I used only one database as an example (taken from documentation) but you should get the point.

Option 3: Cat -> Generate

Segmenting your schema into chunk part filenames and run:

cat *.part.prisma > schema.prisma
yarn prisma generate

Most of these if not all of them are referenced here in the currently Open issue regarding support for Prisma schema file segmentation https://github.com/prisma/prisma/issues/2377

like image 144
Ryan Dennler Avatar answered Nov 23 '25 03:11

Ryan Dennler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!