pg-transact
v1.0.1
Published
A nicer API on node-postgres transactions
Downloads
9
Readme
pg-transact
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, cb){
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);
});