mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 13:17:46 +00:00
25 lines
687 B
TypeScript
25 lines
687 B
TypeScript
import fp from 'fastify-plugin'
|
|
import { FastifyPluginAsync } from 'fastify'
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
// Use TypeScript module augmentation to declare the type of server.prisma to be PrismaClient
|
|
declare module 'fastify' {
|
|
interface FastifyInstance {
|
|
prisma: PrismaClient
|
|
}
|
|
}
|
|
|
|
const prismaPlugin: FastifyPluginAsync = fp(async (server, options) => {
|
|
const prisma = new PrismaClient()
|
|
|
|
await prisma.$connect()
|
|
|
|
// Make Prisma Client available through the fastify server instance: server.prisma
|
|
server.decorate('prisma', prisma)
|
|
|
|
server.addHook('onClose', async server => {
|
|
await server.prisma.$disconnect()
|
|
})
|
|
})
|
|
|
|
export default prismaPlugin
|