This commit is contained in:
Benjamin Toby
2025-01-14 11:14:50 +01:00
parent edd555c088
commit d55fe64d47
30 changed files with 225 additions and 481 deletions
+6
View File
@@ -1,3 +1,9 @@
import { ServerlessMysql } from "serverless-mysql";
declare global {
var DSQL_DB_CONN: ServerlessMysql | undefined;
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql | undefined;
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql | undefined;
}
/**
* Imports
*/
+2 -1
View File
@@ -45,12 +45,14 @@ function apiLoginUser(_a) {
password: password,
})
: null;
console.log("Finding DSQL User ...");
let foundUser = yield (0, varDatabaseDbHandler_1.default)({
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
queryValuesArray: [email, username],
database: dbFullName,
useLocal,
});
console.log("foundUser", foundUser);
if ((!foundUser || !foundUser[0]) && !social)
return {
success: false,
@@ -69,7 +71,6 @@ function apiLoginUser(_a) {
email_login &&
email_login_code &&
email_login_field) {
/** @type {string} */
const tempCode = foundUser[0][email_login_field];
if (!tempCode)
throw new Error("No code Found!");
+2 -13
View File
@@ -15,7 +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"));
const grab_dsql_connection_1 = __importDefault(require("../../utils/grab-dsql-connection"));
/**
* # Main DB Handler Function
*/
@@ -24,18 +24,7 @@ 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 = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
let results;
/**
* Fetch from db
+2 -13
View File
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ path: "./../.env" });
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-connection"));
/**
* # Main DB Handler Function
* @async
@@ -26,18 +26,7 @@ const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
* @returns {Promise<object|null>}
*/
(() => __awaiter(void 0, void 0, void 0, function* () {
const CONNECTION = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
try {
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
console.log("Connection Query Success =>", result);
+4 -14
View File
@@ -13,18 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ path: "./.env" });
const grabDbSSL_1 = __importDefault(require("../utils/backend/grabDbSSL"));
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const connection = (0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
// database: process.env.DSQL_DB_NAME,
charset: "utf8mb4",
ssl: (0, grabDbSSL_1.default)(),
},
});
const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-connection"));
/**
* # Main DB Handler Function
* @async
@@ -37,13 +26,14 @@ const connection = (0, serverless_mysql_1.default)({
* @returns {Promise<object|null>}
*/
(() => __awaiter(void 0, void 0, void 0, function* () {
const CONNECTION = (0, grab_dsql_connection_1.default)({ noDb: true });
/**
* Switch Database
*
* @description If a database is provided, switch to it
*/
try {
const result = yield connection.query("SHOW DATABASES");
const result = yield CONNECTION.query("SHOW DATABASES");
const parsedResults = JSON.parse(JSON.stringify(result));
console.log("parsedResults =>", parsedResults);
}
@@ -51,7 +41,7 @@ const connection = (0, serverless_mysql_1.default)({
console.log("Connection query ERROR =>", error.message);
}
finally {
connection.end();
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
process.exit();
}
}))();
+2 -13
View File
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config({ path: "./../.env" });
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-connection"));
/**
* # Main DB Handler Function
* @async
@@ -27,18 +27,7 @@ const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
*/
(() => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const CONNECTION = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
try {
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
const parsedResults = JSON.parse(JSON.stringify(result));
+2 -13
View File
@@ -15,25 +15,14 @@ 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"));
const grab_dsql_connection_1 = __importDefault(require("../../utils/grab-dsql-connection"));
/**
* # 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, }) {
const CONNECTION = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
let results;
try {
if (query && values) {
+2 -13
View File
@@ -13,25 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
/**
* # 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 = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
try {
if (!CONNECTION)
throw new Error("No Connection provided to DB_HANDLER function!");
@@ -13,36 +13,16 @@ 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"));
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
/**
* # DSQL user read-only DB handler
*/
function DSQL_USER_DB_HANDLER(_a) {
return __awaiter(this, arguments, void 0, function* ({ paradigm, queryString, queryValues, }) {
const CONNECTION = paradigm == "Read Only"
? (0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
})
: (0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
});
? (0, grab_dsql_connection_1.default)({ ro: true })
: (0, grab_dsql_connection_1.default)({ fa: true });
try {
return yield (0, conn_db_handler_1.default)(CONNECTION, queryString, queryValues);
}
@@ -53,85 +33,5 @@ function DSQL_USER_DB_HANDLER(_a) {
finally {
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
}
// try {
// return new Promise((resolve, reject) => {
// const fullAccess = paradigm?.match(/full.access|^fa$/i)
// ? true
// : false;
// try {
// if (fullAccess) {
// DSQL_USER = mysql({
// config: {
// host: process.env.DSQL_DB_HOST,
// user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
// password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
// database: database,
// ssl: grabDbSSL(),
// },
// });
// } else {
// DSQL_USER = mysql({
// config: {
// host: process.env.DSQL_DB_HOST,
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
// database: database,
// ssl: grabDbSSL(),
// },
// });
// }
// /**
// * ### Run query Function
// * @param {any} results
// */
// function runQuery(results: any) {
// DSQL_USER.end();
// resolve(JSON.parse(JSON.stringify(results)));
// }
// /**
// * ### Query Error
// * @param {any} err
// */
// function queryError(err: any) {
// DSQL_USER.end();
// resolve({
// error: err.message,
// queryStringGenerated: queryString,
// queryValuesGenerated: queryValues,
// sql: err.sql,
// });
// }
// if (
// queryValues &&
// Array.isArray(queryValues) &&
// queryValues[0]
// ) {
// DSQL_USER.query(queryString, queryValues)
// .then(runQuery)
// .catch(queryError);
// } else {
// DSQL_USER.query(queryString)
// .then(runQuery)
// .catch(queryError);
// }
// ////////////////////////////////////////
// } catch (/** @type {any} */ error: any) {
// ////////////////////////////////////////
// fs.appendFileSync(
// "./.tmp/dbErrorLogs.txt",
// error.message + "\n" + Date() + "\n\n\n",
// "utf8"
// );
// resolve({
// error: error.message,
// });
// }
// });
// } catch (/** @type {any} */ error: any) {
// return {
// success: false,
// error: error.message,
// };
// }
});
}
@@ -13,47 +13,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = LOCAL_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grabDbSSL_1 = __importDefault(require("../grabDbSSL"));
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
/**
* # DSQL user read-only DB handler
*/
function LOCAL_DB_HANDLER(...args) {
return __awaiter(this, void 0, void 0, function* () {
const MASTER = (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",
ssl: (0, grabDbSSL_1.default)(),
},
onConnect: () => {
console.log("Connection Successful!");
},
onConnectError: (/** @type {any} */ err) => {
console.log("Connection Error", err.message);
},
onError: (/** @type {any} */ err) => {
console.log("Client Error", err.message);
},
});
const MASTER = (0, grab_dsql_connection_1.default)();
console.log("Querying ...");
try {
const results = yield MASTER.query(...args);
yield MASTER.end();
return JSON.parse(JSON.stringify(results));
}
catch ( /** @type {any} */error) {
catch (error) {
console.log("DB Error =>", error.message);
return {
success: false,
error: error.message,
};
}
finally {
yield (MASTER === null || MASTER === void 0 ? void 0 : MASTER.end());
}
});
}
@@ -4,23 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = NO_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
/**
* # DSQL user read-only DB handler
*/
function NO_DB_HANDLER(...args) {
const CONNECTION = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
try {
return new Promise((resolve, reject) => {
CONNECTION.query(...args)
@@ -4,23 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ROOT_DB_HANDLER;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
/**
* # Root DB handler
*/
function ROOT_DB_HANDLER(...args) {
const CONNECTION = (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",
},
});
const CONNECTION = (0, grab_dsql_connection_1.default)();
try {
return new Promise((resolve, reject) => {
CONNECTION.query(...args)
+20
View File
@@ -0,0 +1,20 @@
import { ServerlessMysql } from "serverless-mysql";
type Param = {
/**
* Read Only?
*/
ro?: boolean;
/**
* Full Access?
*/
fa?: boolean;
/**
* Full Access?
*/
noDb?: boolean;
};
/**
* # Grab General CONNECTION for DSQL
*/
export default function grabDSQLConnection(param?: Param): ServerlessMysql;
export {};
+53
View File
@@ -0,0 +1,53 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDSQLConnection;
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
/**
* # Grab General CONNECTION for DSQL
*/
function grabDSQLConnection(param) {
if (param === null || param === void 0 ? void 0 : param.ro) {
return (DSQL_READ_ONLY_DB_CONN ||
(0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
}));
}
if (param === null || param === void 0 ? void 0 : param.fa) {
return (global.DSQL_FULL_ACCESS_DB_CONN ||
(0, serverless_mysql_1.default)({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
}));
}
return (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: (param === null || param === void 0 ? void 0 : param.noDb) ? undefined : process.env.DSQL_DB_NAME,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
},
}));
}
-1
View File
@@ -82,7 +82,6 @@ function loginUser(_a) {
(DSQL_DB_PASSWORD === null || DSQL_DB_PASSWORD === void 0 ? void 0 : DSQL_DB_PASSWORD.match(/./)) &&
(DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) &&
useLocal) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
try {
const localDbSchemaPath = path_1.default.resolve(process.cwd(), "dsql.schema.json");