Prisma
Using Prisma or any other ORM is a perfect example of how you can use provider hooks to interact with Fastify’s lifecycle.
ts
import { PrismaClient } from '@prisma/client';
import { FastifyInstance } from 'fastify';
// Singleton is not a problem here
const prisma = new PrismaClient({
datasourceUrl: process.env.DATABASE_URL,
log: [
{ emit: 'event', level: 'query' },
{ emit: 'event', level: 'warn' },
{ emit: 'event', level: 'info' },
{ emit: 'event', level: 'error' }
]
});
// Simply returns the prisma instance
export default function (): PrismaClient {
return prisma;
}
// Providers can also have lifecycle hooks, this one connects and
// disconnects from the database and also binds the prisma events
// to the fastify logger
export async function onReady(this: FastifyInstance) {
prisma.$on('error', this.log.error.bind(this.log));
prisma.$on('info', this.log.debug.bind(this.log));
prisma.$on('query', this.log.trace.bind(this.log));
prisma.$on('warn', this.log.warn.bind(this.log));
await prisma.$connect();
}
// This hook is called when the server is shutting down
export async function onClose(this: FastifyInstance) {
await prisma.$disconnect();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
The above example registers a Prisma provider that connects to the database when the server is ready and disconnects when the server is shutting down.
This is a good example of how you can use hooks to interact with the lifecycle of Fastify and your application.
You can then just import the PrismaClient
type in your routes and use it as a parameter.
ts
import { PrismaClient } from '@prisma/client';
export function get(prisma: PrismaClient) {
return prisma.user.findMany();
}
1
2
3
4
5
2
3
4
5