Skip to content

Fastify Prisma plugin to share the same PrismaClient across your entire server.

License

Notifications You must be signed in to change notification settings

joggrdocs/fastify-prisma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

fastify-prisma

npm version CI Formatted with Biome

Fastify Prisma plugin to share the same PrismaClient across your entire server.

const allTheDucks = await server.prisma.rubberDucky.findMany();

Requirements

  • fastify >= 5.x
  • @prisma/client >= 6.x
  • prisma set up using the output config πŸ‘‰ see docs

Getting Started

Before using this plugin you will need to have prisma set up. Once you are all set with prisma install the package and register the plugin on your server.

Install the package

npm

npm i @joggr/fastify-prisma

yarn

yarn add @joggr/fastify-prisma

pnpm

pnpm add @joggr/fastify-prisma

Register the plugin

javascript

const fastifyPrisma = require('@joggr/fastify-prisma');
const { PrismaClient } = require('../my-prisma-client');

await fastify.register(fastifyPrisma, {
  client: new PrismaClient(),
});

typescript

import fastifyPrisma from '@joggr/fastify-prisma';
import { PrismaClient } from '../my-prisma-client';

// Add this so you get types across the board
declare module 'fastify' {
  interface FastifyInstance {
    prisma: PrismaClient;
  }
}

await fastify.register(fastifyPrisma, {
  client: new PrismaClient(),
});

[!WARN] Make sure you add in the module declaration or you won't have types!

Accessing the prisma client

async function somePlugin (server, opts) {
  const ducks = await server.prisma.rubberDucky.findMany();

  // do something with the ducks, log for now
  server.log.warn({ ducks }, "πŸ₯πŸ₯ There are lots of ducks! πŸ₯πŸ₯");
}

Tip

You can see a working example of this in the examples directory.

Version Compatibility

Different versions of this library support different versions of fastify and @prisma/client. Please use the version you need based on your project's dependencies.

The table below shows the compatibility matrix.

@joggrdocs/fastify-prisma fastify @prisma/client status
6.x 5.x 6.x active
5.x 5.x 6.x active
4.x 5.x 5.x deprecated
1.x - 3.x 4.x 4.x || 5.x deprecated

License

Licensed under MIT.



⚑️ Want to sign up for Joggr?

You can sign up for free at our website: https://joggr.io
(or click button below πŸ‘‡)

Sign up