diff --git a/engine/db/addDb.js b/engine/db/addDb.js index fe7435d..0b235bd 100644 --- a/engine/db/addDb.js +++ b/engine/db/addDb.js @@ -146,8 +146,6 @@ async function addDb({ dbFullName, tableName, data, tableSchema, duplicateColumn tableSchema, }); - console.log(newInsert); - //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// diff --git a/engine/db/updateDb.js b/engine/db/updateDb.js index 5eae741..7aa204e 100644 --- a/engine/db/updateDb.js +++ b/engine/db/updateDb.js @@ -1,6 +1,9 @@ /** * Imports: Handle imports */ +const encrypt = require("../../functions/encrypt"); +const sanitizeHtml = require("sanitize-html"); +const sanitizeHtmlOptions = require("../utils/sanitizeHtmlOptions"); const dsqlDbHandler = require("../utils/dsqlDbHandler"); /** @@ -46,24 +49,52 @@ async function updateDb({ dbFullName, tableName, data, tableSchema, identifierCo let updateValues = []; for (let i = 0; i < dataKeys.length; i++) { - const dataKey = dataKeys[i]; + try { + const dataKey = dataKeys[i]; + let value = data[dataKey]; - let value = data[dataKey]; + const targetFieldSchemaArray = tableSchema ? tableSchema?.fields?.filter((field) => field.fieldName === dataKey) : null; + const targetFieldSchema = targetFieldSchemaArray && targetFieldSchemaArray[0] ? targetFieldSchemaArray[0] : null; - if (typeof value === "string" && value.match(/^null$/i)) value = ""; + if (!value) continue; - if (!value && value != 0) continue; + if (targetFieldSchema?.encrypted) { + value = encrypt({ data: value, encryptionKey, encryptionSalt }); + } - updateKeyValueArray.push(`\`${dataKey}\`=?`); - updateValues.push(value); + if (targetFieldSchema?.richText) { + value = sanitizeHtml(value, sanitizeHtmlOptions); + } + + if (typeof value === "string" && value.match(/^null$/i)) value = ""; + if (typeof value === "object") { + value = JSON.stringify(value); + } + + if (!value && value != 0) continue; + + updateKeyValueArray.push(`\`${dataKey}\`=?`); + updateValues.push(value); + + //////////////////////////////////////// + //////////////////////////////////////// + } catch (error) { + //////////////////////////////////////// + //////////////////////////////////////// + + console.log("DSQL: Error in parsing data keys in update function =>", error.message); + continue; + } } - /** ********************************************** */ + //////////////////////////////////////// + //////////////////////////////////////// updateKeyValueArray.push(`date_updated='${Date()}'`); updateKeyValueArray.push(`date_updated_code='${Date.now()}'`); - /** ********************************************** */ + //////////////////////////////////////// + //////////////////////////////////////// const query = `UPDATE ${tableName} SET ${updateKeyValueArray.join(",")} WHERE \`${identifierColumnName}\`=?`; diff --git a/package.json b/package.json index 9be1385..03b470f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.1.78", + "version": "1.1.79", "description": "Cloud-based SQL data management tool", "main": "index.js", "scripts": {