This commit is contained in:
Benjamin Toby 2024-11-12 12:00:07 +01:00
parent 106632fdb5
commit f5e89799fc

View File

@ -0,0 +1,67 @@
// @ts-check
/**
* @typedef {object} SQLINsertGenReturn
* @property {string} query
* @property {string[]} values
*/
/**
* @param {object} param0
* @param {any[]} param0.data
* @param {string} param0.tableName
*
* @return {SQLINsertGenReturn | undefined}
*/
function sqlInsertGenerator({ tableName, data }) {
try {
if (Array.isArray(data) && data?.[0]) {
/** @type {string[]} */
let insertKeys = [];
data.forEach((dt) => {
const kys = Object.keys(dt);
kys.forEach((ky) => {
if (!insertKeys.includes(ky)) {
insertKeys.push(ky);
}
});
});
/** @type {string[]} */
let queryBatches = [];
/** @type {string[]} */
let queryValues = [];
data.forEach((item) => {
queryBatches.push(
`(${insertKeys
.map((ky) => {
queryValues.push(
item[ky]?.toString()?.match(/./)
? item[ky]
: null
);
return "?";
})
.join(",")})`
);
});
let query = `INSERT INTO ${tableName} (${insertKeys.join(
","
)}) VALUES ${queryBatches.join(",")}`;
return {
query: query,
values: queryValues,
};
} else {
return undefined;
}
} catch (/** @type {any} */ error) {
console.log(`SQL insert gen ERROR: ${error.message}`);
return undefined;
}
}
module.exports = sqlInsertGenerator;