Giter Club home page Giter Club logo

prismix's Introduction

Prismix

The Prisma schema mixer 🍜

Made for Prisma v2

Prisma requires your schema to be written in a single file, Prismix allows you to write as many schema files as you'd like, wherever you likeβ€”all while supporting cross-file model relations 🀯

Unlike prisma-merge, Prismix allows model relations to exist between files by combining models and enums, allowing you to extend and override Models as you please. This is ideal when working in a monorepo where parts of your schema need to exist in separate modules.

Installation

  1. Install Prismix
yarn add prismix --dev
  1. Create a prismix.config.json file. This allows you to define how you would like Prismix to merge your schemas.
{
  "mixers": [
    {
        "input": [
            "base.prisma",
            "../lib/auth/auth.prisma", 
            "../lib/posts/posts.prisma",
        ],
        "output": "prisma/schema.prisma"
    }
  ]
}

The order of your input files effects how overrides are considered, the later inputs take priority over the earlier inputs.

The default output value is prisma/schema.prisma (it can be omitted from the config) and Prisma encourages you to keep is as such, especially if you want to use prisma format.

  1. Add the npx prismix command as a prefix to your package.json scripts.
{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "prismix": "npx prismix && prisma format",
    "dev": "yarn prismix && ts-node server.ts",
  }
}

Or just run npx prismix from within the repo that contains the Prismix config file.

Using prisma format is optional, but I like clean code.

Note: If you are using a monorepo you won't be able to run this command from the root, if you add a script "prismix": "npx prismix" you could run yarn workspace my-app prismix.

Example

Let's go over how Prismix merges schemas. We'll keep it simple with two schemas that need to relate to each other.

base.prisma

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

datasource db {
    provider = "postgresql"
    url      = "postgresql://..."
}

model Account {
    id       Int    @id @default(autoincrement())
    username String
    email    String
    status   String

    @@map("accounts")
}

We've established our generator and datasource, as well as our first model, Account.

posts.prisma

Now we'll create the Posts schema in a different file. In order for posts to relate to accounts we can define an empty model to represent the account.

model Posts {
    id         Int     @id @default(autoincrement())
    title      String
    content    String
    account_id Int
    account    Account @relation(fields: [account_id], references: [id])

    @@map("posts")
}

model Account {
    id     String @id
    posts  Post[]
}

When Prismix merges these two schemas the relations will be connected.

schema.prisma

This is the generated file:

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

datasource db {
    provider = "postgresql"
    url      = "postgresql://..."
}

model Account {
    id       Int    @id @default(autoincrement())
    username String
    email    String
    status   String
    posts    Post[]
}

model Posts {
    id         Int     @id @default(autoincrement())
    title      String
    content    String
    account_id Int
    account    Account @relation(fields: [account_id], references: [id])

    @@map("posts")
}

As you can see the property posts was added on to the original Account schema and the account relation on the Posts schema links to the original Account schema.

How it works

Using the Prisma SDK we parse the input schemas into a DMMF objects, then process the schema merge into a single DMMF object, finally it is converted back into Prisma schema format manually using deserializer code I found on IBM's GitHub... and cleaned up massively.

To-do

  • Make it work
  • Add glog support for wildcard schema discovery
  • Make prismix.config.json optional
  • Add command flags

Created by @jamiepine

prismix's People

Contributors

jamiepine avatar

Stargazers

Martijn avatar Andrew N avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.