diff --git a/engine/db/add.js b/engine/db/add.js index feb99c2..8700c7a 100644 --- a/engine/db/add.js +++ b/engine/db/add.js @@ -6,6 +6,7 @@ const encrypt = require("../../functions/encrypt"); const handler = require("../utils/handler"); const sanitizeHtml = require("sanitize-html"); const sanitizeHtmlOptions = require("../utils/sanitizeHtmlOptions"); +const updateDb = require("./updateDb"); /** * Add a db Entry Function @@ -53,7 +54,19 @@ module.exports = async function add({ dbFullName, tableName, data, tableSchema, if (duplicateValue && duplicateValue[0] && !update) { return null; } else if (duplicateValue && duplicateValue[0] && update) { - return await update(); + return await updateDb({ + dbFullName, + tableName, + data, + tableSchema, + identifierColumnName: duplicateColumnName, + identifierValue: duplicateColumnValue, + dbHost, + dbPassword, + dbUsername, + encryptionKey, + encryptionSalt, + }); } } else if (duplicateColumnName && typeof duplicateColumnName === "object" && duplicateColumnValue && typeof duplicateColumnValue === "object") { const duplicateArray = duplicateColumnName.map((dupColName, index) => { diff --git a/engine/db/deleteDb.js b/engine/db/deleteDb.js new file mode 100644 index 0000000..231e982 --- /dev/null +++ b/engine/db/deleteDb.js @@ -0,0 +1,74 @@ +/** + * Imports: Handle imports + */ +const handler = require("../utils/handler"); + +/** + * Delete DB Entry 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 {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 deleteDb({ dbFullName, tableName, tableSchema, identifierColumnName, identifierValue, dbHost, dbPassword, dbUsername, encryptionKey, encryptionSalt }) { + try { + /** + * Check if data is valid + */ + + //////////////////////////////////////// + //////////////////////////////////////// + //////////////////////////////////////// + + /** + * Execution + * + * @description + */ + const query = `DELETE FROM ${tableName} WHERE \`${identifierColumnName}\`=?`; + + const deletedEntry = await handler({ + queryString: query, + database: dbFullName, + queryValuesArray: [identifierValue], + dbHost, + dbPassword, + dbUsername, + encryptionKey, + encryptionSalt, + tableSchema, + }); + + //////////////////////////////////////// + //////////////////////////////////////// + //////////////////////////////////////// + + /** + * Return statement + */ + return deletedEntry; + + //////////////////////////////////////// + //////////////////////////////////////// + //////////////////////////////////////// + } catch (error) { + //////////////////////////////////////// + //////////////////////////////////////// + //////////////////////////////////////// + + return null; + } +}; diff --git a/engine/db/test/add.js b/engine/db/test/add.js deleted file mode 100644 index 2ccfc81..0000000 --- a/engine/db/test/add.js +++ /dev/null @@ -1,127 +0,0 @@ -const fs = require("fs"); -const dbHandler = require("../dbHandler"); - -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ - -/** - * Add Database Entry - * ============================================================================== - * @param {object} params - foundUser if any - * @param {string} params.tableName - Table Name - * @param {object} params.data - Data to be added - * @param {string?} params.duplicateColumnName - Duplicate Column Name - * @param {string | number?} params.duplicateColumnValue - Duplicate Column Value - * - * @returns {object} - */ -async function addDbEntry({ tableName, data, duplicateColumnName, duplicateColumnValue }) { - /** - * Check Duplicate if specified - * - * @description Check Duplicate if specified - */ - if (duplicateColumnName) { - let duplicateEntry = await dbHandler(`SELECT ${duplicateColumnName} FROM ${tableName} WHERE ${duplicateColumnName}='${duplicateColumnValue}'`); - - if (duplicateEntry && duplicateEntry[0]) 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]; - let dataValue = data[dataKey]; - // const correspondingColumnObject = dbColumns.filter((col) => col.Field === dataKey); - // const { Field, Type, Null, Key, Default, Extra } = correspondingColumnObject; - - if (!dataValue) continue; - - insertKeysArray.push("`" + dataKey + "`"); - - if (typeof dataValue === "object") { - dataValue = JSON.stringify(data[dataKey]); - } - - // let parsedDataValue = dataValue.toString().replace(/\'/g, "\\'"); - - insertValuesArray.push(dataValue); - } - - /** ********************************************** */ - - let existingDateCreatedColumn = await dbHandler(`SHOW COLUMNS FROM \`${tableName}\` WHERE Field = 'date_created'`); - if (!existingDateCreatedColumn || !existingDateCreatedColumn[0]) { - await dbHandler(`ALTER TABLE ${tableName} ADD COLUMN date_created VARCHAR(255) NOT NULL`); - } - - insertKeysArray.push("date_created"); - insertValuesArray.push(Date()); - - /** ********************************************** */ - - let existingDateCreatedCodeColumn = await dbHandler(`SHOW COLUMNS FROM ${tableName} WHERE Field = 'date_created_code'`); - if (!existingDateCreatedCodeColumn || !existingDateCreatedCodeColumn[0]) { - await dbHandler(`ALTER TABLE ${tableName} ADD COLUMN date_created_code BIGINT NOT NULL`); - } - - insertKeysArray.push("date_created_code"); - insertValuesArray.push(Date.now()); - - /** ********************************************** */ - - let existingDateCodeColumn = await dbHandler(`SHOW COLUMNS FROM ${tableName} WHERE Field = 'date_code'`); - if (existingDateCodeColumn && existingDateCodeColumn[0]) { - insertKeysArray.push("date_code"); - insertValuesArray.push(Date.now()); - } - - /** ********************************************** */ - /** ********************************************** */ - /** ********************************************** */ - - let existingDateUpdatedColumn = await dbHandler(`SHOW COLUMNS FROM ${tableName} WHERE Field = 'date_updated'`); - if (!existingDateUpdatedColumn || !existingDateUpdatedColumn[0]) { - await dbHandler(`ALTER TABLE ${tableName} ADD COLUMN date_updated VARCHAR(255) NOT NULL`); - } - - insertKeysArray.push("date_updated"); - insertValuesArray.push(Date()); - - /** ********************************************** */ - - let existingDateUpdatedCodeColumn = await dbHandler(`SHOW COLUMNS FROM ${tableName} WHERE Field = 'date_updated_code'`); - if (!existingDateUpdatedCodeColumn || !existingDateUpdatedCodeColumn[0]) { - await dbHandler(`ALTER TABLE ${tableName} ADD COLUMN date_updated_code BIGINT NOT NULL`); - } - - insertKeysArray.push("date_updated_code"); - insertValuesArray.push(Date.now()); - - /** ********************************************** */ - - const query = `INSERT INTO ${tableName} (${insertKeysArray.join(",")}) VALUES (${insertValuesArray.map((val) => "?").join(",")})`; - const queryValuesArray = insertValuesArray; - - const newInsert = await dbHandler(query, queryValuesArray); - - /** ********************************************** */ - - return newInsert; - - /** ********************************************** */ - /** ********************************************** */ - /** ********************************************** */ -} diff --git a/engine/db/update.js b/engine/db/update.js deleted file mode 100644 index 2afee00..0000000 --- a/engine/db/update.js +++ /dev/null @@ -1,139 +0,0 @@ -/** - * 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(/(?