Add SQL Delete Generator

This commit is contained in:
Benjamin Toby 2024-11-12 19:45:46 +01:00
parent a01dd7107b
commit eb2bc3a25f
3 changed files with 44 additions and 3 deletions

View File

@ -0,0 +1,41 @@
// @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;

View File

@ -1,7 +1,7 @@
// @ts-check
/**
* @typedef {object} SQLINsertGenReturn
* @typedef {object} SQLInsertGenReturn
* @property {string} query
* @property {string[]} values
*/
@ -11,7 +11,7 @@
* @param {any[]} param0.data
* @param {string} param0.tableName
*
* @return {SQLINsertGenReturn | undefined}
* @return {SQLInsertGenReturn | undefined}
*/
function sqlInsertGenerator({ tableName, data }) {
try {

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "2.6.0",
"version": "2.6.1",
"description": "Cloud-based SQL data management tool",
"main": "index.js",
"bin": {