62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
|
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
|
import connDbHandler from "../../../utils/db/conn-db-handler";
|
|
import { DbContextsArray } from "./runQuery";
|
|
|
|
type Param<T extends { [k: string]: any } = any, K extends string = string> = {
|
|
dbContext?: (typeof DbContextsArray)[number];
|
|
dbFullName?: string;
|
|
tableName: K;
|
|
tableSchema?: DSQL_TableSchemaType;
|
|
identifierColumnName: keyof T;
|
|
identifierValue: string | number;
|
|
forceLocal?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # Delete DB Entry Function
|
|
* @description
|
|
*/
|
|
export default async function deleteDbEntry<
|
|
T extends { [k: string]: any } = any,
|
|
K extends string = string
|
|
>({
|
|
dbContext,
|
|
dbFullName,
|
|
tableName,
|
|
identifierColumnName,
|
|
identifierValue,
|
|
forceLocal,
|
|
}: Param<T, K>): Promise<PostInsertReturn | null> {
|
|
try {
|
|
const isMaster = forceLocal
|
|
? true
|
|
: checkIfIsMaster({ dbContext, dbFullName });
|
|
|
|
const DB_CONN = isMaster
|
|
? global.DSQL_DB_CONN
|
|
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
|
|
|
|
/**
|
|
* Execution
|
|
*
|
|
* @description
|
|
*/
|
|
const query = `DELETE FROM ${
|
|
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
|
}\`${tableName}\` WHERE \`${identifierColumnName.toString()}\`=?`;
|
|
|
|
const deletedEntry = await connDbHandler(DB_CONN, query, [
|
|
identifierValue,
|
|
]);
|
|
|
|
/**
|
|
* Return statement
|
|
*/
|
|
return deletedEntry;
|
|
} catch (error) {
|
|
global.ERROR_CALLBACK?.(`Error Deleting Entry`, error as Error);
|
|
return null;
|
|
}
|
|
}
|