datasquirel/package-shared/functions/dsql/sql/sql-delete-generator.js

42 lines
959 B
JavaScript
Raw Permalink Normal View History

2024-11-12 18:45:46 +00:00
// @ts-check
/**
* @typedef {object} SQLDeleteGenReturn
* @property {string} query
* @property {string[]} values
*/
/**
* @param {object} param0
* @param {any} param0.data
* @param {string} param0.tableName
*
* @return {SQLDeleteGenReturn | undefined}
*/
function sqlDeleteGenerator({ tableName, data }) {
try {
let queryStr = `DELETE FROM ${tableName}`;
/** @type {string[]} */
let deleteBatch = [];
/** @type {string[]} */
let queryArr = [];
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) {
console.log(`SQL delete gen ERROR: ${error.message}`);
return undefined;
}
}
module.exports = sqlDeleteGenerator;