This commit is contained in:
Benjamin Toby
2024-11-26 10:31:39 +01:00
parent 4b13c5264d
commit d13ba5057b
266 changed files with 702 additions and 426 deletions
View File
View File
View File
View File
View File
@@ -11,6 +11,7 @@
const fs = require("fs");
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
const fullAccessDbHandler = require("../fullAccessDbHandler");
const varReadOnlyDatabaseDbHandler = require("../varReadOnlyDatabaseDbHandler");
const serverError = require("../serverError");
@@ -18,8 +19,8 @@ const serverError = require("../serverError");
const addDbEntry = require("./addDbEntry");
const updateDbEntry = require("./updateDbEntry");
const deleteDbEntry = require("./deleteDbEntry");
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
const parseDbResults = require("../parseDbResults");
const trimSql = require("../../../utils/trim-sql");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -89,8 +90,27 @@ async function runQuery({
*/
try {
if (typeof query === "string") {
const formattedQuery = trimSql(query);
/**
* Input Validation
*
* @description Input Validation
*/
if (
readOnly &&
formattedQuery.match(
/^alter|^delete|information_schema|databases|^create/i
)
) {
throw new Error("Wrong Input!");
}
if (local) {
const rawResults = await DB_HANDLER(query, queryValuesArray);
const rawResults = await LOCAL_DB_HANDLER(
formattedQuery,
queryValuesArray
);
result = tableSchema
? parseDbResults({
unparsedResults: rawResults,
@@ -99,14 +119,14 @@ async function runQuery({
: rawResults;
} else if (readOnly) {
result = await varReadOnlyDatabaseDbHandler({
queryString: query,
queryString: formattedQuery,
queryValuesArray,
database: dbFullName,
tableSchema,
});
} else {
result = await fullAccessDbHandler({
queryString: query,
queryString: formattedQuery,
queryValuesArray,
database: dbFullName,
tableSchema,
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
+4 -2
View File
@@ -1172,7 +1172,7 @@ export type FetchDataParams = {
};
export type AuthFetchQuery = ServerQueryParam & {
[key: string]: string | number | { [key: string]: any };
[key: string]: any;
};
export type ServerQueryParamsJoin<
@@ -1199,7 +1199,9 @@ export type ServerQueryParamsJoinMatchObject<
/** Field name from the **Root Table** */
source: string | ServerQueryParamsJoinMatchSourceTargetObject;
/** Field name from the **Join Table** */
target: keyof Field | ServerQueryParamsJoinMatchSourceTargetObject;
target?: keyof Field | ServerQueryParamsJoinMatchSourceTargetObject;
/** A literal value: No source and target Needed! */
targetLiteral?: string;
};
export type ServerQueryParamsJoinMatchSourceTargetObject = {
@@ -0,0 +1,54 @@
// @ts-check
const mysql = require("serverless-mysql");
const grabDbSSL = require("../grabDbSSL");
/**
* DSQL user read-only DB handler
* @param {object} params
* @param {string} params.paradigm
* @param {string} params.database
* @param {string} params.queryString
* @param {string[]} [params.queryValues]
*/
async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
const MASTER = mysql({
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: grabDbSSL(),
},
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);
},
});
console.log("Querying ...");
try {
const results = await MASTER.query(...args);
await MASTER.end();
return JSON.parse(JSON.stringify(results));
} catch (/** @type {any} */ error) {
console.log("DB Error =>", error.message);
return {
success: false,
error: error.message,
};
}
}
module.exports = LOCAL_DB_HANDLER;
-3
View File
@@ -8,9 +8,6 @@ const fs = require("fs");
module.exports = function grabDbSSL() {
const SSL_DIR = process.env.DSQL_SSL_DIR;
if (!SSL_DIR?.match(/./)) {
// console.log(
// "No SSL certificate provided. Query will run in normal mode. To add SSL add an env path dir `DSQL_SSL_DIR` with a file named `ca-cert.pem`"
// );
return undefined;
}
View File
@@ -0,0 +1,35 @@
// @ts-check
const https = require("https");
const http = require("http");
/**
* @typedef {object} GrabHostNamesReturn
* @property {string} host
* @property {number | string} port
* @property {typeof http | typeof https} scheme
*/
/**
* # Grab Names For Query
* @returns {GrabHostNamesReturn}
*/
function grabHostNames() {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
return {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
};
}
module.exports = grabHostNames;
+24
View File
@@ -0,0 +1,24 @@
export = trimSql;
/**
* @typedef {object} GrabHostNamesReturn
* @property {string} host
* @property {number | string} port
* @property {typeof http | typeof https} scheme
*/
/**
* # Trim SQL
* @description Remove Returns and miltiple spaces from SQL Query
* @param {string} sql
* @returns {string}
*/
declare function trimSql(sql: string): string;
declare namespace trimSql {
export { GrabHostNamesReturn };
}
type GrabHostNamesReturn = {
host: string;
port: number | string;
scheme: typeof http | typeof https;
};
import http = require("http");
import https = require("https");
+26
View File
@@ -0,0 +1,26 @@
// @ts-check
const https = require("https");
const http = require("http");
/**
* @typedef {object} GrabHostNamesReturn
* @property {string} host
* @property {number | string} port
* @property {typeof http | typeof https} scheme
*/
/**
* # Trim SQL
* @description Remove Returns and miltiple spaces from SQL Query
* @param {string} sql
* @returns {string}
*/
function trimSql(sql) {
return sql
.replace(/\n|\r|\n\r|\r\n/gm, " ")
.replace(/ {2,}/g, " ")
.trim();
}
module.exports = trimSql;