34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
// src/db/index.ts
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
import { Pool } from "pg";
|
|
import * as schema from "./schema";
|
|
import {secrets} from "../src/utils/secrets";
|
|
|
|
console.log("[DB INIT] 1. Suche Connection String...");
|
|
|
|
const fallbackConnectionString = "postgres://postgres:wJw7aNpEBJdcxgoct6GXNpvY4Cn6ECqu@fedeo-db-001.vpn.internal:5432/fedeo"
|
|
|
|
// Checken woher die URL kommt
|
|
let connectionString = process.env.DATABASE_URL || secrets.DATABASE_URL || fallbackConnectionString;
|
|
if (process.env.DATABASE_URL) {
|
|
console.log("[DB INIT] -> Gefunden in process.env.DATABASE_URL");
|
|
} else if (secrets.DATABASE_URL) {
|
|
console.log("[DB INIT] -> Gefunden in secrets.DATABASE_URL");
|
|
} else if (connectionString) {
|
|
console.log("[DB INIT] -> Nutze Fallback aus dem Projekt");
|
|
} else {
|
|
console.error("[DB INIT] ❌ KEIN CONNECTION STRING GEFUNDEN! .env nicht geladen?");
|
|
}
|
|
|
|
export const pool = new Pool({
|
|
connectionString,
|
|
max: 10,
|
|
});
|
|
|
|
// TEST: Ist die DB wirklich da?
|
|
pool.query('SELECT NOW()')
|
|
.then(res => console.log(`[DB INIT] ✅ VERBINDUNG ERFOLGREICH! Zeit auf DB: ${res.rows[0].now}`))
|
|
.catch(err => console.error(`[DB INIT] ❌ VERBINDUNGSFEHLER:`, err.message));
|
|
|
|
export const db = drizzle(pool, { schema });
|