2023-08-12 13:36:18 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const encrypt = require("../../../functions/encrypt");
|
|
|
|
const dbHandler = require("../../engine/utils/dbHandler");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports: Handle imports
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2023-08-13 14:25:06 +00:00
|
|
|
* @param {*} params.data - Data to add
|
2023-08-12 13:36:18 +00:00
|
|
|
* @param {import("../../../types/database-schema.td").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
|
2023-08-12 16:35:59 +00:00
|
|
|
* @param {string} params.encryptionKey - Encryption key
|
|
|
|
* @param {string} params.encryptionSalt - Encryption salt
|
2023-08-12 13:36:18 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<object|null>}
|
|
|
|
*/
|
2023-08-12 16:35:59 +00:00
|
|
|
async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt }) {
|
2023-08-12 13:36:18 +00:00
|
|
|
/**
|
|
|
|
* Check if data is valid
|
|
|
|
*/
|
|
|
|
if (!data || !Object.keys(data).length) return null;
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
const dataKeys = Object.keys(data);
|
|
|
|
|
|
|
|
let updateKeyValueArray = [];
|
|
|
|
let updateValues = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
for (let i = 0; i < dataKeys.length; i++) {
|
|
|
|
try {
|
|
|
|
const dataKey = dataKeys[i];
|
|
|
|
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 == "undefined") continue;
|
|
|
|
if (typeof value !== "string" && typeof value !== "number" && !value) continue;
|
|
|
|
|
|
|
|
if (targetFieldSchema?.encrypted) {
|
|
|
|
value = encrypt({ data: value, encryptionKey, encryptionSalt });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "object") {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "string" && value.match(/^null$/i)) {
|
|
|
|
value = {
|
|
|
|
toSqlString: function () {
|
|
|
|
return "NULL";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-29 07:02:21 +00:00
|
|
|
if (targetFieldSchema?.pattern) {
|
|
|
|
const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || "");
|
2023-08-29 07:27:30 +00:00
|
|
|
if (!value?.toString()?.match(pattern)) {
|
2023-08-29 07:02:21 +00:00
|
|
|
console.log("DSQL: Pattern not matched =>", value);
|
|
|
|
value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 13:36:18 +00:00
|
|
|
if (typeof value === "string" && !value.match(/./i)) {
|
|
|
|
value = {
|
|
|
|
toSqlString: function () {
|
|
|
|
return "NULL";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value && typeof value == "number" && value != 0) continue;
|
|
|
|
|
|
|
|
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
|
|
|
updateValues.push(value);
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
2023-08-13 13:56:56 +00:00
|
|
|
} catch (/** @type {*} */ error) {
|
2023-08-12 13:36:18 +00:00
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
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 = await dbHandler({
|
|
|
|
database: dbFullName,
|
|
|
|
query: query,
|
|
|
|
values: updateValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return statement
|
|
|
|
*/
|
|
|
|
return updatedEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = updateDbEntry;
|