Share your Prisma Client across projects

Creating a new Prisma project

Setup a new project with npm init. We'll be publishing this project as a package so use the following naming convention to name it: @YOUR-USERNAME/YOUR-REPOSITORY or @YOUR-ORGANISATION/YOUR-REPOSITORY

  • First create a new typescript project and install the prisma package. Your dependencies should look similar to the ones below:
  • Next create or copy from one of you existing projects the prisma folder and the schema.prisma file. The only bit we care about from the schema.prisma file is the set of code which tells Prisma what we’re using as a datasource — in our case, a PostgreSQL db.
  • Now we can add the following scripts to our package.json file which allow us to generate the schema.prisma by introspecting our db and to update the prisma client:
  • Create a src directory and then create both index.ts and index.d.ts files inside it. We’ll need to create and export our context in our index.ts as below:
  • Last step is to create a postinstall script that builds your ts code and generates the prisma client after you install the newly create package:

Publishing your package

We do this by using github actions to create a new private package for our organisation, a great guide into how to do so can be found here.

Press on Releases and create a new Draft release

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store