Created
September 15, 2016 16:15
-
-
Save alastaircoote/25c0f05a2885d71565e91bd293d493c5 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
class DbStream extends stream.Writable { | |
constructor() { | |
super( {objectMode: true} ); | |
} | |
_write(data, encoding, cb) { | |
let specificFields = ["name", "pid", "hostname", "time", "level", "msg", "req_id", "v"]; | |
let fieldData = []; | |
specificFields.forEach( (field) => { | |
fieldData.push(data[field]); | |
delete data[field]; | |
} ); | |
// Manually add the data field, as we didn't want to iterate over it earlier | |
specificFields.push("data"); | |
fieldData.push(data); | |
// The query needs to be formatted as $1, $2, $3 etc. So create an array for that. | |
let variableNumbers = specificFields.map((item, i) => "$" + (i + 1)); | |
let query = "INSERT INTO log_entries (" + | |
specificFields.join(",") + | |
") VALUES (" + | |
variableNumbers.join(",") | |
+ ":: jsonb)"; | |
client.query(query, fieldData, (err, result) => { | |
if (err) { | |
console.error(err); | |
} | |
cb() | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alastaircoote how scalable have you found this to be? and where does
client
come from?