Skip to content

wherrera/node-pg-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-pg-helper

common helper functions for node-postgres

Node.js Package

Examples

Setup postgres connection

const { Pool } = require('pg');

const db = require('node-pg-helper');

const pool = new Pool({
    connectionString: process.env.DATABASE_URL,
    ssl: { rejectUnauthorized: false }
});

Set the client as Pool

db.setClient(pool);

example insert

await db.insert('users', {
    'id': 100,
    'firstname': 'john',
    'lastname': 'smith'
})

example upsert

await db.upsert('users', {
    'id': 100,
    'firstname': 'john',
    'lastname': 'snow'
}, 'id');

example update

await db.update('users',{
    'lastname': 'snow'
},
{
    'id': 100
});

select all the rows in a table

let rows = await db.selectAllRows("users");

rows.forEach(row => {
    console.log(row);
});