@hafdon/pghelper
v2.1.1
Published
wrapper on pg to help with managing postgres pools and multiple configs
Readme
pghelper
wrapper on pg to help with managing postgres pools and multiple configs
install
npm i @hafdon/pghelper
use
const { poolmanager } = require('pghelper');
const { configure, cxn } = poolmanager;
const db = 'your_chosen_name_for_connection_1';
// rename the db prop `configOptions`
const { [db]: configOptions } = require('./pgconnection_examples.config.js');
const log = require('debug')('pghelper');
async function main() {
await configure(db, configOptions);
let results = await cxn({
text: `select * from bird_table limit 1;`, // required
values: [], // optional
db, // required
});
if (results._error) {
// TODO: handle error
log('TODO: handle error');
}
log(results);
/* pghelper:
{
_error: false,
error: null,
rows: [
{
id: 1,
name: 'Finch',
flying_ability: 17
}
],
length: 1,
response : Object [Postgres Response Object]
}
*/
}
main();api
configure
- synchronous function
proper type validation for config files
// pgconnection_examples.config.js
module.exports = {
your_chosen_name_for_connection_1: [
{
protocol: 'postgres',
host: 'host.com',
user: 'user1',
password: 'password1',
database: 'db',
schema: 'public',
search_path: 'public',
ssl: true,
port: 1032,
max: 5,
},
{
protocol: 'postgres',
host: 'host.com',
user: 'user2',
password: 'password2',
database: 'db',
schema: 'public',
search_path: 'public',
ssl: true,
port: 1032,
max: 5,
},
],
your_chosen_name_for_connection_2: [
{
protocol: 'postgres',
host: 'another.host.com',
user: 'user3',
password: 'password3',
database: 'db',
schema: 'public',
search_path: 'public',
ssl: true,
port: 1032,
max: 5,
},
],
};