UNPKG

multibridge

Version:

A multi-database connection framework with centralized configuration

42 lines (41 loc) 1.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMySQLConnection = createMySQLConnection; const promise_1 = __importDefault(require("mysql2/promise")); const loggers_1 = __importDefault(require("../utils/loggers")); const envConfig_1 = require("../config/envConfig"); async function createMySQLConnection(config) { try { // Create a connection pool with mysql2. // The `database` should be the tenant-specific schema. const pool = promise_1.default.createPool({ host: config.host, port: config.port, user: config.username, password: config.password, database: config.schema, // Connect directly to the tenant's schema waitForConnections: true, connectionLimit: envConfig_1.envConfig.MYSQL_POOL_MAX, queueLimit: envConfig_1.envConfig.MYSQL_QUEUE_LIMIT, }); // Test the connection by getting one from the pool const connection = await pool.getConnection(); connection.release(); loggers_1.default.info(`Connected to MySQL and created pool for database: ${config.schema}`, { host: config.host, schema: config.schema, connectionLimit: envConfig_1.envConfig.MYSQL_POOL_MAX, }); return pool; } catch (error) { loggers_1.default.error(`Error connecting to MySQL: ${error.message}`, { host: config.host, schema: config.schema, }); throw error; } }