Skip to content

Commit 3cf8a8a

Browse files
fix database connection for Supabase with prepare false and dotenv loading
1 parent 8509404 commit 3cf8a8a

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

app/IPC/db/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;

drizzle.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
});

scripts/migrate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)