Skip to content

Instantly share code, notes, and snippets.

@alastaircoote
Created September 15, 2016 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alastaircoote/25c0f05a2885d71565e91bd293d493c5 to your computer and use it in GitHub Desktop.
Save alastaircoote/25c0f05a2885d71565e91bd293d493c5 to your computer and use it in GitHub Desktop.
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()
});
}
}
@rarkins
Copy link

rarkins commented Oct 9, 2017

@alastaircoote how scalable have you found this to be? and where does client come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment