dsql-admin/dsql-app/package-shared/functions/backend/db/updateDbEntry.ts

176 lines
5.2 KiB
TypeScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
import sanitizeHtml from "sanitize-html";
import sanitizeHtmlOptions from "../html/sanitizeHtmlOptions";
import encrypt from "../../dsql/encrypt";
2025-01-28 18:43:16 +00:00
import checkIfIsMaster from "../../../utils/check-if-is-master";
import connDbHandler from "../../../utils/db/conn-db-handler";
import { DbContextsArray } from "./runQuery";
2025-01-13 08:00:21 +00:00
type Param = {
2025-01-28 18:43:16 +00:00
dbContext?: (typeof DbContextsArray)[number];
2025-01-13 08:00:21 +00:00
dbFullName?: string;
tableName: string;
encryptionKey?: string;
encryptionSalt?: string;
data: any;
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
2025-02-12 16:56:44 +00:00
forceLocal?: boolean;
2025-01-13 08:00:21 +00:00
};
2024-11-05 11:12:42 +00:00
/**
2025-01-13 08:00:21 +00:00
* # Update DB Function
* @description
2024-11-05 11:12:42 +00:00
*/
2025-01-13 08:00:21 +00:00
export default async function updateDbEntry({
2024-11-05 11:12:42 +00:00
dbContext,
dbFullName,
tableName,
data,
tableSchema,
identifierColumnName,
identifierValue,
encryptionKey,
encryptionSalt,
2025-02-12 16:56:44 +00:00
forceLocal,
2025-01-13 08:00:21 +00:00
}: Param): Promise<object | null> {
2024-11-05 11:12:42 +00:00
/**
* Check if data is valid
*/
if (!data || !Object.keys(data).length) return null;
2025-02-12 16:56:44 +00:00
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
2025-01-28 18:43:16 +00:00
const DB_CONN = isMaster
? global.DSQL_DB_CONN
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
const DB_RO_CONN = isMaster
? global.DSQL_DB_CONN
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
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);
}
////////////////////////////////////////
////////////////////////////////////////
2025-01-13 08:00:21 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-05 11:12:42 +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()}'`);
////////////////////////////////////////
////////////////////////////////////////
2025-01-28 18:43:16 +00:00
const query = `UPDATE ${
isMaster ? "" : `\`${dbFullName}\`.`
}\`${tableName}\` SET ${updateKeyValueArray.join(
2024-11-05 11:12:42 +00:00
","
)} WHERE \`${identifierColumnName}\`=?`;
updateValues.push(identifierValue);
2025-01-28 18:43:16 +00:00
const updatedEntry = await connDbHandler(DB_CONN, query, updateValues);
2024-11-05 11:12:42 +00:00
/**
* Return statement
*/
return updatedEntry;
}