Prisma ORM version 5.15.0 introduced a preview feature that allows developers to organize their Prisma schema into multiple files, enhancing manageability and clarity in larger projects.
Enabling Multi-File Schema Support
To utilize this feature, ensure that both prisma
and @prisma/client
are updated to at least version 5.15.0. Additionally, update the Prisma Visual Studio Code Extension to the latest version to benefit from enhanced IDE support.
In your schema.prisma
file, enable the prismaSchemaFolder
preview feature by adding it to the previewFeatures
field within the generator
block:
generator client {
provider = "prisma-client-js"
previewFeatures = ["prismaSchemaFolder"]
}
Organizing Schema Files
Create a schema
subdirectory within your prisma
directory. You can move your existing schema.prisma
file into this directory and split its contents into multiple .prisma
files based on logical groupings, such as models related to users, posts, or other domains.
For example, you might have:
prisma/
├── schema/
│ ├── user.prisma
│ ├── post.prisma
│ └── comment.prisma
Prisma will automatically combine these files during operations like prisma generate
, so there's no need for manual imports between them.
Best Practices
- Organize by Domain: Group related models into the same file to enhance readability and maintainability.
- Clear Naming Conventions: Use descriptive and concise filenames, such as
user.prisma
orpost.prisma
, to indicate the contents clearly. - Main Schema File: Designate a primary schema file to define
datasource
andgenerator
blocks, ensuring these configurations are easy to locate.
Workflow Integration
After organizing your schema into multiple files, continue using Prisma CLI commands as usual. Commands like prisma generate
, prisma migrate
, and prisma db push
will function seamlessly, processing the combined schema files.
Considerations
This feature is currently in preview. While it offers significant benefits for large projects, it’s advisable to test thoroughly in a development environment before deploying to production. Stay updated with Prisma’s official documentation and release notes for any changes or enhancements to this feature.
By adopting the multi-file schema feature, you can improve the organization and scalability of your Prisma projects, making them more maintainable as they grow in complexity.
Sources
https://www.prisma.io/blog/organize-your-prisma-schema-with-multi-file-support