"use strict";
/**
 * TypeORM Configuration
 * Configuración de conexión a PostgreSQL
 *
 * @see https://typeorm.io/data-source-options
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppDataSource = void 0;
exports.initializeDatabase = initializeDatabase;
exports.closeDatabase = closeDatabase;
const typeorm_1 = require("typeorm");
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
exports.AppDataSource = new typeorm_1.DataSource({
    type: 'postgres',
    url: process.env.DATABASE_URL,
    host: process.env.DB_HOST || 'localhost',
    port: parseInt(process.env.DB_PORT || '5432'),
    username: process.env.DB_USER || 'erp_user',
    password: process.env.DB_PASSWORD || 'erp_dev_password',
    database: process.env.DB_NAME || 'erp_construccion',
    synchronize: process.env.DB_SYNCHRONIZE === 'true', // ⚠️ NUNCA en producción
    logging: process.env.DB_LOGGING === 'true',
    entities: [
        __dirname + '/../../modules/**/entities/*.entity{.ts,.js}',
    ],
    migrations: [
        __dirname + '/../../../migrations/*{.ts,.js}',
    ],
    subscribers: [],
    // Configuración de pool
    extra: {
        max: 20, // Máximo de conexiones
        min: 5, // Mínimo de conexiones
        idleTimeoutMillis: 30000,
    },
});
/**
 * Inicializar conexión
 */
async function initializeDatabase() {
    try {
        await exports.AppDataSource.initialize();
        console.log('✅ TypeORM conectado a PostgreSQL');
    }
    catch (error) {
        console.error('❌ Error al conectar TypeORM:', error);
        throw error;
    }
}
/**
 * Cerrar conexión
 */
async function closeDatabase() {
    try {
        await exports.AppDataSource.destroy();
        console.log('✅ Conexión TypeORM cerrada');
    }
    catch (error) {
        console.error('❌ Error al cerrar TypeORM:', error);
        throw error;
    }
}
//# sourceMappingURL=typeorm.config.js.map