Skip to content

Instantly share code, notes, and snippets.

@twocolors
Created August 22, 2021 21:06
Show Gist options
  • Save twocolors/ab161336c61caa465169b91cebb0b006 to your computer and use it in GitHub Desktop.
Save twocolors/ab161336c61caa465169b91cebb0b006 to your computer and use it in GitHub Desktop.
let table = env.get('table');
let add = env.get('add');
if (table && msg.payload) {
if (typeof msg.payload === 'string') {
msg.payload = JSON.parse(msg.payload);
}
if (typeof msg.payload === 'object') {
// support value
if (typeof add === 'object') {
msg.payload = Object.assign({}, msg.payload, add);
}
// https://reedbarger.com/how-to-transform-javascript-objects-the-power-of-objectkeys-values-entries/
let col = Object.keys(msg.payload).map((key) => '`'+key+'`').join(',');
let val = Object.keys(msg.payload).map((key) => ':'+key).join(',');
// msg.topic = 'INSERT INTO '+table+' ('+col+') VALUES ('+val+')';
msg.topic = `INSERT INTO \`${table}\` (${col}) VALUES (${val})`;
return msg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment