@zrosenbauer/fastify-prisma
v7.0.2
Published
Fastify Prisma plugin connection to share the same connection across your entire server.
Maintainers
Readme
fastify-prisma
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.xprismaset up using theoutputconfig 👉 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 @zrosenbauer/fastify-prismayarn
yarn add @zrosenbauer/fastify-prismapnpm
pnpm add @zrosenbauer/fastify-prismaRegister the plugin
javascript
const fastifyPrisma = require("@zrosenbauer/fastify-prisma");
const { PrismaClient } = require("../my-prisma-client");
await fastify.register(fastifyPrisma, {
client: new PrismaClient(),
});typescript
import fastifyPrisma from "@zrosenbauer/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(),
});[!WARNING] 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.
| @zrosenbauer/fastify-prisma | fastify | @prisma/client | status |
| ---------------------------------- | --------- | ---------------- | ----------------------------------------------------- |
| 7.x | 5.x | 7.x | active |
| @joggr/fastify-prisma <= 7.0.1 | — | — | deprecated — renamed to @zrosenbauer/fastify-prisma |
License
Licensed under MIT.
