dsql-admin/dsql-app/package-shared/functions/backend/db/deleteDbEntry.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-01-28 18:43:16 +00:00
import checkIfIsMaster from "../../../utils/check-if-is-master";
import connDbHandler from "../../../utils/db/conn-db-handler";
import { DbContextsArray } from "./runQuery";
2025-01-13 08:00:21 +00:00
type Param = {
2025-01-28 18:43:16 +00:00
dbContext?: (typeof DbContextsArray)[number];
2025-01-13 08:00:21 +00:00
dbFullName: string;
tableName: string;
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
2025-02-12 16:56:44 +00:00
forceLocal?: boolean;
2025-01-13 08:00:21 +00:00
};
/**
* # Delete DB Entry Function
* @description
*/
export default async function deleteDbEntry({
dbContext,
dbFullName,
tableName,
identifierColumnName,
identifierValue,
2025-02-12 16:56:44 +00:00
forceLocal,
2025-01-13 08:00:21 +00:00
}: Param): Promise<object | null> {
try {
2025-02-12 16:56:44 +00:00
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
2025-01-13 08:00:21 +00:00
2025-01-28 18:43:16 +00:00
const DB_CONN = isMaster
? global.DSQL_DB_CONN
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
const DB_RO_CONN = isMaster
? global.DSQL_DB_CONN
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
2025-01-13 08:00:21 +00:00
/**
* Execution
*
* @description
*/
2025-01-28 18:43:16 +00:00
const query = `DELETE FROM ${
isMaster ? "" : `\`${dbFullName}\`.`
}\`${tableName}\` WHERE \`${identifierColumnName}\`=?`;
2025-01-13 08:00:21 +00:00
2025-01-28 18:43:16 +00:00
const deletedEntry = await connDbHandler(DB_CONN, query, [
identifierValue,
]);
2025-01-13 08:00:21 +00:00
/**
* Return statement
*/
return deletedEntry;
} catch (error) {
2025-02-19 19:38:56 +00:00
global.ERROR_CALLBACK?.(`Error Deleting Entry`, error as Error);
2025-01-13 08:00:21 +00:00
return null;
}
}