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

@sabinthedev/fastify-prisma

v1.0.25

Published

A plugin for Fastify to easily register Prisma Client in your Fastify instance

Downloads

3

Readme

Prisma + Fastify

Header

npm version Tests/Linting JavaScript Style Guide semantic-release

NPM

Installation

To use this package, first install it:

npm i @sabinthedev/fastify-prisma

Usage

In order to use this package, you will need Prisma set up in your project and Prisma Client generated. This plugin currently relies on Prisma Client being in node_modules/@prisma/client, (the default output location of the generated client).

import prismaPlugin from '@sabinthedev/fastify-prisma'
import Fastify from 'fastify'

const fastify = Fastify()

fastify.register(prismaPlugin)

fastify.get('/', async () => {
  // You will get nice intellisense here 👇🏻
  const users = await fastify.prisma.user.findMany()
  return { users }
})

fastify.listen({ port: 3000 }, (err, address) => {
  console.info(`⚡️ Server running at ${address}`)
})

You can also provide an object that contains Prisma Client's constructor arguments. For example, if you would like to manually set the datasource:


fastify.register(prismaPlugin, {
  datasources: {
    db: {
      url: 'file:./dev.db'
    }
  }
})

Caveats

There are a few caveats with this plugin which, at the moment, have no workarounds I am aware of:

Can't use a custom output directory for Prisma Client

In Prisma, you have the ability via the Prisma schema to change the output directory of the generated client, which defaults to node_modules/@prisma/client.

This plugin, however, relies on that client being in the default location.

Only a single Prisma Client can be used

You can set up different schemas in Prisma and generate Prisma Client for each schema. This requires using custom outputs, which is not supported in this plugin for the reason detailed above.

Prisma Client options may result in unexpected problems with typings & intellisense

Because the plugin has to look in the node_modules directory for Prisma Client and registers the prisma decorator with the type PrismaClient, certain actions that adjust the type of PrismaClient will not register correctly in the Fastify instance.

For example, while configuring logging in PrismaClient:

const prisma = new PrismaClient({
  log: [{ emit: 'event', level: 'info' }]
})

This will actually adjust the PrismaClient type and give you the ability to hook into events:

prisma.$on('info', (e) => {
  console.log(e)
})

Passing those logging configurations with this plugin will not adjust the type of the prisma decorator on the Fastify instance, resulting in a type error if you attempt to use the code above.

Just to clarify, this will affect any operation that adjusts the Prisma Client type.

Author

I'm Sabin Adams!

Contributors

None yet! But contributions are welcome!

License

Licensed under MIT.