Code Monkey home page Code Monkey logo

node-pg-transaction's Introduction

Build Status

Make it easier to write transactions for PostgreSQL using node-postgres.

The callback and event emitter styles both work.
Note: if you use a callback, then the error event won't be emitted. This is consistent with node-postgres.

##Installation:

To install the most recent release from npm, run:

npm install pg-transaction

Methods:

begin([callback]);
query(); // This is pg.Client.query from node-postgres. There are various possible arguments look at its documentation
savepoint(savepoint, [callback]);
release(savepoint, [callback]);
rollback([savepoint], [callback]);
commit([callback]);
abort([callback]);

Events:

  • error

Example:

/**
 * Module dependencies
 */

var
  // PostgreSQL modules
  pg = require('pg')
, Transaction = require('pg-transaction')

  // Configuration stuff
, connectionString = process.env['PG_CON'] || ''
;

var die = function(err){
  if (err) throw err;
};

var client = new pg.Client(connectionString);
client.connect();

client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");

var tx = new Transaction(client);
tx.on('error', die);

tx.begin();
tx.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);
tx.savepoint('savepoint1');
tx.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);
tx.rollback('savepoint1'); // all statements after savepoint1 are undone (John will not be inserted)
tx.release('savepoint1'); // can no longer use savepoint1 as a point to rollback to
tx.commit();

client.query("SELECT COUNT(*) AS count FROM beatles", function(err, result){
  if (err) return die(err);
  console.log(result.rows[0].count); // 1
  client.end();
});

Contributors:

Special thanks to the following:

node-pg-transaction's People

Contributors

lalitkapoor avatar brianc avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.