43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import * as mariadb from "mariadb";
|
|
import type { Connection } from "mariadb";
|
|
import type { DsqlConnectionParam } from "../types";
|
|
import grabDbSSL from "./grab-db-ssl";
|
|
|
|
/**
|
|
* # Grab General CONNECTION for DSQL
|
|
*/
|
|
export default async function grabDBConnection(
|
|
param?: DsqlConnectionParam,
|
|
): Promise<Connection> {
|
|
const configData = global.CONFIG;
|
|
const CONN_TIMEOUT = configData?.connection_timeout || 10000;
|
|
|
|
const config: mariadb.ConnectionConfig = {
|
|
host: process.env.BUN_MARIADB_SERVER_HOST,
|
|
user: process.env.BUN_MARIADB_SERVER_USERNAME,
|
|
password: process.env.BUN_MARIADB_SERVER_PASSWORD,
|
|
database: configData?.db_name,
|
|
port: process.env.BUN_MARIADB_SERVER_PORT
|
|
? Number(process.env.BUN_MARIADB_SERVER_PORT)
|
|
: undefined,
|
|
charset: configData?.charset || "utf8mb4",
|
|
ssl: grabDbSSL(),
|
|
bigIntAsNumber: true,
|
|
supportBigNumbers: true,
|
|
bigNumberStrings: false,
|
|
dateStrings: true,
|
|
metaAsArray: true,
|
|
socketTimeout: CONN_TIMEOUT,
|
|
connectTimeout: CONN_TIMEOUT,
|
|
compress: true,
|
|
...param?.config,
|
|
};
|
|
|
|
try {
|
|
return await mariadb.createConnection(config);
|
|
} catch (error) {
|
|
console.log(`Error Grabbing DSQL Connection =>`, config);
|
|
throw error;
|
|
}
|
|
}
|