dsql-admin/dsql-app/package-shared/shell/utils/dbHandler.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
import fs from "fs";
import path from "path";
import mysql from "serverless-mysql";
import grabDbSSL from "../../utils/backend/grabDbSSL";
2025-01-14 15:27:08 +00:00
import grabDSQLConnection from "../../utils/grab-dsql-connection";
2025-01-13 08:00:21 +00:00
type Param = {
query: string;
values?: string[] | object;
};
/**
* # Main DB Handler Function
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
export default async function dbHandler({
query,
values,
}: Param): Promise<any[] | object | null> {
2025-01-14 15:27:08 +00:00
const CONNECTION = grabDSQLConnection();
2025-01-13 08:00:21 +00:00
let results;
try {
if (query && values) {
2025-01-14 15:27:08 +00:00
results = await CONNECTION.query(query, values);
2025-01-13 08:00:21 +00:00
} else {
2025-01-14 15:27:08 +00:00
results = await CONNECTION.query(query);
2025-01-13 08:00:21 +00:00
}
} catch (/** @type {any} */ error: any) {
if (process.env.FIRST_RUN) {
return null;
}
console.log("ERROR in dbHandler =>", error.message);
console.log(error);
2025-01-14 15:27:08 +00:00
console.log(CONNECTION.config());
2025-01-13 08:00:21 +00:00
fs.appendFileSync(
path.resolve(__dirname, "../.tmp/dbErrorLogs.txt"),
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
"utf8"
);
results = null;
} finally {
2025-01-14 15:27:08 +00:00
await CONNECTION?.end();
2025-01-13 08:00:21 +00:00
}
if (results) {
return JSON.parse(JSON.stringify(results));
} else {
return null;
}
}