datasquirel/package-shared/functions/backend/db/deleteDbEntry.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
2024-11-06 06:26:23 +00:00
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
const DSQL_USER_DB_HANDLER = require("../../../utils/backend/global-db/DSQL_USER_DB_HANDLER");
2024-12-06 11:55:03 +00:00
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
2023-09-21 14:00:04 +00:00
/**
* Imports: Handle imports
*/
/**
* Delete DB Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {string} [params.dbContext] - What is the database context? "Master"
* or "Dsql User". Defaults to "Master"
* @param {("Read Only" | "Full Access")} [params.paradigm] - What is the paradigm for "Dsql User"?
* "Read only" or "Full Access"? Defaults to "Read Only"
* @param {string} params.dbFullName - Database full name
* @param {string} params.tableName - Table name
2024-11-06 06:26:23 +00:00
* @param {import("../../../types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
2023-09-21 14:00:04 +00:00
* @param {string} params.identifierColumnName - Update row identifier column name
* @param {string|number} params.identifierValue - Update row identifier column value
2024-12-06 11:55:03 +00:00
* @param {boolean} [params.useLocal]
2023-09-21 14:00:04 +00:00
*
* @returns {Promise<object|null>}
*/
2024-10-14 06:49:01 +00:00
async function deleteDbEntry({
dbContext,
paradigm,
dbFullName,
tableName,
identifierColumnName,
identifierValue,
2024-12-06 11:55:03 +00:00
useLocal,
2024-10-14 06:49:01 +00:00
}) {
2023-09-21 14:00:04 +00:00
try {
/**
* Check if data is valid
*/
2024-12-09 11:45:39 +00:00
const isMaster = useLocal
? true
: dbContext?.match(/dsql.user/i)
2024-11-06 06:26:23 +00:00
? false
: dbFullName && !dbFullName.match(/^datasquirel$/)
? false
: true;
/** @type { (a1:any, a2?:any) => any } */
2024-12-06 11:55:03 +00:00
const dbHandler = useLocal
? LOCAL_DB_HANDLER
: isMaster
? DB_HANDLER
: DSQL_USER_DB_HANDLER;
2023-09-21 14:00:04 +00:00
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Execution
*
* @description
*/
const query = `DELETE FROM ${tableName} WHERE \`${identifierColumnName}\`=?`;
2024-11-06 06:26:23 +00:00
const deletedEntry = isMaster
? await dbHandler(query, [identifierValue])
: await dbHandler({
paradigm,
queryString: query,
database: dbFullName,
queryValues: [identifierValue],
});
2023-09-21 14:00:04 +00:00
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/
return deletedEntry;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return null;
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
module.exports = deleteDbEntry;