128 lines
4.1 KiB
TypeScript
128 lines
4.1 KiB
TypeScript
import mariadb, { Connection, ConnectionConfig } from "mariadb";
|
|
import grabDbSSL from "./backend/grabDbSSL";
|
|
|
|
type Param = {
|
|
/**
|
|
* No Database Connection
|
|
*/
|
|
noDb?: boolean;
|
|
/**
|
|
* Database Name
|
|
*/
|
|
database?: string;
|
|
/**
|
|
* Debug
|
|
*/
|
|
config?: ConnectionConfig;
|
|
};
|
|
|
|
/**
|
|
* # Grab General CONNECTION for DSQL
|
|
*/
|
|
export default async function grabDSQLConnection(
|
|
param?: Param
|
|
): Promise<Connection> {
|
|
const config: ConnectionConfig = {
|
|
host: process.env.DSQL_DB_HOST,
|
|
user: process.env.DSQL_DB_USERNAME,
|
|
password: process.env.DSQL_DB_PASSWORD,
|
|
database:
|
|
param?.database ||
|
|
(param?.noDb ? undefined : process.env.DSQL_DB_NAME),
|
|
port: process.env.DSQL_DB_PORT
|
|
? Number(process.env.DSQL_DB_PORT)
|
|
: undefined,
|
|
charset: "utf8mb4",
|
|
ssl: grabDbSSL(),
|
|
bigIntAsNumber: true,
|
|
supportBigNumbers: true,
|
|
bigNumberStrings: false,
|
|
dateStrings: true,
|
|
metaAsArray: true,
|
|
...param?.config,
|
|
};
|
|
|
|
try {
|
|
return await mariadb.createConnection(config);
|
|
} catch (error) {
|
|
console.log(`Error Grabbing DSQL Connection =>`, config);
|
|
throw error;
|
|
}
|
|
|
|
// if (global.DSQL_USE_LOCAL || param?.local) {
|
|
// return (
|
|
// global.DSQL_DB_CONN ||
|
|
// (await mariadb.createConnection({
|
|
// host: process.env.DSQL_DB_HOST,
|
|
// user: process.env.DSQL_DB_USERNAME,
|
|
// password: process.env.DSQL_DB_PASSWORD,
|
|
// database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
|
// port: process.env.DSQL_DB_PORT
|
|
// ? Number(process.env.DSQL_DB_PORT)
|
|
// : undefined,
|
|
// charset: "utf8mb4",
|
|
// ssl: grabDbSSL(),
|
|
// supportBigNumbers: true,
|
|
// bigNumberStrings: false,
|
|
// dateStrings: true,
|
|
// }))
|
|
// );
|
|
// }
|
|
|
|
// if (param?.ro) {
|
|
// return (
|
|
// global.DSQL_READ_ONLY_DB_CONN ||
|
|
// (await mariadb.createConnection({
|
|
// host: process.env.DSQL_DB_HOST,
|
|
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
|
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
|
// port: process.env.DSQL_DB_PORT
|
|
// ? Number(process.env.DSQL_DB_PORT)
|
|
// : undefined,
|
|
// charset: "utf8mb4",
|
|
// ssl: grabDbSSL(),
|
|
// supportBigNumbers: true,
|
|
// bigNumberStrings: false,
|
|
// dateStrings: true,
|
|
// }))
|
|
// );
|
|
// }
|
|
|
|
// if (param?.fa) {
|
|
// return (
|
|
// global.DSQL_FULL_ACCESS_DB_CONN ||
|
|
// (await mariadb.createConnection({
|
|
// host: process.env.DSQL_DB_HOST,
|
|
// user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
|
// password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
|
// port: process.env.DSQL_DB_PORT
|
|
// ? Number(process.env.DSQL_DB_PORT)
|
|
// : undefined,
|
|
// charset: "utf8mb4",
|
|
// ssl: grabDbSSL(),
|
|
// supportBigNumbers: true,
|
|
// bigNumberStrings: false,
|
|
// dateStrings: true,
|
|
// }))
|
|
// );
|
|
// }
|
|
|
|
// return (
|
|
// global.DSQL_DB_CONN ||
|
|
// (await mariadb.createConnection({
|
|
// host: process.env.DSQL_DB_HOST,
|
|
// user: process.env.DSQL_DB_USERNAME,
|
|
// password: process.env.DSQL_DB_PASSWORD,
|
|
// database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
|
// port: process.env.DSQL_DB_PORT
|
|
// ? Number(process.env.DSQL_DB_PORT)
|
|
// : undefined,
|
|
// charset: "utf8mb4",
|
|
// ssl: grabDbSSL(),
|
|
// supportBigNumbers: true,
|
|
// bigNumberStrings: false,
|
|
// dateStrings: true,
|
|
// }))
|
|
// );
|
|
}
|