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