datasquirel/dist/package-shared/functions/backend/dbHandler.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

48 lines
1.5 KiB
JavaScript

import fs from "fs";
import path from "path";
import grabDSQLConnection from "../../utils/grab-dsql-connection";
/**
* # Main DB Handler Function
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
export default async function dbHandler({ query, values, noErrorLogs, }) {
var _a;
const CONNECTION = grabDSQLConnection();
let results;
try {
if (query && values) {
results = await CONNECTION.query(query, values);
}
else {
results = await CONNECTION.query(query);
}
}
catch (error) {
if (!noErrorLogs) {
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `DB Handler Error...`, error);
}
if (process.env.FIRST_RUN) {
return null;
}
if (!noErrorLogs) {
console.log("ERROR in dbHandler =>", error.message);
console.log(error);
console.log(CONNECTION.config());
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
if (!fs.existsSync(tmpFolder))
fs.mkdirSync(tmpFolder, { recursive: true });
fs.appendFileSync(path.resolve(tmpFolder, "./dbErrorLogs.txt"), JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n", "utf8");
}
results = null;
}
finally {
await (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
}
if (results) {
return JSON.parse(JSON.stringify(results));
}
else {
return null;
}
}