import path from "path"; import grabDirNames from "../data/grab-dir-names"; import {} from "../types"; import { SQL } from "bun"; /** Reuse one client per distinct connection config to avoid exhausting MariaDB connections. */ const clientCache = new Map(); /** * Cache key covering everything that affects the connection, not just the * database name. */ export function mariadbClientCacheKey(config) { return JSON.stringify({ db_name: config?.db_name || "__default__", db_config: config?.db_config || null, ssl_ca: config?.ssl_ca || null, ssl_reject_unauthorized: config?.ssl_reject_unauthorized ?? null, }); } export default function grabMariaDBClient({ config, }) { const cacheKey = mariadbClientCacheKey(config); const cached = clientCache.get(cacheKey); if (cached) { return cached; } // Prefer the process-wide client when it was created from the same config if (global.MARIADB_CLIENT && global.CONFIG && mariadbClientCacheKey(global.CONFIG) === cacheKey) { clientCache.set(cacheKey, global.MARIADB_CLIENT); return global.MARIADB_CLIENT; } const { ROOT_DIR } = grabDirNames(); try { // Verify the server certificate unless explicitly disabled const rejectUnauthorized = config.ssl_reject_unauthorized !== false; const tls = config.ssl_ca ? { ca: Bun.file(path.resolve(ROOT_DIR, config.ssl_ca)), rejectUnauthorized, } : { rejectUnauthorized, }; const MariaDBClient = new SQL({ hostname: process.env.BUN_MARIADB_SERVER_HOST, username: process.env.BUN_MARIADB_SERVER_USERNAME, password: process.env.BUN_MARIADB_SERVER_PASSWORD, database: config.db_name, port: process.env.BUN_MARIADB_SERVER_PORT ? Number(process.env.BUN_MARIADB_SERVER_PORT) : undefined, tls, ...config.db_config, adapter: "mariadb", }); clientCache.set(cacheKey, MariaDBClient); return MariaDBClient; } catch (error) { console.error(`Couldn't grab MariaDB Client => ` + error.message); return; } }