-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): handle database connection errors and retries
Updated database connection handling in src/db/index.ts to retry connection after a 5-second delay upon encountering a CONNECT_TIMEOUT error. - Updated .env.example with DATABASE_URL configuration - Updated README.md with setup instructions for DATABASE_URL
- Loading branch information
Showing
3 changed files
with
49 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import { drizzle } from 'drizzle-orm/postgres-js'; | ||
import postgres from 'postgres'; | ||
import postgres, { Sql } from 'postgres'; | ||
import { env } from '../config'; | ||
|
||
const client = postgres(env.DATABASE_URL); | ||
export const db = drizzle(client); | ||
import { logger } from '../utils'; | ||
|
||
export * from './schema'; | ||
export * from './queries'; | ||
|
||
let client: Sql | undefined; | ||
|
||
const connectDatabase = async () => { | ||
try { | ||
client = postgres(env.DATABASE_URL); | ||
return drizzle(client); | ||
} catch (error) { | ||
logger.error('Failed to connect to the database:', error as Error); | ||
await reconnectAfterDelay(); | ||
} | ||
}; | ||
|
||
const reconnectAfterDelay = async () => { | ||
await new Promise(resolve => setTimeout(resolve, 5000)); | ||
await connectDatabase(); | ||
}; | ||
|
||
export const db = connectDatabase(); |