Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@prisma-utils/prismerge

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prisma-utils/prismerge

A handy CLI to merge multiple `*.prisma` files into one big `schema.prisma` file that can be processed and handled by `Prisma`.

  • 1.7.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
635
decreased by-9.54%
Maintainers
1
Weekly downloads
 
Created
Source

PrisMerge

A handy CLI to merge multiple *.prisma files into one big schema.prisma file that can be processed and handled by Prisma.

Installation

Install the package via

npm i -D @prisma-utils/prismerge

Now you can call

npx prismerge -g -i prismerge.json

to create a default prismerge.json configuration file. This file looks like this:

{
  "app": {
    "inputs": [],
    "fragments": {},
    "output": ""
  }
}

Usage

Now simply add paths to your *.prisma files for inputs, and define the output file, like follows:

{
  "app": {
    "inputs": [
      "./libs/core/prisma/base.prisma",
      "./libs/user/prisma/user.prisma",
      "./libs/article/prisma/article.prisma"
    ],
    "output": "./prisma/schema.prisma"
  }
}

Executing

npx prismerge -i prismerge.json

will read all *.prisma files defined in inputs and merges them into one single schema.prisma file that can be read and processed by Prisma.

Apps

Of course you can add additional apps (i.e., top level element of the prismerge.json file), if you have multiple services.

{
  "auth-service": {
    "inputs": [
      "./libs/auth/core/prisma/base.prisma",
      "./libs/auth/user/prisma/user.prisma"
    ],
    "output": "./prisma/auth/schema.prisma"
  },
  "article-service": {
    "inputs": [
      "./libs/article/core/prisma/base.prisma",
      "./libs/article/article/prisma/article.prisma"
    ],
    "output": "./prisma/article/schema.prisma"
  },
  "log-service": {
    "inputs": [
      "./libs/log/core/prisma/base.prisma",
      "./libs/log/prisma/log.prisma"
    ],
    "output": "./prisma/log/schema.prisma"
  }
}

You can specify to exclude a specific app, via the --excludeApps (-eA) parameter. Running

npx prismerge --oA auth-service article-service

will exclude these specific apps from the generation process.

Globs

PrisMerge also allows to use glob patterns for inputs. Consider the following example:

{
  "app": {
    "inputs": [
      "./libs/*/prisma/*.prisma"
    ],
    "output": "./prisma/schema.prisma"
  }
}

This will, for example, find the prisma files in

  • ./libs/user/prisma/user.prisma
  • ./libs/article/prisma/article.prisma

See the glob docs for more ideas, how this can be used.

Fragments

PrisMerge also allows for defining Fragments, that can be inserted into models. These Fragments can be used to define reoccurring field definitions, like the description for id fields.

Consider the following example for a fragent file:

  id String @id @default(uuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

This information will be used over and over again in all your models.

Unfortunately, Prisma itself does not provide a suitable mechanism for extending / inheriting a base model.

With PrisMerge you can link to *.prisma.fragment files. Fragment placeholders are then replaced during the merge-process with the actual content of these files.

First, define your fragment as follows:

# File: ./my/custom/path/id.prisma.fragment

id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

Second, add the fragment to your prismerge.json file as follows and assign a proper key (i.e., id in this example).

{
  "app": {
    "inputs": [
      "./libs/core/prisma/base.prisma",
      "./libs/user/prisma/user.prisma",
      "./libs/article/prisma/article.prisma"
    ],
    "fragments": {
      "id": "./my/custom/path/id.prisma.fragment"
    },
    "output": "./prisma/schema.prisma"
  }
}

Finally, add the placeholder to your model files, like so:

# File: ./libs/user/prisma/user.prisma

model User {
  ...id

  // additional fields
  email    String @unique
  password String
}

When running

npx prismerge

the placeholders are properly replaced, resulting in the final model

model User {
  id        String   @id @default(uuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  // additional fields
  email    String @unique
  password String
}

Nx Generators

This library also provides nrwl/nx generators that can be used to

  • init prismerge
  • add a new model to the prismerge file
  • add a new fragment to the prismerge file

Respective generators can be easily called via the Nx VSCode Extension or via cli. More information are provided within the description of the generators via

npx nx generate @prisma-utils/prismerge:init --help
npx nx generate @prisma-utils/prismerge:add-model --help
npx nx generate @prisma-utils/prismerge:add-fragment --help

Pro Tips

You can also use a custom npm script to align these commands, like so:

// in your package.json
{
  "scripts": {
    "prisma:generate": "npx prismerge -i prismerge.json && npx prisma generate"
  }
}

This will first create the single schema file and then call the generators defined in the generated schema in one go.

Help

Call

npx prismerge --help

for additional information or configuration options.

Contribution

You can easily create an issue and request additional features or fix bugs.

Running lint

Run nx lint prismerge to execute the lint via ESLint.

Running unit tests

Run nx test prismerge to execute the unit tests via Jest.

FAQs

Package last updated on 10 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc