Skip to content

boxymoron/node-pg-tx

Repository files navigation

node-pg-tx

NodeJS PostgresSQL Transaction wrapper.

This is a thin wrapper around the pg module to make it easier to execute multiple SQL statements in a single transaction. It also lets you set the transaction isolation level and makes committing and rolling back easier.

Example:

	db.connect().then((ctx) =>{
		return db.beginTransaction(ctx, db.READ_COMMITTED);
	}).then((ctx) =>{
		return runQuery(ctx);
	}).then((ctx) => {
		return runUpsert(ctx);
	}).then((ctx) =>{
		return db.commit(ctx);
	}).then((ctx) =>{
		return sendResponse(ctx);
	}).catch((ctx) =>{
		var err = ctx.err;
		console.log(ctx.err);
		db.rollback(ctx);
		res.status(500).send('error: '+err);
	});

The interesting bits are in: service/database.js and routes/fib.js.

Before running, initialize the db with the DDL below and set the connection parameters to your database in service/database.js:

CREATE TABLE public.node_test
(
  count integer,
  uuid character varying(256) NOT NULL,
  CONSTRAINT "UUID_PK" PRIMARY KEY (uuid)
)

To run:

npm install
npm install supervisor -g
supervisor app.js
open http://localhost:8888/rest/fib