File size: 1,108 Bytes
54d9dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d700b
 
54d9dcb
 
06d700b
54d9dcb
06d700b
54d9dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Database Connection for Feedback Service - PostgreSQL (Supabase)
 */
const { Pool } = require('pg');

const DATABASE_URL = process.env.DATABASE_URL || process.env.SUPABASE_DB_URL;

let pool = null;

async function connectDB() {
  if (pool) return pool;
  
  try {
    if (!DATABASE_URL) {
      console.error('❌ Feedback Service: DATABASE_URL is not set');
      return null;
    }

    const useSSL = process.env.PGSSLMODE !== 'disable' && !DATABASE_URL.includes('sslmode=disable');
    
    pool = new Pool({
      connectionString: DATABASE_URL,
      ssl: useSSL ? {
        rejectUnauthorized: false
      } : false
    });

    const client = await pool.connect();
    console.log('✅ Feedback Service: PostgreSQL connected');
    client.release();
    
    return pool;
  } catch (err) {
    console.error('❌ Feedback 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 };