-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
26 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,28 @@ | ||
#!/usr/bin/env node | ||
import { Client } from 'pg' | ||
import pump from 'pump' | ||
import split from 'split2' | ||
import through from 'through2' | ||
import { pipeline } from 'stream' | ||
|
||
import args from './args' | ||
|
||
interface PinoPgConfig { | ||
connectionUrl: string | ||
table: string | ||
column: string | ||
function transporter(table: string, column: string, client: Client) { | ||
return async function* (source: AsyncIterable<string>) { | ||
for await (const line of source) { | ||
await client.query(`INSERT INTO ${table}(${column}) VALUES($1)`, [JSON.parse(line)]) | ||
yield line | ||
} | ||
} | ||
} | ||
|
||
function transporter(config: PinoPgConfig) { | ||
return through.obj((chunk, encoding, callback) => { | ||
const client = new Client({ connectionString: config.connectionUrl}) | ||
client.connect((connectErr) => { | ||
if(connectErr !== null) { | ||
console.error('Failed to connect to PostgreSQL server.', connectErr) | ||
return callback('Failed to connect to PostgreSQL server.') | ||
} | ||
|
||
client.query(`INSERT INTO ${config.table}(${config.column}) VALUES($1)`, [JSON.parse(chunk)], (queryErr, result) => { | ||
if(queryErr !== null) { | ||
console.error('Query failed.', queryErr) | ||
return callback('Query failed.') | ||
} | ||
client.end(endErr => { | ||
if(endErr !== undefined) { | ||
console.error('Fail to close PG connection.', endErr) | ||
return callback('Fail to close PG connection.') | ||
} | ||
callback(null, chunk) | ||
}) | ||
}) | ||
function main() { | ||
const client = new Client({ connectionString: args.connectionUrl}) | ||
client.connect((connectErr) => { | ||
if(connectErr !== null) { | ||
return console.error('Failed to connect to PostgreSQL server.', connectErr) | ||
} | ||
pipeline(process.stdin, split(), transporter(args.table, args.column, client) as any, process.stdout, () => { | ||
client.end() | ||
}) | ||
}) | ||
} | ||
|
||
function main() { | ||
pump(process.stdin, split(), transporter(args as PinoPgConfig), process.stdout) | ||
} | ||
|
||
export { main, transporter }; |