From eb2bc3a25f8c9bb6f5933552753497639df72f74 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Tue, 12 Nov 2024 19:45:46 +0100 Subject: [PATCH] Add SQL Delete Generator --- functions/sql/sql-delete-generator.js | 41 +++++++++++++++++++++++++++ functions/sql/sql-insert-generator.js | 4 +-- package.json | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 functions/sql/sql-delete-generator.js diff --git a/functions/sql/sql-delete-generator.js b/functions/sql/sql-delete-generator.js new file mode 100644 index 0000000..781e9e5 --- /dev/null +++ b/functions/sql/sql-delete-generator.js @@ -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; diff --git a/functions/sql/sql-insert-generator.js b/functions/sql/sql-insert-generator.js index 421005a..ac9dca9 100644 --- a/functions/sql/sql-insert-generator.js +++ b/functions/sql/sql-insert-generator.js @@ -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 { diff --git a/package.json b/package.json index cd07a1d..aed9961 100644 --- a/package.json +++ b/package.json @@ -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": {