Import source: backend/src/plugins/prisma
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import fp from 'fastify-plugin';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
prisma: PrismaClient;
|
||||
}
|
||||
}
|
||||
|
||||
export const prismaPlugin: FastifyPluginAsync = fp(async (fastify) => {
|
||||
const prisma = new PrismaClient({
|
||||
log: fastify.log.level === 'debug' ? ['query', 'error', 'warn'] : ['error'],
|
||||
});
|
||||
|
||||
await prisma.$connect();
|
||||
|
||||
fastify.decorate('prisma', prisma);
|
||||
|
||||
fastify.addHook('onClose', async (server) => {
|
||||
await server.prisma.$disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
export default prismaPlugin;
|
||||
Reference in New Issue
Block a user