Skip to content

iceddev/pg-transact

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg-transact

Build Status

A nicer API on node-postgres transactions

Usage

var pg = require('pg.js');
var pgTransact = require('pg-transact');

function transaction(client, cb){
  // everything in here is run as a transaction
  client.query('SELECT NOW() as when', function(err, result){
    if(err){
      // passing an error to the callback does a rollback on the transaction
      return cb(err);
    }

    // passing a `null` error and a result will resolve the transaction as the result
    cb(null, result);
  });
}

pg.connect(connectionString, function(err, client, done){
  if(err){
    throw err;
  }

  pgTransact(client, transaction, done)
    .then(console.log, console.error);
});

It also will work with a returned promise:

var pg = require('pg.js');
var pgTransact = require('pg-transact');

function transaction(client){
  return new Promise(function(resolve, reject){
    client.query('SELECT NOW() as when', function(err, result){
      if(err){
        return reject(err);
      }

      resolve(result);
    });
  });
}

pg.connect(connectionString, function(err, client, done){
  if(err){
    throw err;
  }

  pgTransact(client, transaction, done)
    .then(console.log, console.error);
});

About

A nicer API on node-postgres transactions

Resources

License

Stars

Watchers

Forks

Packages

No packages published