File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { drizzle } from 'drizzle-orm/postgres-js' ;
2+ import postgres from 'postgres' ;
3+ import * as schema from './schema' ;
4+
5+ // Lazy-loaded database connection
6+ let dbInstance : ReturnType < typeof drizzle > | null = null ;
7+
8+ export const getDb = ( ) => {
9+ if ( ! dbInstance ) {
10+ const connectionString = process . env . DATABASE_URL ;
11+
12+ if ( ! connectionString ) {
13+ throw new Error ( 'DATABASE_URL environment variable is not set' ) ;
14+ }
15+
16+ try {
17+ const client = postgres ( connectionString , {
18+ prepare : false ,
19+ ssl : 'require' ,
20+ max : 1 ,
21+ idle_timeout : 20 ,
22+ connect_timeout : 10 ,
23+ } ) ;
24+ dbInstance = drizzle ( client , { schema } ) ;
25+ } catch ( error ) {
26+ console . error ( 'Failed to create database client:' , error ) ;
27+ throw error ;
28+ }
29+ }
30+
31+ return dbInstance ;
32+ } ;
33+
34+ // For backward compatibility
35+ export const db = getDb ( ) ;
36+
37+ // Export types
38+ export type Database = typeof db ;
Original file line number Diff line number Diff line change 1+ import { defineConfig } from 'drizzle-kit' ;
2+ import * as dotenv from 'dotenv' ;
3+
4+ dotenv . config ( ) ;
5+
6+ export default defineConfig ( {
7+ schema : './app/IPC/db/schema.ts' ,
8+ out : './app/IPC/db/migrations' ,
9+ dialect : 'postgresql' ,
10+ dbCredentials : {
11+ url : process . env . DATABASE_URL ! ,
12+ } ,
13+ } ) ;
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ async function runMigrations() {
2121 console . log ( '🔄 Connecting to database...' ) ;
2222
2323 const sql = postgres ( connectionString , {
24+ prepare : false ,
2425 ssl : 'require' ,
2526 max : 1 ,
2627 } ) ;
You can’t perform that action at this time.
0 commit comments