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; } }