Updates
This commit is contained in:
@@ -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;
|
||||
Executable → Regular
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Executable → Regular
@@ -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
@@ -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");
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user