13

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?

Hasibur Rahman
  • 641
  • 2
  • 7
  • 18

5 Answers5

8

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

Ryan Dennler
  • 219
  • 2
  • 5
4

This is not yet possible with Prisma. See this outstanding issue for possible workarounds https://github.com/prisma/prisma/issues/2377.

Austin Crim
  • 739
  • 4
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 18 '22 at 02:14
3

prisma-import is a relatively recent solution and, in my opinion, the best one. It has its own branch of the Prisma VSCode extension for a nice workflow, using import statements analogous to JS imports.

1

There is a library called Prismix that allows you to write as many schema files as you'd like, here you go the link

luisbar
  • 676
  • 5
  • 11
1

You can merge all schemas into one before generating

yarn add -g glob

// package.json
"scripts": {
  "prisma-concat": "npx ts-node prisma/concat-schemas.ts && npx prisma format",
  "generate": "yarn prisma-concat && npx prisma generate",
}
// connect-db.prisma

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

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
// prisma/concat-schemas.ts

import { appendFile, readFile, writeFile } from 'fs/promises'
import { glob } from 'glob'

const start = async () => {
  const schemaFile = 'prisma/schema.prisma'
  const connectFile = 'prisma/connect-db.prisma'
  const models = await glob('src/**/*.prisma')
  const files = [connectFile, ...models]

  await writeFile(schemaFile, '')

  await Promise.all(
    files.map(async (path) => {
      const content = await readFile(path)
      return appendFile(schemaFile, content.toString())
    }),
  )
}
start()

And run yarn generate

Kiritushka
  • 770
  • 1
  • 6
  • 11