37 lines
823 B
TypeScript
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;
|
||
|
}
|
||
|
}
|