Migrate DB driver from Neon-HTTP to node-postgres for improved compatibility
This commit is contained in:
+16
-10
@@ -1,19 +1,25 @@
|
||||
import { drizzle, NeonHttpDatabase } from 'drizzle-orm/neon-http';
|
||||
import { neon } from '@neondatabase/serverless';
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import * as schema from './schema';
|
||||
|
||||
// Best Practice:
|
||||
// In Vercel builds, environment variables might be missing during the "Build" step.
|
||||
// We provide a fallback connection string to allow the code to load and type-check.
|
||||
// The app will fail fast at runtime if it tries to actually query with this invalid URL.
|
||||
// Best Practice for VPS/Self-Hosting:
|
||||
// We use 'pg' (node-postgres) which connects via standard TCP.
|
||||
// This works for local Postgres, Docker, VPS, and managed clouds (AWS RDS, Neon, etc.).
|
||||
|
||||
const connectionString = process.env.DATABASE_URL || 'postgres://placeholder:placeholder@localhost:5432/placeholder';
|
||||
|
||||
const sql = neon(connectionString);
|
||||
// Create a connection pool
|
||||
const pool = new Pool({
|
||||
connectionString,
|
||||
max: 20, // Adjust based on your server capacity
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 2000,
|
||||
});
|
||||
|
||||
// Create the Drizzle client with the specific schema type
|
||||
export const db = drizzle(sql, { schema });
|
||||
// Create the Drizzle client
|
||||
export const db = drizzle(pool, { schema });
|
||||
|
||||
// Helper to check if DB is configured (useful for UI checks)
|
||||
// Helper to check if DB is configured
|
||||
export const isDbAvailable = () => !!process.env.DATABASE_URL;
|
||||
|
||||
// Export schema for use elsewhere
|
||||
|
||||
Reference in New Issue
Block a user