UNPKG

sqmicro-driver-pg

Version:

Postgres driver for SQ micro connection

41 lines (32 loc) 1.05 kB
// Test connection / reconnection logic const PgDriver = require('..'); const Connection = require('sqmicro-connection')(PgDriver); // To simulate network problems use socat: // socat TCP-LISTEN:5435,reuseaddr,fork TCP:localhost:5432 const connectionString = 'postgresql://user:passuser@localhost:5435/proto-pgs'; // The number of rows will be logged. const SQL = 'SELECT * from command'; process.on('SIGINT', async () => { await connection.disconnect(); console.log('Bye, bye user...'); process.exit(); }); process.on('unhandledRejection', (reason, p) => { reason.stack; console.error('Unhandled Rej ', reason) }); const connection = new Connection({ connectionString }); connection.on('error', e => console.log(`got error: ${e}`)); connection.connect(); setInterval( () => work(), 5000 ); async function work() { if (!connection.isConnected) { console.log('--- no connection'); return; } let res = await connection.query(SQL); console.log(`+++ RES[${res.rows.length}]`); }