2024-11-05 11:12:42 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports: Handle imports
|
|
|
|
*/
|
|
|
|
const sanitizeHtml = require("sanitize-html");
|
|
|
|
const sanitizeHtmlOptions = require("../html/sanitizeHtmlOptions");
|
|
|
|
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
|
|
|
const DSQL_USER_DB_HANDLER = require("../../../utils/backend/global-db/DSQL_USER_DB_HANDLER");
|
2024-12-06 13:24:26 +00:00
|
|
|
const encrypt = require("../../dsql/encrypt");
|
|
|
|
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update DB Function
|
|
|
|
* ==============================================================================
|
|
|
|
* @description Description
|
|
|
|
* @async
|
|
|
|
*
|
|
|
|
* @param {object} params - An object containing the function parameters.
|
|
|
|
* @param {("Master" | "Dsql User")} [params.dbContext] - What is the database context? "Master"
|
|
|
|
* or "Dsql User". Defaults to "Master"
|
|
|
|
* @param {("Read Only" | "Full Access")} [params.paradigm] - What is the paradigm for "Dsql User"?
|
|
|
|
* "Read only" or "Full Access"? Defaults to "Read Only"
|
|
|
|
* @param {string} [params.dbFullName] - Database full name
|
|
|
|
* @param {string} params.tableName - Table name
|
|
|
|
* @param {string} [params.encryptionKey]
|
|
|
|
* @param {string} [params.encryptionSalt]
|
|
|
|
* @param {any} params.data - Data to add
|
|
|
|
* @param {import("../../../types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
|
|
|
* @param {string} params.identifierColumnName - Update row identifier column name
|
|
|
|
* @param {string | number} params.identifierValue - Update row identifier column value
|
2024-12-06 13:24:26 +00:00
|
|
|
* @param {boolean} [params.useLocal]
|
2024-11-05 11:12:42 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<object|null>}
|
|
|
|
*/
|
|
|
|
async function updateDbEntry({
|
|
|
|
dbContext,
|
|
|
|
paradigm,
|
|
|
|
dbFullName,
|
|
|
|
tableName,
|
|
|
|
data,
|
|
|
|
tableSchema,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
2024-12-06 13:24:26 +00:00
|
|
|
useLocal,
|
2024-11-05 11:12:42 +00:00
|
|
|
}) {
|
|
|
|
/**
|
|
|
|
* Check if data is valid
|
|
|
|
*/
|
|
|
|
if (!data || !Object.keys(data).length) return null;
|
|
|
|
|
2024-12-09 12:27:08 +00:00
|
|
|
const isMaster = useLocal
|
|
|
|
? true
|
|
|
|
: dbContext?.match(/dsql.user/i)
|
2024-11-05 11:12:42 +00:00
|
|
|
? false
|
|
|
|
: dbFullName && !dbFullName.match(/^datasquirel$/)
|
|
|
|
? false
|
|
|
|
: true;
|
|
|
|
|
|
|
|
/** @type {(a1:any, a2?:any)=> any } */
|
2024-12-06 13:24:26 +00:00
|
|
|
const dbHandler = useLocal
|
|
|
|
? LOCAL_DB_HANDLER
|
|
|
|
: isMaster
|
|
|
|
? DB_HANDLER
|
|
|
|
: DSQL_USER_DB_HANDLER;
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
const dataKeys = Object.keys(data);
|
|
|
|
|
|
|
|
let updateKeyValueArray = [];
|
|
|
|
let updateValues = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < dataKeys.length; i++) {
|
|
|
|
try {
|
|
|
|
const dataKey = dataKeys[i];
|
|
|
|
// @ts-ignore
|
|
|
|
let value = data[dataKey];
|
|
|
|
|
|
|
|
const targetFieldSchemaArray = tableSchema
|
|
|
|
? tableSchema?.fields?.filter(
|
|
|
|
(field) => field.fieldName === dataKey
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
const targetFieldSchema =
|
|
|
|
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
|
|
|
? targetFieldSchemaArray[0]
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (value == null || value == undefined) continue;
|
|
|
|
|
2024-12-15 11:27:16 +00:00
|
|
|
const htmlRegex = /<[^>]+>/g;
|
|
|
|
|
|
|
|
if (targetFieldSchema?.richText || String(value).match(htmlRegex)) {
|
2024-11-05 11:12:42 +00:00
|
|
|
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetFieldSchema?.encrypted) {
|
2024-12-06 13:24:26 +00:00
|
|
|
value = encrypt({
|
|
|
|
data: value,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
});
|
2024-11-05 11:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "object") {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetFieldSchema?.pattern) {
|
|
|
|
const pattern = new RegExp(
|
|
|
|
targetFieldSchema.pattern,
|
|
|
|
targetFieldSchema.patternFlags || ""
|
|
|
|
);
|
|
|
|
if (!pattern.test(value)) {
|
|
|
|
console.log("DSQL: Pattern not matched =>", value);
|
|
|
|
value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "string" && value.match(/^null$/i)) {
|
|
|
|
value = {
|
|
|
|
toSqlString: function () {
|
|
|
|
return "NULL";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "string" && !value.match(/./i)) {
|
|
|
|
value = {
|
|
|
|
toSqlString: function () {
|
|
|
|
return "NULL";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
|
|
|
|
|
|
|
if (typeof value == "number") {
|
|
|
|
updateValues.push(String(value));
|
|
|
|
} else {
|
|
|
|
updateValues.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {any} */ 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}\`=?`;
|
|
|
|
|
|
|
|
updateValues.push(identifierValue);
|
|
|
|
|
|
|
|
const updatedEntry = isMaster
|
|
|
|
? await dbHandler(query, updateValues)
|
|
|
|
: await dbHandler({
|
|
|
|
paradigm,
|
|
|
|
database: dbFullName,
|
|
|
|
queryString: query,
|
|
|
|
queryValues: updateValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return statement
|
|
|
|
*/
|
|
|
|
return updatedEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = updateDbEntry;
|