pg-transact
A nicer API on node-postgres transactions
Last updated 6 years ago by phated .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install pg-transact 
SYNC missed versions from official npm registry.

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, 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);
});

Current Tags

  • 1.0.1                                ...           latest (6 years ago)

2 Versions

  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 1
Dependencies (1)
Dev Dependencies (3)

Copyright 2014 - 2017 © taobao.org |