Add SQL Delete Generator
This commit is contained in:
parent
a01dd7107b
commit
eb2bc3a25f
41
functions/sql/sql-delete-generator.js
Normal file
41
functions/sql/sql-delete-generator.js
Normal 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;
|
@ -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 {
|
||||
|
@ -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": {
|
||||
|
Loading…
Reference in New Issue
Block a user