2023-09-21 14:00:04 +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
|
|
|
|
* @param {*} params.data - Data to add
|
2024-10-14 06:49:01 +00:00
|
|
|
* @param {import("@/package-shared/types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
2023-09-21 14:00:04 +00:00
|
|
|
* @param {string} params.identifierColumnName - Update row identifier column name
|
|
|
|
* @param {string | number} params.identifierValue - Update row identifier column value
|
|
|
|
* @param {string} params.encryptionKey - Encryption key
|
|
|
|
* @param {string} params.encryptionSalt - Encryption salt
|
|
|
|
*
|
|
|
|
* @returns {Promise<object|null>}
|
|
|
|
*/
|
2024-10-14 06:49:01 +00:00
|
|
|
async function updateDbEntry({
|
|
|
|
dbContext,
|
|
|
|
paradigm,
|
|
|
|
dbFullName,
|
|
|
|
tableName,
|
|
|
|
data,
|
|
|
|
tableSchema,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
}) {
|
2023-09-21 14:00:04 +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];
|
|
|
|
|
2024-10-14 06:49:01 +00:00
|
|
|
const targetFieldSchemaArray = tableSchema
|
|
|
|
? tableSchema?.fields?.filter(
|
|
|
|
(field) => field.fieldName === dataKey
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
const targetFieldSchema =
|
|
|
|
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
|
|
|
? targetFieldSchemaArray[0]
|
|
|
|
: null;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
if (typeof value == "undefined") continue;
|
2024-10-14 06:49:01 +00:00
|
|
|
if (
|
|
|
|
typeof value !== "string" &&
|
|
|
|
typeof value !== "number" &&
|
|
|
|
!value
|
|
|
|
)
|
|
|
|
continue;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetFieldSchema?.pattern) {
|
2024-10-14 06:49:01 +00:00
|
|
|
const pattern = new RegExp(
|
|
|
|
targetFieldSchema.pattern,
|
|
|
|
targetFieldSchema.patternFlags || ""
|
|
|
|
);
|
2023-09-21 14:00:04 +00:00
|
|
|
if (!value?.toString()?.match(pattern)) {
|
|
|
|
console.log("DSQL: Pattern not matched =>", value);
|
|
|
|
value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {*} */ error) {
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2024-10-14 06:49:01 +00:00
|
|
|
console.log(
|
|
|
|
"DSQL: Error in parsing data keys in update function =>",
|
|
|
|
error.message
|
|
|
|
);
|
2023-09-21 14:00:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
updateKeyValueArray.push(`date_updated='${Date()}'`);
|
|
|
|
updateKeyValueArray.push(`date_updated_code='${Date.now()}'`);
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2024-10-14 06:49:01 +00:00
|
|
|
const query = `UPDATE ${tableName} SET ${updateKeyValueArray.join(
|
|
|
|
","
|
|
|
|
)} WHERE \`${identifierColumnName}\`=?`;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
updateValues.push(identifierValue);
|
|
|
|
|
|
|
|
const updatedEntry = await dbHandler({
|
|
|
|
database: dbFullName,
|
|
|
|
query: query,
|
|
|
|
values: updateValues,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return statement
|
|
|
|
*/
|
|
|
|
return updatedEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = updateDbEntry;
|