Code Monkey home page Code Monkey logo

prisma-multischema's Introduction

NPM NPM Tests NPM Snyk

Prisma: MultiSchema NPM

Prisma normally limits your schema to one file, but with prisma-multischema, you can write multiple prisma schema files in an organized manner without any restrictions.

For Multiple files inter-relation you can import schemas , to manage the relation.

Built using TypeScript to for ES Module and CommonJS (CJS), to Unify Multiple Structured Schemas of Prisma-ORM

Installation

npm i prisma-multischema
yarn add prisma-multischema

Note Using VS Code ? Install Recommended VsCode Extensions from Dependencies (optional)

Usage

  • How to Use Tutorial : πŸ“šMediumBlog || ✨YT Link

  • Place all your schemas in ProjectRoot/prisma/subschemas Folder.
    Like this :

    project_root 
        β”œβ”€β”€β”€node_modules
        β”œβ”€β”€β”€prisma 
        β”‚   β”œβ”€β”€β”€subschemas <<<-----Place all your Schemas here
        β”‚   β”‚   β”œβ”€β”€β”€type 
        β”‚   β”‚   β”‚    └───user.types.prisma
        β”‚   β”‚   β”‚    └───bookmark.types.prisma
        β”‚   β”‚   └───user
        β”‚   β”‚   β”‚    └───userData.prisma
        β”‚   β”‚   β”‚    └───validity.prisma
        β”‚   β”‚   β”œβ”€β”€β”€anything-you-want.prisma
        β”‚   β”‚   β”œβ”€β”€β”€base.prisma  
        |   |   └───...  
        β”‚   └───schema.prisma   <-- will be Auto-Generated
        β”œβ”€β”€β”€src
        β”‚   └───...
        β”œβ”€β”€β”€package.json
        β”‚        
        └───.gitignore

    For Clearer View : Image

  • Run in Terminal

    npx prisma-multischema

Project Demonstration

working example is available below -

Example

Let's go with two schemas User and Bookmark on different files ,where the relation is -

  • A User can have many bookmarks
  • Each bookmark has an userId field

base.prisma [ root/prisma/subschemas/base.prisma ]

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

datasource db {
  provider = "mongodb"
  url      = env("PRISMA_DATABASE_URL")
}

user.prisma [ root/prisma/subschemas/User/user.prisma ]

import { Bookmark } from "..\Bookmark\bookmark"
model User {

    id String @id @default(auto()) @map("_id") @db.ObjectId
    email String @unique

    Bookmark Bookmark[]
}
//MongoDB model IDs in prisma -must have a @map("_id") 
//https://www.prisma.io/docs/concepts/components/prisma-schema

bookmark.prisma [ root/prisma/subschemas/Bookmark/bookmark.prisma ]

import {  User } from "..\User\user"
model Bookmark {

    id String     @id  @db.ObjectId @default(auto()) @map("_id") 
    title String
    
    user  User    @relation(fields: [userId], references: [id])
    userId String @db.ObjectId
}

Generated schema.prisma [root/prisma/schema.prisma]
after Running npx prisma-multischema

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

datasource db {
  provider = "mongodb" 
  url      = env("PRISMA_DATABASE_URL")
}

model User {
  id       String     @id @default(auto()) @map("_id") @db.ObjectId
  email    String     @unique
  Bookmark Bookmark[]
}

model Bookmark {
  id          String  @id @default(auto()) @map("_id") @db.ObjectId 
  title       String
  user        User    @relation(fields: [userId], references: [id])
  userId      String  @db.ObjectId
}

https://www.prisma.io/docs

Additional

  • prisma schema files starting with header //#exclude will be excluded in final schema

  • Executing npx prisma-multischema will

    • Automatically run : npx prisma generate
      So, You don't need to update @prisma/client manually, each time the schema updates
    • Automatically run : npx prisma format
      because, Everyone likes clean code
  • Add npx prisma-multischema command as a prefix to your start script in package.json.

    {
    "name": "my-app",
    "version": "1.0.0",
    "scripts": {
        "unify": "npx prisma-multischema",
        "start": "npm run unify && node index.js",
        ...
      }
    }


    Now it will run & regenerate Main Schema everytime the project starts.

Dependencies (optional)

To use prisma import feature : (if you are using VS code, its better to use these)

  • Install prisma-import Extension (for VS code)

  • Disable Official prisma Extension (for VS code)

These are Optional Dependencies, If you can maintain multiple *.prisma schemas without TYPO ,you can ignore these.

To-do's

  • Add Support for keeping prisma's in different folder and aggregate them ( like root/src/auth/auth.prisma )

  • Add Command Flags

  • Handle/Remove " Error validating datasource db: " Warning Fixed

Authors - @joydip007x

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.