pino-postgres
Advanced tools
Comparing version 0.0.10 to 0.0.11
38
index.js
@@ -11,2 +11,5 @@ #! /usr/bin/env node | ||
let buffer = [] | ||
let interval | ||
class PinoTransform extends Transform { | ||
@@ -20,3 +23,3 @@ constructor (opts, sql) { | ||
_transform (chunk, encoding, callback) { | ||
const { schema, table, column, passThrough } = this.opts | ||
const { column, passThrough } = this.opts | ||
const content = chunk.toString('utf-8') | ||
@@ -30,11 +33,4 @@ let log = {} | ||
this.sql` | ||
INSERT INTO ${this.sql(schema)}.${this.sql(table)} (${this.sql(column)}) VALUES (${this.sql.json(log)}) | ||
ON CONFLICT DO NOTHING; | ||
` | ||
.then(() => callback(null, passThrough ? `${chunk}\n` : null)) | ||
.catch((err) => { | ||
console.error('error in pino-postgres transform', err) | ||
callback(null, passThrough ? `${chunk}\n` : null) | ||
}) | ||
buffer.push({ [column]: log }) | ||
callback(null, passThrough ? `${chunk}\n` : null) | ||
} | ||
@@ -79,3 +75,23 @@ } | ||
const transport = new PinoTransform(opts, sql) | ||
transport.on('end', sql.end) | ||
transport.on('end', () => { | ||
sql.end() | ||
clearInterval(interval) | ||
}) | ||
interval = setInterval(() => { | ||
if (opts.debug) { | ||
console.log(`DEBUG - buffer size: ${buffer.length}`) | ||
} | ||
if (buffer.length) { | ||
this.sql` | ||
INSERT INTO ${this.sql(opts.schema)}.${this.sql(opts.table)} (${this.sql(opts.column)}) VALUES (${this.sql(buffer)}) | ||
ON CONFLICT DO NOTHING; | ||
`.catch((err) => { | ||
console.error('error in pino-postgres sql', err) | ||
}) | ||
buffer = [] | ||
} | ||
}, 5000) | ||
interval.unref() | ||
pipeline(process.stdin, split(), transport, process.stdout, err => { | ||
@@ -82,0 +98,0 @@ if (err) { |
{ | ||
"name": "pino-postgres", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Pino transport for Postgres", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4806
90