From 7ffa5c0da57790197deb6d451c8a2c6bd308290a Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sun, 17 Nov 2024 05:36:56 +0100 Subject: [PATCH] Updates --- package-lock.json | 62 ++++++++++++++-- .../functions/backend/db/runQuery.js | 7 +- .../backend/global-db/LOCAL_DB_HANDLER.js | 72 +++++++++++++++++++ package.json | 3 +- 4 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 package-shared/utils/backend/global-db/LOCAL_DB_HANDLER.js diff --git a/package-lock.json b/package-lock.json index 3bf314b..3584eb3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@moduletrace/datasquirel", - "version": "2.6.4", + "version": "2.6.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@moduletrace/datasquirel", - "version": "2.6.4", + "version": "2.6.5", "license": "ISC", "dependencies": { "@types/ace": "^0.0.52", @@ -15,6 +15,7 @@ "dotenv": "^16.3.1", "generate-password": "^1.7.1", "lodash": "^4.17.21", + "mariadb": "^3.4.0", "mysql": "^2.18.1", "nodemailer": "^6.9.14", "sanitize-html": "^2.13.1", @@ -36,6 +37,12 @@ "integrity": "sha512-YPF9S7fzpuyrxru+sG/rrTpZkC6gpHBPF14W3x70kqVOD+ks6jkYLapk4yceh36xej7K4HYxcyz9ZDQ2lTvwgQ==", "license": "MIT" }, + "node_modules/@types/geojson": { + "version": "7946.0.14", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.14.tgz", + "integrity": "sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==", + "license": "MIT" + }, "node_modules/@types/jquery": { "version": "3.5.32", "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.32.tgz", @@ -65,7 +72,6 @@ "version": "22.7.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz", "integrity": "sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==", - "devOptional": true, "license": "MIT", "dependencies": { "undici-types": "~6.19.2" @@ -130,6 +136,15 @@ "node": ">=0.10.0" } }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10" + } + }, "node_modules/dom-serializer": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz", @@ -245,6 +260,18 @@ "entities": "^4.4.0" } }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", @@ -270,6 +297,28 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "license": "MIT" }, + "node_modules/lru-cache": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz", + "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", + "license": "ISC" + }, + "node_modules/mariadb": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/mariadb/-/mariadb-3.4.0.tgz", + "integrity": "sha512-hdRPcAzs+MTxK5VG1thBW18gGTlw6yWBe9YnLB65GLo7q0fO5DWsgomIevV/pXSaWRmD3qi6ka4oSFRTExRiEQ==", + "license": "LGPL-2.1-or-later", + "dependencies": { + "@types/geojson": "^7946.0.14", + "@types/node": "^22.5.4", + "denque": "^2.1.0", + "iconv-lite": "^0.6.3", + "lru-cache": "^10.3.0" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/mysql": { "version": "2.18.1", "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", @@ -375,6 +424,12 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, "node_modules/sanitize-html": { "version": "2.13.1", "resolved": "https://registry.npmjs.org/sanitize-html/-/sanitize-html-2.13.1.tgz", @@ -430,7 +485,6 @@ "version": "6.19.8", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", - "devOptional": true, "license": "MIT" }, "node_modules/util-deprecate": { diff --git a/package-shared/functions/backend/db/runQuery.js b/package-shared/functions/backend/db/runQuery.js index 007ff00..b90d2dc 100644 --- a/package-shared/functions/backend/db/runQuery.js +++ b/package-shared/functions/backend/db/runQuery.js @@ -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,7 +19,6 @@ 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"); /** ****************************************************************************** */ @@ -90,7 +90,10 @@ async function runQuery({ try { if (typeof query === "string") { if (local) { - const rawResults = await DB_HANDLER(query, queryValuesArray); + const rawResults = await LOCAL_DB_HANDLER( + query, + queryValuesArray + ); result = tableSchema ? parseDbResults({ unparsedResults: rawResults, diff --git a/package-shared/utils/backend/global-db/LOCAL_DB_HANDLER.js b/package-shared/utils/backend/global-db/LOCAL_DB_HANDLER.js new file mode 100644 index 0000000..cb2b596 --- /dev/null +++ b/package-shared/utils/backend/global-db/LOCAL_DB_HANDLER.js @@ -0,0 +1,72 @@ +// @ts-check + +const mysql = require("serverless-mysql"); +const grabDbSSL = require("../grabDbSSL"); + +// const mariadb = require("mariadb"); +// const pool = mariadb.createPool({ +// 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: true, +// }); + +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(), + timeout: 1000, + connectTimeout: 1000, + }, +}); + +/** + * 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) { + MASTER.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(), + timeout: 1000, + connectTimeout: 1000, + }); + + 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); + return { + success: false, + error: error.message, + }; + } +} + +module.exports = LOCAL_DB_HANDLER; diff --git a/package.json b/package.json index dc6b700..b22a6fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "2.6.5", + "version": "2.6.6", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { @@ -36,6 +36,7 @@ "dotenv": "^16.3.1", "generate-password": "^1.7.1", "lodash": "^4.17.21", + "mariadb": "^3.4.0", "mysql": "^2.18.1", "nodemailer": "^6.9.14", "sanitize-html": "^2.13.1",