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
*
+19 -3
View File
@@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ path: "./../.env" });
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Main DB Handler Function
* @async
@@ -22,16 +26,28 @@ require("dotenv").config({ path: "./../.env" });
* @returns {Promise<object|null>}
*/
(() => __awaiter(void 0, void 0, void 0, function* () {
const connection = global.DSQL_DB_CONN;
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",
},
});
try {
const result = yield connection.query("SELECT id,first_name,last_name FROM users LIMIT 3");
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
console.log("Connection Query Success =>", result);
}
catch (error) {
console.log("Connection query ERROR =>", error.message);
}
finally {
connection.end();
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
process.exit();
}
}))();
+20 -4
View File
@@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ path: "./../.env" });
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Main DB Handler Function
* @async
@@ -23,9 +27,21 @@ require("dotenv").config({ path: "./../.env" });
*/
(() => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const connection = global.DSQL_DB_CONN;
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",
},
});
try {
const result = yield connection.query("SELECT user,host,ssl_type FROM mysql.user");
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
const parsedResults = JSON.parse(JSON.stringify(result));
for (let i = 0; i < parsedResults.length; i++) {
const user = parsedResults[i];
@@ -38,7 +54,7 @@ require("dotenv").config({ path: "./../.env" });
if (ssl_type === "ANY") {
continue;
}
const addUserSSL = yield connection.query(`ALTER USER '${User}'@'${Host}'`);
const addUserSSL = yield CONNECTION.query(`ALTER USER '${User}'@'${Host}'`);
console.log(`addUserSSL => ${User}@${Host}`, addUserSSL);
}
}
@@ -46,7 +62,7 @@ require("dotenv").config({ path: "./../.env" });
console.log("Connection query ERROR =>", error.message);
}
finally {
connection.end();
CONNECTION.end();
process.exit();
}
}))();
+18 -5
View File
@@ -15,20 +15,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dbHandler;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Main DB Handler Function
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
function dbHandler(_a) {
return __awaiter(this, arguments, void 0, function* ({ query, values, }) {
let connection = global.DSQL_DB_CONN;
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;
try {
if (query && values) {
results = yield connection.query(query, values);
results = yield CONNECTION.query(query, values);
}
else {
results = yield connection.query(query);
results = yield CONNECTION.query(query);
}
}
catch ( /** @type {any} */error) {
@@ -37,12 +50,12 @@ function dbHandler(_a) {
}
console.log("ERROR in dbHandler =>", error.message);
console.log(error);
console.log(connection.config());
console.log(CONNECTION.config());
fs_1.default.appendFileSync(path_1.default.resolve(__dirname, "../.tmp/dbErrorLogs.txt"), JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n", "utf8");
results = null;
}
finally {
yield (connection === null || connection === void 0 ? void 0 : connection.end());
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
}
if (results) {
return JSON.parse(JSON.stringify(results));
+17 -1
View File
@@ -8,15 +8,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # DSQL user read-only DB handler
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
function DB_HANDLER(...args) {
return __awaiter(this, void 0, void 0, function* () {
const CONNECTION = global.DSQL_DB_CONN;
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",
},
});
try {
if (!CONNECTION)
throw new Error("No Connection provided to DB_HANDLER function!");
@@ -13,20 +13,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = DSQL_USER_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const conn_db_handler_1 = __importDefault(require("../../db/conn-db-handler"));
/**
* # DSQL user read-only DB handler
*/
function DSQL_USER_DB_HANDLER(_a) {
return __awaiter(this, arguments, void 0, function* ({ paradigm, queryString, queryValues, }) {
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",
},
});
try {
switch (paradigm) {
case "Read Only":
return yield (0, conn_db_handler_1.default)(global.DSQL_READ_ONLY_DB_CONN, queryString, queryValues);
return yield (0, conn_db_handler_1.default)(global.DSQL_READ_ONLY_DB_CONN || CONNECTION, queryString, queryValues);
case "Full Access":
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN, queryString, queryValues);
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION, queryString, queryValues);
case "FA":
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN, queryString, queryValues);
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION, queryString, queryValues);
default:
return null;
}
@@ -1,20 +1,36 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = NO_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # DSQL user read-only DB handler
*/
function NO_DB_HANDLER(...args) {
const NO_DB = global.DSQL_DB_CONN;
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",
},
});
try {
return new Promise((resolve, reject) => {
NO_DB.query(...args)
CONNECTION.query(...args)
.then((results) => {
NO_DB.end();
CONNECTION.end();
resolve(JSON.parse(JSON.stringify(results)));
})
.catch((err) => {
NO_DB.end();
CONNECTION.end();
resolve({
error: err.message,
sql: err.sql,
@@ -28,4 +44,7 @@ function NO_DB_HANDLER(...args) {
error: error.message,
};
}
finally {
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
}
}
@@ -1,20 +1,36 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ROOT_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Root DB handler
*/
function ROOT_DB_HANDLER(...args) {
const NO_DB = global.DSQL_DB_CONN;
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",
},
});
try {
return new Promise((resolve, reject) => {
NO_DB.query(...args)
CONNECTION.query(...args)
.then((results) => {
NO_DB.end();
CONNECTION.end();
resolve(JSON.parse(JSON.stringify(results)));
})
.catch((err) => {
NO_DB.end();
CONNECTION.end();
resolve({
error: err.message,
sql: err.sql,
@@ -28,4 +44,7 @@ function ROOT_DB_HANDLER(...args) {
error: error.message,
};
}
finally {
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
}
}