datasquirel/package-shared/functions/backend/db/deleteDbEntry.ts
2025-01-10 20:10:28 +01:00

69 lines
1.9 KiB
TypeScript

import DB_HANDLER from "../../../utils/backend/global-db/DB_HANDLER";
import DSQL_USER_DB_HANDLER from "../../../utils/backend/global-db/DSQL_USER_DB_HANDLER";
import LOCAL_DB_HANDLER from "../../../utils/backend/global-db/LOCAL_DB_HANDLER";
type Param = {
dbContext?: string;
paradigm?: "Read Only" | "Full Access";
dbFullName: string;
tableName: string;
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
useLocal?: boolean;
};
/**
* # Delete DB Entry Function
* @description
*/
export default async function deleteDbEntry({
dbContext,
paradigm,
dbFullName,
tableName,
identifierColumnName,
identifierValue,
useLocal,
}: Param): Promise<object | null> {
try {
const isMaster = useLocal
? true
: dbContext?.match(/dsql.user/i)
? false
: dbFullName && !dbFullName.match(/^datasquirel$/)
? false
: true;
/** @type { (a1:any, a2?:any) => any } */
const dbHandler: (a1: any, a2?: any) => any = useLocal
? LOCAL_DB_HANDLER
: isMaster
? DB_HANDLER
: DSQL_USER_DB_HANDLER;
/**
* Execution
*
* @description
*/
const query = `DELETE FROM ${tableName} WHERE \`${identifierColumnName}\`=?`;
const deletedEntry = isMaster
? await dbHandler(query, [identifierValue])
: await dbHandler({
paradigm,
queryString: query,
database: dbFullName,
queryValues: [identifierValue],
});
/**
* Return statement
*/
return deletedEntry;
} catch (error) {
return null;
}
}