Prisma Compositor

As developers, we often find ourselves working with Prisma, a powerful database toolkit that provides an Object-Relational Mapping (ORM) layer for building applications. Prisma allows us to define our database schema using the Prisma Schema Language, a declarative language that describes the structure and relationships of our data models. However, as our projects grow and become more complex, splitting into multiple Prisma schema files becomes necessary.
// user.prisma fragment TimeStamps { updatedAt DateTime @updatedAt createdAt DateTime @default(now()) } model User { id String @id @default(cuid()) ...TimeStamps }
// blog.prisma model BlogPost { id String @id @default(cuid()) title String body String ...TimeStamps }
To address this challenge, we have developed a handy command-line interface (CLI) tool called Prisma Compositor. This tool simplifies the process of merging multiple Prisma schema files and enables the use of schema fragments by adding a preprocessing layer.
What is Prisma Compositor?
Prisma Compositor is an open-source tool that streamlines the management of multiple Prisma schema files. It allows you to combine schema files located in a specified directory or explicitly list the input files. By running a single command, Prisma Compositor merges these schema files into a single compiled schema file, making it easier to manage and work with larger projects.
Additionally, Prisma Compositor introduces the concept of schema fragments, which are reusable pieces of schema definitions. Using the predefined fragment TimeStamps as an example, you can define common fields like updatedAt and createdAt once and reuse them across multiple data models. Prisma Compositor's preprocessing layer enables the use of fragments, further enhancing code organization and reusability.
How to Get Started
To get started with Prisma Compositor, you can install it via npm. Simply run the following command in your project directory:my-app/ ├─ schemas/ │ ├─ user.prisma │ ├─ commerce.prisma │ ├─ robots.prisma ├─ src/
To merge the Prisma schema files located in the schemas/ directory and generate a compiled schema file named compiled-schema.prisma, you can execute the following command:
Alternatively, if you want to explicitly list the input files, you can do so like this:
Enjoy the benefits of streamlined schema merging and improved code organization in your Prisma projects! To learn more about Prisma Compositor and explore its source code, you can visit the official npm package page.
Happy coding with Prisma Compositor!