datasquirel/package-shared/functions/dsql/sql/sql-delete-generator.ts
2025-01-10 20:10:28 +01:00

37 lines
823 B
TypeScript

interface SQLDeleteGenReturn {
query: string;
values: string[];
}
/**
* # SQL Delete Generator
*/
export default function sqlDeleteGenerator({
tableName,
data,
}: {
data: any;
tableName: string;
}): SQLDeleteGenReturn | undefined {
try {
let queryStr = `DELETE FROM ${tableName}`;
let deleteBatch: string[] = [];
let queryArr: string[] = [];
Object.keys(data).forEach((ky) => {
deleteBatch.push(`${ky}=?`);
queryArr.push(data[ky]);
});
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
return {
query: queryStr,
values: queryArr,
};
} catch (/** @type {any} */ error: any) {
console.log(`SQL delete gen ERROR: ${error.message}`);
return undefined;
}
}