Skip to content

goodybag/node-pg-transaction

Repository files navigation

Build Status Dependency Status

NPM

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([mode], [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:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 5

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy