Created
August 22, 2021 21:06
-
-
Save twocolors/ab161336c61caa465169b91cebb0b006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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