|
1 | | -// Force IPv4 first (critical for Render) |
2 | | -const dns = require("dns") |
3 | | -dns.setDefaultResultOrder("ipv4first") |
4 | | - |
5 | 1 | const { configDotenv } = require("dotenv") |
6 | 2 | const Sequelize = require("sequelize") |
7 | 3 |
|
8 | 4 | configDotenv() |
9 | 5 |
|
10 | | -// Connection pool configuration |
11 | | -const poolConfig = { |
12 | | - max: 20, |
13 | | - min: 5, |
14 | | - idle: 10000, |
15 | | - acquire: 30000, |
16 | | - evict: 30000, |
17 | | -} |
18 | | - |
19 | | -// IMPORTANT: For Supabase pooler, use DATABASE_URL instead of separate params |
20 | 6 | const sequelize = new Sequelize(process.env.DATABASE_URL, { |
21 | 7 | dialect: "postgres", |
22 | | - protocol: "postgres", |
23 | 8 | logging: false, |
24 | | - |
| 9 | + pool: { |
| 10 | + max: 5, |
| 11 | + min: 0, |
| 12 | + idle: 30000, |
| 13 | + }, |
25 | 14 | dialectOptions: { |
26 | 15 | ssl: { |
27 | 16 | require: true, |
28 | | - rejectUnauthorized: false, // ← FIX self-signed cert error |
| 17 | + rejectUnauthorized: false, |
29 | 18 | }, |
30 | 19 | }, |
31 | | - |
32 | | - pool: poolConfig, |
33 | | - |
34 | | - retry: { |
35 | | - max: 3, |
36 | | - match: [ |
37 | | - /ConnectionError/, |
38 | | - /SequelizeConnectionError/, |
39 | | - /SequelizeConnectionRefusedError/, |
40 | | - /SequelizeHostNotFoundError/, |
41 | | - /SequelizeHostNotReachableError/, |
42 | | - /SequelizeInvalidConnectionError/, |
43 | | - /SequelizeConnectionTimedOutError/, |
44 | | - /TimeoutError/, |
45 | | - ], |
46 | | - }, |
47 | | -}) |
48 | | - |
49 | | -// Retry logic |
50 | | -const connectWithRetry = async (retries = 5, delay = 5000) => { |
51 | | - let retryCount = 0 |
52 | | - |
53 | | - while (retryCount < retries) { |
54 | | - try { |
55 | | - await sequelize.authenticate() |
56 | | - console.log("Database Connected Successfully") |
57 | | - return |
58 | | - } catch (err) { |
59 | | - retryCount++ |
60 | | - console.error(`Connection attempt ${retryCount} failed:`, err.message) |
61 | | - |
62 | | - if (retryCount >= retries) { |
63 | | - console.error("Maximum retry attempts reached. Cannot connect to database.") |
64 | | - throw err |
65 | | - } |
66 | | - |
67 | | - console.log(`Retrying in ${delay}ms...`) |
68 | | - await new Promise((resolve) => setTimeout(resolve, delay)) |
69 | | - } |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -// Start connection |
74 | | -connectWithRetry().catch((err) => { |
75 | | - console.error("Fatal database connection error:", err) |
76 | | - process.exit(1) |
77 | | -}) |
78 | | - |
79 | | -// Clean shutdown |
80 | | -process.on("SIGINT", async () => { |
81 | | - try { |
82 | | - await sequelize.close() |
83 | | - console.log("Database connection closed due to app termination") |
84 | | - process.exit(0) |
85 | | - } catch (error) { |
86 | | - console.error("Error during database disconnection:", error) |
87 | | - process.exit(1) |
88 | | - } |
89 | 20 | }) |
90 | 21 |
|
91 | 22 | module.exports = sequelize |
0 commit comments