-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
28 lines (24 loc) · 812 Bytes
/
index.ts
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
import type { FastifyPluginCallback } from "fastify";
import fp from "fastify-plugin";
import postgres from "postgres";
declare module "fastify" {
interface FastifyInstance {
sql: ReturnType<typeof postgres>;
}
}
export type FastifyPostgresJsOptions<
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
T extends Record<string, postgres.PostgresType<any>> = {}
> = postgres.Options<T>;
const fastifyPostgresJsPlugin: FastifyPluginCallback<
FastifyPostgresJsOptions
> = (fastify, options, done) => {
const sql = postgres(options);
fastify.decorate("sql", sql);
fastify.addHook("onClose", async () => {
await sql.end();
});
done();
};
export default fp(fastifyPostgresJsPlugin);
export const fastifyPostgresJs = fp(fastifyPostgresJsPlugin);