140 lines
4.8 KiB
JavaScript
140 lines
4.8 KiB
JavaScript
/**
|
|
* Imports: Handle imports
|
|
*/
|
|
|
|
const handler = require("../utils/handler");
|
|
|
|
/**
|
|
* Update DB Function
|
|
* ==============================================================================
|
|
* @description Description
|
|
*
|
|
* @param {object} params - An object containing the function parameters.
|
|
* @param {string} params.dbFullName - Database full name
|
|
* @param {string} params.tableName - Table name
|
|
* @param {object} params.data - Data to add
|
|
* @param {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
|
|
* @param {boolean?} params.update - Update this row if it exists
|
|
* @param {string?} params.dbHost - Database host
|
|
* @param {string?} params.dbPassword - Database password
|
|
* @param {string?} params.dbUsername - Database username
|
|
* @param {string?} params.encryptionKey - Encryption key
|
|
* @param {string?} params.encryptionSalt - Encryption salt
|
|
*
|
|
* @returns {object}
|
|
*/
|
|
module.exports = async function update({ dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, update, dbHost, dbPassword, dbUsername, encryptionKey, encryptionSalt }) {
|
|
/**
|
|
* Initialize variables
|
|
*/
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
/**
|
|
* Handle function logic
|
|
*/
|
|
if (duplicateColumnName && typeof duplicateColumnName === "string") {
|
|
const duplicateValue = await handler({
|
|
queryString: `SELECT * FROM \`${tableName}\` WHERE \`${duplicateColumnName}\`=?`,
|
|
queryValuesArray: [duplicateColumnValue],
|
|
database: dbFullName,
|
|
dbHost,
|
|
dbPassword,
|
|
dbUsername,
|
|
});
|
|
|
|
if (duplicateValue && duplicateValue[0] && !update) {
|
|
return null;
|
|
} else if (duplicateValue && duplicateValue[0] && update) {
|
|
return await update();
|
|
}
|
|
} else if (duplicateColumnName && typeof duplicateColumnName === "object" && duplicateColumnValue && typeof duplicateColumnValue === "object") {
|
|
const duplicateArray = duplicateColumnName.map((dupColName, index) => {
|
|
return `\`${dupColName}\`='${duplicateColumnValue[index]}'`;
|
|
});
|
|
|
|
const duplicateValue = await handler({
|
|
queryString: `SELECT * FROM ${tableName} WHERE ${duplicateArray.join(" AND ")}`,
|
|
database: dbFullName,
|
|
});
|
|
|
|
if (duplicateValue && duplicateValue[0] && !update) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Declare variables
|
|
*
|
|
* @description Declare "results" variable
|
|
*/
|
|
const dataKeys = Object.keys(data);
|
|
|
|
let insertKeysArray = [];
|
|
let insertValuesArray = [];
|
|
|
|
for (let i = 0; i < dataKeys.length; i++) {
|
|
const dataKey = dataKeys[i];
|
|
// const correspondingColumnObject = dbColumns.filter((col) => col.Field === dataKey);
|
|
// const { Field, Type, Null, Key, Default, Extra } = correspondingColumnObject;
|
|
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) continue;
|
|
|
|
if (targetFieldSchema?.encrypted) {
|
|
value = await encrypt(value);
|
|
}
|
|
|
|
if (targetFieldSchema?.richText) {
|
|
value = sanitizeHtml(value, sanitizeHtmlOptions).replace(/\n|\r|\n\r/gm, "");
|
|
}
|
|
|
|
insertKeysArray.push("`" + dataKey + "`");
|
|
|
|
let parsedDataValue = value.toString().replace(/(?<!\\)\'/g, "\\'");
|
|
|
|
insertValuesArray.push("'" + parsedDataValue + "'");
|
|
}
|
|
|
|
/** ********************************************** */
|
|
|
|
insertKeysArray.push("date_created");
|
|
insertValuesArray.push("'" + Date() + "'");
|
|
|
|
insertKeysArray.push("date_created_code");
|
|
insertValuesArray.push("'" + Date.now() + "'");
|
|
|
|
/** ********************************************** */
|
|
|
|
insertKeysArray.push("date_updated");
|
|
insertValuesArray.push("'" + Date() + "'");
|
|
|
|
insertKeysArray.push("date_updated_code");
|
|
insertValuesArray.push("'" + Date.now() + "'");
|
|
|
|
/** ********************************************** */
|
|
|
|
const query = `INSERT INTO ${tableName} (${insertKeysArray.join(",")}) VALUES (${insertValuesArray.join(",")})`;
|
|
|
|
const newInsert = await handler({
|
|
queryString: query,
|
|
database: dbFullName,
|
|
});
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
/**
|
|
* Return statement
|
|
*/
|
|
return newInsert;
|
|
};
|