2023-07-07 13:58:14 +00:00
|
|
|
/**
|
|
|
|
* Imports: Handle imports
|
|
|
|
*/
|
|
|
|
|
|
|
|
const encrypt = require("../../functions/encrypt");
|
|
|
|
const handler = require("../utils/handler");
|
|
|
|
const sanitizeHtml = require("sanitize-html");
|
|
|
|
const sanitizeHtmlOptions = require("../utils/sanitizeHtmlOptions");
|
2023-07-07 14:43:33 +00:00
|
|
|
const updateDb = require("./updateDb");
|
2023-07-07 13:58:14 +00:00
|
|
|
|
|
|
|
/**
|
2023-07-07 14:16:24 +00:00
|
|
|
* Add a db Entry Function
|
2023-07-07 13:58:14 +00:00
|
|
|
* ==============================================================================
|
|
|
|
* @description Description
|
2023-07-07 14:52:24 +00:00
|
|
|
* @async
|
2023-07-07 13:58:14 +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.duplicateColumnName - Duplicate column name
|
|
|
|
* @param {string?} params.duplicateColumnValue - Duplicate 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 13:58:14 +00:00
|
|
|
*/
|
|
|
|
module.exports = async function add({ dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, 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) {
|
2023-07-07 14:43:33 +00:00
|
|
|
return await updateDb({
|
|
|
|
dbFullName,
|
|
|
|
tableName,
|
|
|
|
data,
|
|
|
|
tableSchema,
|
|
|
|
identifierColumnName: duplicateColumnName,
|
|
|
|
identifierValue: duplicateColumnValue,
|
|
|
|
dbHost,
|
|
|
|
dbPassword,
|
|
|
|
dbUsername,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
});
|
2023-07-07 13:58:14 +00:00
|
|
|
}
|
|
|
|
} 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++) {
|
2023-07-07 15:45:55 +00:00
|
|
|
try {
|
|
|
|
const dataKey = dataKeys[i];
|
|
|
|
let value = data[dataKey];
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
const targetFieldSchemaArray = tableSchema ? tableSchema?.fields.filter((field) => field.fieldName === dataKey) : null;
|
|
|
|
const targetFieldSchema = targetFieldSchemaArray && targetFieldSchemaArray[0] ? targetFieldSchemaArray[0] : null;
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
if (!value) continue;
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
if (targetFieldSchema?.encrypted) {
|
|
|
|
value = encrypt({ data: value, encryptionKey, encryptionSalt });
|
|
|
|
}
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
if (targetFieldSchema?.richText) {
|
|
|
|
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
|
|
|
}
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
insertKeysArray.push("`" + dataKey + "`");
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
if (typeof value === "object") {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
2023-07-07 13:58:14 +00:00
|
|
|
|
2023-07-07 15:45:55 +00:00
|
|
|
insertValuesArray.push(value);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error in add DB try catch block =>", error.message);
|
|
|
|
}
|
2023-07-07 13:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
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.map(() => "?").join(",")})`;
|
|
|
|
const queryValuesArray = insertValuesArray;
|
|
|
|
|
|
|
|
const newInsert = await handler({
|
|
|
|
queryString: query,
|
|
|
|
database: dbFullName,
|
|
|
|
queryValuesArray,
|
|
|
|
dbHost,
|
|
|
|
dbPassword,
|
|
|
|
dbUsername,
|
|
|
|
encryptionKey,
|
|
|
|
encryptionSalt,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return statement
|
|
|
|
*/
|
|
|
|
return newInsert;
|
|
|
|
};
|