Update `server/db.ts` to support both Neon serverless and standard PostgreSQL drivers, add database health checks, and improve error logging for database operations. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 55ee188b-6bb4-49b0-8966-1795106363b1 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/C4ZJnmQ
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-serverless';
|
|
import { drizzle as drizzleNode } from 'drizzle-orm/node-postgres';
|
|
import { Pool as NeonPool, neonConfig } from '@neondatabase/serverless';
|
|
import pg from 'pg';
|
|
import type { Pool as PgPoolType } from 'pg';
|
|
import ws from "ws";
|
|
import * as schema from "@shared/schema";
|
|
|
|
const { Pool: PgPool } = pg;
|
|
|
|
if (!process.env.DATABASE_URL) {
|
|
throw new Error(
|
|
"DATABASE_URL must be set. Did you forget to provision a database?",
|
|
);
|
|
}
|
|
|
|
// Check if running on Replit (Neon) or AlmaLinux (PostgreSQL)
|
|
const isNeon = process.env.DATABASE_URL.includes('neon.tech') ||
|
|
process.env.DATABASE_URL.includes('neon.aws');
|
|
|
|
let pool: NeonPool | PgPoolType;
|
|
let db: ReturnType<typeof drizzleNeon> | ReturnType<typeof drizzleNode>;
|
|
|
|
if (isNeon) {
|
|
// Neon serverless (Replit)
|
|
neonConfig.webSocketConstructor = ws;
|
|
pool = new NeonPool({ connectionString: process.env.DATABASE_URL });
|
|
db = drizzleNeon(pool, { schema });
|
|
console.log('📦 Using Neon serverless database');
|
|
} else {
|
|
// PostgreSQL standard (AlmaLinux)
|
|
pool = new PgPool({ connectionString: process.env.DATABASE_URL });
|
|
db = drizzleNode(pool, { schema });
|
|
console.log('🐘 Using standard PostgreSQL database');
|
|
}
|
|
|
|
// Health check: test database connection on startup
|
|
pool.query('SELECT 1')
|
|
.then(() => console.log('✅ Database connection successful'))
|
|
.catch((err) => {
|
|
console.error('❌ Database connection failed:', err.message);
|
|
// Mask entire connection string for security
|
|
const dbType = isNeon ? 'Neon Cloud' : 'PostgreSQL';
|
|
console.error(`Database type: ${dbType}`);
|
|
});
|
|
|
|
export { pool, db };
|