datasquirel/package-shared/functions/backend/db/updateDbEntry.ts

187 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import sanitizeHtml from "sanitize-html";
import sanitizeHtmlOptions from "../html/sanitizeHtmlOptions";
import DB_HANDLER from "../../../utils/backend/global-db/DB_HANDLER";
import DSQL_USER_DB_HANDLER from "../../../utils/backend/global-db/DSQL_USER_DB_HANDLER";
import encrypt from "../../dsql/encrypt";
import LOCAL_DB_HANDLER from "../../../utils/backend/global-db/LOCAL_DB_HANDLER";
type Param = {
dbContext?: "Master" | "Dsql User";
paradigm?: "Read Only" | "Full Access";
dbFullName?: string;
tableName: string;
encryptionKey?: string;
encryptionSalt?: string;
data: any;
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
useLocal?: boolean;
};
2023-09-21 14:00:04 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Update DB Function
* @description
2023-09-21 14:00:04 +00:00
*/
2025-01-10 19:10:28 +00:00
export default async function updateDbEntry({
2024-10-14 06:49:01 +00:00
dbContext,
paradigm,
dbFullName,
tableName,
data,
tableSchema,
identifierColumnName,
identifierValue,
encryptionKey,
encryptionSalt,
2024-12-06 11:55:03 +00:00
useLocal,
2025-01-10 19:10:28 +00:00
}: Param): Promise<object | null> {
2023-09-21 14:00:04 +00:00
/**
* Check if data is valid
*/
if (!data || !Object.keys(data).length) return null;
2024-12-09 11:45:39 +00:00
const isMaster = useLocal
? true
: dbContext?.match(/dsql.user/i)
2024-11-06 06:26:23 +00:00
? false
: dbFullName && !dbFullName.match(/^datasquirel$/)
? false
: true;
/** @type {(a1:any, a2?:any)=> any } */
2025-01-10 19:10:28 +00:00
const dbHandler: (a1: any, a2?: any) => any = useLocal
2024-12-06 11:55:03 +00:00
? LOCAL_DB_HANDLER
: isMaster
? DB_HANDLER
: DSQL_USER_DB_HANDLER;
2024-11-06 06:26:23 +00:00
2023-09-21 14:00:04 +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];
2024-11-06 06:26:23 +00:00
// @ts-ignore
2023-09-21 14:00:04 +00:00
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
2024-11-06 06:26:23 +00:00
if (value == null || value == undefined) continue;
2024-12-12 04:59:14 +00:00
const htmlRegex = /<[^>]+>/g;
if (targetFieldSchema?.richText || String(value).match(htmlRegex)) {
2024-11-06 06:26:23 +00:00
value = sanitizeHtml(value, sanitizeHtmlOptions);
}
2023-09-21 14:00:04 +00:00
if (targetFieldSchema?.encrypted) {
2024-12-06 10:31:24 +00:00
value = encrypt({
data: value,
encryptionKey,
encryptionSalt,
});
2023-09-21 14:00:04 +00:00
}
if (typeof value === "object") {
value = JSON.stringify(value);
}
if (targetFieldSchema?.pattern) {
2024-10-14 06:49:01 +00:00
const pattern = new RegExp(
targetFieldSchema.pattern,
targetFieldSchema.patternFlags || ""
);
2024-11-06 06:26:23 +00:00
if (!pattern.test(value)) {
2023-09-21 14:00:04 +00:00
console.log("DSQL: Pattern not matched =>", value);
value = "";
}
}
2024-11-06 06:26:23 +00:00
if (typeof value === "string" && value.match(/^null$/i)) {
2023-09-21 14:00:04 +00:00
value = {
toSqlString: function () {
return "NULL";
},
};
}
2024-11-06 06:26:23 +00:00
if (typeof value === "string" && !value.match(/./i)) {
value = {
toSqlString: function () {
return "NULL";
},
};
}
2023-09-21 14:00:04 +00:00
updateKeyValueArray.push(`\`${dataKey}\`=?`);
2024-11-06 06:26:23 +00:00
if (typeof value == "number") {
updateValues.push(String(value));
} else {
updateValues.push(value);
}
2023-09-21 14:00:04 +00:00
////////////////////////////////////////
////////////////////////////////////////
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2023-09-21 14:00:04 +00:00
////////////////////////////////////////
////////////////////////////////////////
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);
2024-11-06 06:26:23 +00:00
const updatedEntry = isMaster
? await dbHandler(query, updateValues)
: await dbHandler({
paradigm,
database: dbFullName,
queryString: query,
queryValues: updateValues,
});
2023-09-21 14:00:04 +00:00
/**
* Return statement
*/
return updatedEntry;
}