Spaces:
Paused
Paused
Download db.js from imkrish/yatra-x-admin: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/imkrish/yatra-x-admin/resolve/main/db.js
- Command line
-
hf download hf://spaces/imkrish/yatra-x-admin/db.js
-
curl -L -o db.js https://huggingface.co/spaces/imkrish/yatra-x-admin/resolve/main/db.js
1.46 kB
| /** | |
| * Database Connection for Admin Service - PostgreSQL (Supabase) | |
| */ | |
| const { Pool } = require('pg'); | |
| const DATABASE_URL = process.env.DATABASE_URL || process.env.SUPABASE_DB_URL; | |
| let pool = null; | |
| function shouldUseSSL(connectionString) { | |
| if (!connectionString) return false; | |
| if (process.env.PGSSLMODE === 'disable' || connectionString.includes('sslmode=disable')) { | |
| return false; | |
| } | |
| // Cloud SQL Unix socket connections already run on local secure transport. | |
| if (connectionString.includes('host=/cloudsql/') || connectionString.includes('/cloudsql/')) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| async function connectDB() { | |
| if (pool) return pool; | |
| try { | |
| if (!DATABASE_URL) { | |
| console.error('❌ Admin Service: DATABASE_URL is not set'); | |
| return null; | |
| } | |
| const useSSL = shouldUseSSL(DATABASE_URL); | |
| pool = new Pool({ | |
| connectionString: DATABASE_URL, | |
| ssl: useSSL ? { | |
| rejectUnauthorized: false | |
| } : false | |
| }); | |
| const client = await pool.connect(); | |
| console.log('✅ Admin Service: PostgreSQL connected'); | |
| client.release(); | |
| return pool; | |
| } catch (err) { | |
| console.error('❌ Admin Service: PostgreSQL connection failed', err.message); | |
| pool = null; | |
| return null; | |
| } | |
| } | |
| function getPool() { | |
| return pool; | |
| } | |
| async function closeDB() { | |
| if (pool) { | |
| await pool.end(); | |
| pool = null; | |
| } | |
| } | |
| module.exports = { connectDB, getPool, closeDB }; | |