const { Pool } = require('pg'); const fs = require('fs'); const path = require('path'); // PostgreSQL 연결 풀 생성 const pool = new Pool({ connectionString: process.env.DATABASE_URL, }); // 데이터베이스 초기화 (테이블 생성) async function initializeDatabase() { try { // v2 스키마 사용 (새로운 멀티유저 시스템) const schemaPath = fs.existsSync(path.join(__dirname, 'schema_v2.sql')) ? 'schema_v2.sql' : 'schema.sql'; const schema = fs.readFileSync(path.join(__dirname, schemaPath), 'utf8'); await pool.query(schema); console.log(`✅ Database tables initialized successfully (${schemaPath})`); } catch (error) { console.error('❌ Error initializing database:', error); throw error; } } // 쿼리 실행 헬퍼 함수 async function query(text, params) { const start = Date.now(); try { const res = await pool.query(text, params); const duration = Date.now() - start; console.log('Executed query', { text, duration, rows: res.rowCount }); return res; } catch (error) { console.error('Query error:', error); throw error; } } module.exports = { query, pool, initializeDatabase, };