datasquirel/engine/db/addDb.js

163 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-07-07 13:58:14 +00:00
/**
* Imports: Handle imports
*/
const encrypt = require("../../functions/encrypt");
const sanitizeHtml = require("sanitize-html");
const sanitizeHtmlOptions = require("../utils/sanitizeHtmlOptions");
2023-07-08 06:15:25 +00:00
const dsqlDbHandler = require("../utils/dsqlDbHandler");
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
*/
2023-07-07 19:13:13 +00:00
async function addDb({ dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, update, dbHost, dbPassword, dbUsername, encryptionKey, encryptionSalt }) {
2023-07-07 13:58:14 +00:00
/**
* Initialize variables
*/
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/
2023-07-07 15:55:56 +00:00
2023-07-07 13:58:14 +00:00
if (duplicateColumnName && typeof duplicateColumnName === "string") {
2023-07-08 06:15:25 +00:00
const duplicateValue = await dsqlDbHandler({
2023-07-07 13:58:14 +00:00
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
}
}
/**
* 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) {
2023-07-07 15:50:44 +00:00
console.log("DSQL: Error in parsing data keys =>", error.message);
2023-07-07 15:45:55 +00:00
}
2023-07-07 13:58:14 +00:00
}
/** ********************************************** */
2023-07-07 19:27:43 +00:00
insertKeysArray.push("`date_created`");
2023-07-07 13:58:14 +00:00
insertValuesArray.push(Date());
2023-07-07 19:27:43 +00:00
insertKeysArray.push("`date_created_code`");
2023-07-07 13:58:14 +00:00
insertValuesArray.push(Date.now());
/** ********************************************** */
2023-07-07 19:27:43 +00:00
insertKeysArray.push("`date_updated`");
2023-07-07 13:58:14 +00:00
insertValuesArray.push(Date());
2023-07-07 19:27:43 +00:00
insertKeysArray.push("`date_updated_code`");
2023-07-07 13:58:14 +00:00
insertValuesArray.push(Date.now());
/** ********************************************** */
const query = `INSERT INTO \`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
const queryValuesArray = insertValuesArray;
2023-07-08 06:15:25 +00:00
const newInsert = await dsqlDbHandler({
2023-07-07 13:58:14 +00:00
queryString: query,
database: dbFullName,
queryValuesArray,
dbHost,
dbPassword,
dbUsername,
encryptionKey,
encryptionSalt,
tableSchema,
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/
return newInsert;
2023-07-07 19:13:13 +00:00
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
module.exports = addDb;