sqmicro-driver-pg
Version:
Postgres driver for SQ micro connection
55 lines (45 loc) • 1.58 kB
JavaScript
/**
* Test INSERT / COPY queries
*/
const PgDriver = require('..');
const Connection = require('sqmicro-connection')(PgDriver);
// To simulate network problems use socat:
// socat TCP-LISTEN:5435,reuseaddr,fork TCP:localhost:5433
const connectionString = 'postgresql://user:passuser@localhost:5435/proto-pgs';
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().then(work);
async function work() {
console.log('Inserting 1 row');
const sql1 = 'INSERT INTO command(type, message, id) VALUES($1, $2, $3)';
const values = ['insert', 'testing insert', `${new Date().toISOString()}`];
const res1 = await connection.query(sql1, values);
console.log(`Got response: ${JSON.stringify(res1)}`);
console.log(`Copying 3 rows`);
const rows = new Set();
[1,2,3].forEach(i => {
rows.add({
type: 'copy',
message: `msg ${i}`,
id: Date.now() + i
});
});
const res2 = await connection.copy(
'command',
rows, {
fieldNames: 'type message id'.split(' '),
delimiter: '|'
});
console.log(`Got response: ${JSON.stringify(res2)}`);
await connection.disconnect();
console.log('CY later, alligator!');
}