2023-07-07 14:43:33 +00:00
|
|
|
/**
|
|
|
|
* Imports: Handle imports
|
|
|
|
*/
|
|
|
|
const handler = require("../utils/handler");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update DB Function
|
|
|
|
* ==============================================================================
|
|
|
|
* @description Description
|
2023-07-07 14:52:24 +00:00
|
|
|
* @async
|
2023-07-07 14:43:33 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2023-07-07 14:52:24 +00:00
|
|
|
* @returns {Promise<object|null>}
|
2023-07-07 14:43:33 +00:00
|
|
|
*/
|
2023-07-07 19:13:13 +00:00
|
|
|
async function updateDb({ dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, dbHost, dbPassword, dbUsername, encryptionKey, encryptionSalt }) {
|
2023-07-07 14:43:33 +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 = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < dataKeys.length; i++) {
|
|
|
|
const dataKey = dataKeys[i];
|
|
|
|
|
|
|
|
let value = data[dataKey];
|
|
|
|
|
|
|
|
if (typeof value === "string" && value.match(/^null$/i)) value = "";
|
|
|
|
|
|
|
|
if (!value && value != 0) continue;
|
|
|
|
|
|
|
|
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
|
|
|
updateValues.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
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 handler({
|
|
|
|
queryString: query,
|
|
|
|
database: dbFullName,
|
|
|
|
queryValuesArray: updateValues,
|
|
|
|
dbHost,
|
|
|
|
dbPassword,
|
|
|
|
dbUsername,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return statement
|
|
|
|
*/
|
|
|
|
return updatedEntry;
|
2023-07-07 19:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = updateDb;
|