DB Restructuring
This commit is contained in:
@@ -1,31 +1,25 @@
|
|||||||
import fp from "fastify-plugin"
|
// src/plugins/db.ts
|
||||||
import {drizzle, NodePgDatabase} from "drizzle-orm/node-postgres"
|
import fp from "fastify-plugin";
|
||||||
import * as schema from "../../db/schema"
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||||
import {secrets} from "../utils/secrets";
|
import * as schema from "../../db/schema";
|
||||||
import { Pool } from "pg"
|
import { db, pool } from "../../db"; // <--- Importiert jetzt die globale Instanz
|
||||||
|
|
||||||
export default fp(async (server, opts) => {
|
export default fp(async (server, opts) => {
|
||||||
|
|
||||||
const pool = new Pool({
|
// Wir nutzen die db, die wir in src/db/index.ts erstellt haben
|
||||||
connectionString: secrets.DATABASE_URL,
|
server.decorate("db", db);
|
||||||
max: 10, // je nach Last
|
|
||||||
})
|
|
||||||
|
|
||||||
const db = drizzle(pool , {schema})
|
// Graceful Shutdown: Wenn Fastify ausgeht, schließen wir den Pool
|
||||||
|
|
||||||
// Dekorieren -> überall server.db
|
|
||||||
server.decorate("db", db)
|
|
||||||
|
|
||||||
// Graceful Shutdown
|
|
||||||
server.addHook("onClose", async () => {
|
server.addHook("onClose", async () => {
|
||||||
await pool.end()
|
console.log("[DB] Closing connection pool...");
|
||||||
})
|
await pool.end();
|
||||||
|
});
|
||||||
|
|
||||||
console.log("Drizzle database connected")
|
console.log("[Fastify] Database attached from shared instance");
|
||||||
})
|
});
|
||||||
|
|
||||||
declare module "fastify" {
|
declare module "fastify" {
|
||||||
interface FastifyInstance {
|
interface FastifyInstance {
|
||||||
db:NodePgDatabase<typeof schema>
|
db: NodePgDatabase<typeof schema>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user