diff --git a/package-shared/functions/backend/db/addDbEntry.js b/package-shared/functions/backend/db/addDbEntry.js index 8beac5d..1c57dee 100644 --- a/package-shared/functions/backend/db/addDbEntry.js +++ b/package-shared/functions/backend/db/addDbEntry.js @@ -138,6 +138,13 @@ async function addDbEntry({ if (value == null || value == undefined) continue; + if ( + targetFieldSchema?.dataType?.match(/int$/i) && + typeof value == "string" && + !value?.match(/./) + ) + continue; + if (targetFieldSchema?.encrypted) { value = encrypt(value, encryptionKey, encryptionSalt); console.log("DSQL: Encrypted value =>", value); diff --git a/package-shared/functions/backend/dbHandler.js b/package-shared/functions/backend/dbHandler.js index 2d7543f..1bf0f83 100644 --- a/package-shared/functions/backend/dbHandler.js +++ b/package-shared/functions/backend/dbHandler.js @@ -4,10 +4,7 @@ const fs = require("fs"); const serverError = require("./serverError"); const mysql = require("serverless-mysql"); -const path = require("path"); - -const SSL_DIR = - process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../ssl"); +const grabDbSSL = require("../../utils/backend/grabDbSSL"); const connection = mysql({ config: { @@ -16,9 +13,7 @@ const connection = mysql({ password: process.env.DSQL_DB_PASSWORD, database: process.env.DSQL_DB_NAME, charset: "utf8mb4", - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); diff --git a/package-shared/utils/backend/global-db/DB_HANDLER.js b/package-shared/utils/backend/global-db/DB_HANDLER.js index 28d5cb3..bef595a 100644 --- a/package-shared/utils/backend/global-db/DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/DB_HANDLER.js @@ -1,11 +1,7 @@ // @ts-check -const fs = require("fs"); -const path = require("path"); - const mysql = require("serverless-mysql"); -const SSL_DIR = - process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); +const grabDbSSL = require("../grabDbSSL"); const MASTER = mysql({ config: { @@ -17,9 +13,7 @@ const MASTER = mysql({ ? Number(process.env.DSQL_DB_PORT) : undefined, charset: "utf8mb4", - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); diff --git a/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js b/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js index 9a222a3..ead7f56 100644 --- a/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js @@ -4,9 +4,7 @@ const fs = require("fs"); const path = require("path"); const mysql = require("serverless-mysql"); - -const SSL_DIR = - process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); +const grabDbSSL = require("../grabDbSSL"); let DSQL_USER = mysql({ config: { @@ -14,9 +12,7 @@ let DSQL_USER = mysql({ user: process.env.DSQL_DB_READ_ONLY_USERNAME, password: process.env.DSQL_DB_READ_ONLY_PASSWORD, charset: "utf8mb4", - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); @@ -48,9 +44,7 @@ function DSQL_USER_DB_HANDLER({ user: process.env.DSQL_DB_FULL_ACCESS_USERNAME, password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD, database: database, - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); } else { @@ -60,9 +54,7 @@ function DSQL_USER_DB_HANDLER({ user: process.env.DSQL_DB_READ_ONLY_USERNAME, password: process.env.DSQL_DB_READ_ONLY_PASSWORD, database: database, - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); } diff --git a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js index 5fc8812..80bf796 100644 --- a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js @@ -1,12 +1,7 @@ // @ts-check -const fs = require("fs"); -const path = require("path"); - const mysql = require("serverless-mysql"); - -const SSL_DIR = - process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); +const grabDbSSL = require("../grabDbSSL"); let NO_DB = mysql({ config: { @@ -14,9 +9,7 @@ let NO_DB = mysql({ user: process.env.DSQL_DB_USERNAME, password: process.env.DSQL_DB_PASSWORD, charset: "utf8mb4", - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); diff --git a/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js b/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js index c5383ad..3733cef 100644 --- a/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js @@ -1,12 +1,7 @@ // @ts-check -const fs = require("fs"); -const path = require("path"); - const mysql = require("serverless-mysql"); - -const SSL_DIR = - process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); +const grabDbSSL = require("../grabDbSSL"); let NO_DB = mysql({ config: { @@ -14,9 +9,7 @@ let NO_DB = mysql({ user: process.env.DSQL_DB_USERNAME, password: process.env.DSQL_DB_PASSWORD, charset: "utf8mb4", - ssl: { - ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), - }, + ssl: grabDbSSL(), }, }); diff --git a/package-shared/utils/backend/global-db/index.js b/package-shared/utils/backend/global-db/index.js deleted file mode 100644 index ae9b9af..0000000 --- a/package-shared/utils/backend/global-db/index.js +++ /dev/null @@ -1,49 +0,0 @@ -// @ts-check - -const fs = require("fs"); - -const DSQL_USER_DB_HANDLER = require("./DSQL_USER_DB_HANDLER"); - -//////////////////////////////////////// -//////////////////////////////////////// -//////////////////////////////////////// - -process.addListener("exit", async (code) => { - console.log("PROCESS EXITING ..."); -}); - -//////////////////////////////////////// -//////////////////////////////////////// -//////////////////////////////////////// - -/** - * Global function - * ================================================ - * @description this sets all require global variables. This only runs once. - */ -module.exports = function globalFunction() { - /** - * Main Db Handler - */ - - //////////////////////////////////////// - //////////////////////////////////////// - //////////////////////////////////////// - /** - * Main Db Handler - */ - - //////////////////////////////////////// - //////////////////////////////////////// - //////////////////////////////////////// - - /** - * 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] - */ - DSQL_USER_DB_HANDLER; -}; diff --git a/package-shared/utils/backend/grabDbSSL.js b/package-shared/utils/backend/grabDbSSL.js new file mode 100644 index 0000000..9768bd4 --- /dev/null +++ b/package-shared/utils/backend/grabDbSSL.js @@ -0,0 +1,22 @@ +// @ts-check + +const fs = require("fs"); + +/** + * @returns {string | (import("tls").SecureContextOptions & { rejectUnauthorized?: boolean | undefined;}) | undefined} + */ +module.exports = function grabDbSSL() { + const SSL_DIR = process.env.DSQL_SSL_DIR; + if (!SSL_DIR?.match(/./)) return undefined; + + const caFilePath = `${SSL_DIR}/ca-cert.pem`; + if (!fs.existsSync(caFilePath)) { + console.log(`${caFilePath} does not exist`); + + return undefined; + } + + return { + ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), + }; +}; diff --git a/package.json b/package.json index 6d7eb6c..3c94408 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.4.1", + "version": "2.4.2", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {