Fastify Prisma plugin to share the same PrismaClient
across your entire server.
const allTheDucks = await server.prisma.rubberDucky.findMany();
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.
npm
npm i @joggr/fastify-prisma
yarn
yarn add @joggr/fastify-prisma
You have two options for registering the plugin:
- Use the plugin as-is or pass in your own configs
- Create a separate client and pass it in (great if you are using a monorepo or other more unique use cases)
javascript
const { PrismaClient } = require('@prisma/client');
const fastifyPrisma = require('@joggr/fastify-prisma');
// Default
await fastify.register(fastifyPrisma);
// With client
await fastify.register(fastifyPrisma, {
client: new PrismaClient(),
});
// with options
await fastify.register(fastifyPrisma, {
clientConfig: {
log: [{ emit: 'event', level: 'query' }]
}
});
typescript
import { PrismaClient } from '@prisma/client';
import fastifyPrisma from '@joggr/fastify-prisma';
// Default
await fastify.register(fastifyPrisma);
// With client
await fastify.register(fastifyPrisma, {
client: new PrismaClient(),
});
// with options
await fastify.register(fastifyPrisma, {
clientConfig: {
log: [{ emit: 'event', level: 'query' }]
}
});
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! 🐥🐥");
}
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 |
---|---|---|---|
5.x |
5.x |
6.x |
active |
4.x |
5.x |
5.x |
active |
1.x - 3.x |
4.x |
4.x || 5.x |
deprecated |
Licensed under MIT.
We'd love to have you join, but we are in closed beta.
You can join our waitlist below.