npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@prisma-utils/prismerge

v1.7.0

Published

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

Downloads

3,887

Readme

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.