promised-postgres

1.0.0-1 • Public • Published

Promised Postgres

Very simple wrapper for awesome node pg module.

Installation

npm install --save promised-postgres

Usage

const Postgres = require('promised-postgres');
const ourDbInstance = new Postgres('connectionStringOrConfigObject');
var rollback; 
ourDbInstance
    .getNewClient()
    .then( function ( client) {
        rollback = client.roolback;
        return client.begin()
    })
    .then( function( client ) {
        
        return client.query('INSERT INTO something VALUES ($1) returning id',[ 'withParams' ]);
    })
    .then( function( result ) {
        
        return result.$client.query('UPDATE something_else SET a = $1',[ result.rows[0].id ])
    })
    .then( function ( result ) {
    
        //do something with result and commit
        result.$client.commit();
    })
    .then( function ( client ) {
        
        //!!dont forget release client
        client.done()
    })
    .catch(function( error) {
        
        rollback();
    });

API

Postgres

  • Postgres.constructor( connectionConfig ) : Postgres
  • Postgres.getNewClient( ) : Promise.<Client>

Client

  • Client.begin() : Promise.<Client>
  • Client.commit() : Promise.<Client>
  • Client.rollback() : Promise.<Client>
  • Client.query( sql, params ) : Promise.<Result>
  • Client.done( ) : void

Result

result is a pg result object extended with one property:

  • Result.$client : Client

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0-1
    0
    • latest

Version History

Package Sidebar

Install

npm i promised-postgres

Weekly Downloads

0

Version

1.0.0-1

License

BSD-2-Clause

Last publish

Collaborators

  • jatzek_at_netgenes