This commit is contained in:
Benjamin Toby
2025-01-14 10:49:37 +01:00
parent 2118a55ab6
commit 0cd3eb418c
19 changed files with 307 additions and 65 deletions
+18 -3
View File
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dbHandler;
const fs_1 = __importDefault(require("fs"));
const serverError_1 = __importDefault(require("./serverError"));
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Main DB Handler Function
*/
@@ -23,6 +24,19 @@ function dbHandler(...args) {
var _a;
((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.match(/dev/)) &&
fs_1.default.appendFileSync("./.tmp/sqlQuery.sql", args[0] + "\n" + Date() + "\n\n\n", "utf8");
const CONNECTION = global.DSQL_DB_CONN ||
(0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_USERNAME,
password: process.env.DSQL_DB_PASSWORD,
database: process.env.DSQL_DB_NAME,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
});
let results;
/**
* Fetch from db
@@ -30,9 +44,8 @@ function dbHandler(...args) {
* @description Fetch data from db if no cache
*/
try {
const connection = global.DSQL_DB_CONN;
results = yield new Promise((resolve, reject) => {
connection.query(...args, (error, result, fields) => {
CONNECTION.query(...args, (error, result, fields) => {
if (error) {
resolve({ error: error.message });
}
@@ -41,7 +54,6 @@ function dbHandler(...args) {
}
});
});
yield connection.end();
}
catch (error) {
fs_1.default.appendFileSync("./.tmp/dbErrorLogs.txt", JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n", "utf8");
@@ -51,6 +63,9 @@ function dbHandler(...args) {
message: error.message,
});
}
finally {
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
}
/**
* Return results
*