25 lines
774 B
TypeScript
25 lines
774 B
TypeScript
// src/plugins/db.ts
|
|
import fp from "fastify-plugin";
|
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
import * as schema from "../../db/schema";
|
|
import { db, pool } from "../../db"; // <--- Importiert jetzt die globale Instanz
|
|
|
|
export default fp(async (server, opts) => {
|
|
|
|
// Wir nutzen die db, die wir in src/db/index.ts erstellt haben
|
|
server.decorate("db", db);
|
|
|
|
// Graceful Shutdown: Wenn Fastify ausgeht, schließen wir den Pool
|
|
server.addHook("onClose", async () => {
|
|
console.log("[DB] Closing connection pool...");
|
|
await pool.end();
|
|
});
|
|
|
|
console.log("[Fastify] Database attached from shared instance");
|
|
});
|
|
|
|
declare module "fastify" {
|
|
interface FastifyInstance {
|
|
db: NodePgDatabase<typeof schema>
|
|
}
|
|
} |