datasquirel/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.ts

125 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-11-06 06:26:23 +00:00
// @ts-check
2025-01-10 19:10:28 +00:00
import fs from "fs";
import path from "path";
2024-11-06 06:26:23 +00:00
2025-01-10 19:10:28 +00:00
import mysql from "serverless-mysql";
import grabDbSSL from "../grabDbSSL";
2024-11-06 06:26:23 +00:00
let DSQL_USER = mysql({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
charset: "utf8mb4",
2024-11-07 14:28:28 +00:00
ssl: grabDbSSL(),
2024-11-06 06:26:23 +00:00
},
});
2025-01-10 19:10:28 +00:00
type Param = {
paradigm: "Full Access" | "FA" | "Read Only";
database: string;
queryString: string;
queryValues?: string[];
};
2024-11-06 06:26:23 +00:00
/**
2025-01-10 19:10:28 +00:00
* # DSQL user read-only DB handler
2024-11-06 06:26:23 +00:00
*/
2025-01-10 19:10:28 +00:00
export default function DSQL_USER_DB_HANDLER({
2024-11-06 06:26:23 +00:00
paradigm,
database,
queryString,
queryValues,
2025-01-10 19:10:28 +00:00
}: Param) {
2024-11-06 06:26:23 +00:00
try {
return new Promise((resolve, reject) => {
const fullAccess = paradigm?.match(/full.access|^fa$/i)
? true
: false;
try {
if (fullAccess) {
DSQL_USER = mysql({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
database: database,
2024-11-07 14:28:28 +00:00
ssl: grabDbSSL(),
2024-11-06 06:26:23 +00:00
},
});
} else {
DSQL_USER = mysql({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
database: database,
2024-11-07 14:28:28 +00:00
ssl: grabDbSSL(),
2024-11-06 06:26:23 +00:00
},
});
}
/**
* ### Run query Function
* @param {any} results
*/
2025-01-10 19:10:28 +00:00
function runQuery(results: any) {
2024-11-06 06:26:23 +00:00
DSQL_USER.end();
resolve(JSON.parse(JSON.stringify(results)));
}
/**
* ### Query Error
* @param {any} err
*/
2025-01-10 19:10:28 +00:00
function queryError(err: any) {
2024-11-06 06:26:23 +00:00
DSQL_USER.end();
resolve({
error: err.message,
queryStringGenerated: queryString,
queryValuesGenerated: queryValues,
sql: err.sql,
});
}
if (
queryValues &&
Array.isArray(queryValues) &&
queryValues[0]
) {
DSQL_USER.query(queryString, queryValues)
.then(runQuery)
.catch(queryError);
} else {
DSQL_USER.query(queryString)
.then(runQuery)
.catch(queryError);
}
////////////////////////////////////////
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-06 06:26:23 +00:00
////////////////////////////////////////
fs.appendFileSync(
"./.tmp/dbErrorLogs.txt",
error.message + "\n" + Date() + "\n\n\n",
"utf8"
);
resolve({
error: error.message,
});
}
});
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-06 06:26:23 +00:00
return {
success: false,
error: error.message,
};
}
}
module.exports = DSQL_USER_DB_HANDLER;