import "server-only";
import mysql from "mysql2/promise";

let pool;

function createPool() {
  return mysql.createPool({
    host: process.env.DB_HOST || "127.0.0.1",
    port: Number(process.env.DB_PORT || 3306),
    user: process.env.DB_USER || "root",
    password: process.env.DB_PASSWORD || "",
    database: process.env.DB_NAME || "codesite_codesitex",
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
  });
}

export function getDbPool() {
  if (!pool) {
    pool = createPool();
  }

  return pool;
}
