|
1 | | -import { drizzle } from 'drizzle-orm/postgres-js' |
| 1 | +import { PostgresJsDatabase, drizzle } from 'drizzle-orm/postgres-js' |
2 | 2 | import postgres from 'postgres' |
3 | 3 | import { env } from '@/lib/env' |
| 4 | +import * as schema from './schema' |
4 | 5 |
|
5 | 6 | // In production, use the Vercel-generated POSTGRES_URL |
6 | 7 | // In development, use the direct DATABASE_URL |
7 | 8 | const connectionString = env.POSTGRES_URL ?? env.DATABASE_URL |
8 | 9 |
|
9 | | -// Disable prefetch as it is not supported for "Transaction" pool mode |
10 | | -const client = postgres(connectionString, { |
11 | | - prepare: false, |
12 | | - idle_timeout: 30, // Keep connections alive for 30 seconds when idle |
13 | | - connect_timeout: 30, // Timeout after 30 seconds when connecting |
14 | | -}) |
| 10 | +const drizzleClient = drizzle( |
| 11 | + postgres(connectionString, { |
| 12 | + prepare: false, // Disable prefetch as it is not supported for "Transaction" pool mode |
| 13 | + idle_timeout: 30, // Keep connections alive for 30 seconds when idle |
| 14 | + connect_timeout: 30, // Timeout after 30 seconds when connecting |
| 15 | + }), |
| 16 | + { schema }, |
| 17 | +) |
15 | 18 |
|
16 | | -// Export the database client (never null) |
17 | | -export const db = drizzle(client) |
| 19 | +declare global { |
| 20 | + var database: PostgresJsDatabase<typeof schema> | undefined |
| 21 | +} |
| 22 | + |
| 23 | +export const db = global.database || drizzleClient |
| 24 | +if (process.env.NODE_ENV !== 'production') global.database = db |
0 commit comments