Skip to content

Commit

Permalink
Added support for nested transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 22, 2015
1 parent 4f67ddf commit 60723b4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 59 deletions.
128 changes: 70 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function dbInit(cn, options) {
// Returns a detached connection instance to allow
// chaining queries under the same connection;
dbInst.connect = function () {
var db;
var db = {};
var self = {
query: function (query, values, qrm) {
if (db) {
Expand All @@ -115,27 +115,21 @@ function dbInit(cn, options) {
} else {
throw new Error("Cannot invoke done() on a disconnected client.");
}
},
tx: function (cb) {
return $transact(self, cb);
}
};
$extendProtocol(self);
$extendProtocol(self, null, db, options);
return $connect(cn)
.then(function (obj) {
db = {
client: obj.client,
done: function () {
$notify(false, obj, options);
obj.done();
}
db.client = obj.client;
db.done = function () {
$notify(false, obj, options);
obj.done();
};
$notify(true, obj, options);
return $p.resolve(self);
});
};


// Generic query request;
// qrm is Query Result Mask, combination of queryResult flags.
dbInst.query = function (query, values, qrm) {
Expand All @@ -158,58 +152,15 @@ function dbInit(cn, options) {
});
});
};

dbInst.tx = function (cb) {
var db;

function attach(obj) {
db = obj;
$notify(true, db, options);
}

function detach() {
$notify(false, db, options);
db.done();
db = null;
}

var tx = {
query: function (query, values, qrm) {
if (!db) {
throw new Error("Unexpected call outside of transaction.");
}
return $query(db.client, query, values, qrm, options);
}
};
$extendProtocol(tx);
return $p(function (resolve, reject) {
$connect(cn)
.then(function (db) {
attach(db);
return $transact(tx, cb);
}, function (reason) {
reject(reason);
})
.then(function (data) {
detach();
resolve(data);
}, function (reason) {
detach();
reject(reason);
});
});
};

$extendProtocol(dbInst);

$extendProtocol(dbInst, cn, null, options);
return dbInst;
}

////////////////////////////////////////////////
// Global, reusable functions, all start with $

// Simpler promise instantiation;
var $p = function(func){
var $p = function (func) {
return new npm.promise(func);
};

Expand Down Expand Up @@ -474,7 +425,7 @@ function $connect(cn) {
}

// Injects additional methods into an access object.
function $extendProtocol(obj) {
function $extendProtocol(obj, cn, db, options) {

// Expects no data to be returned;
obj.none = function (query, values) {
Expand Down Expand Up @@ -518,6 +469,67 @@ function $extendProtocol(obj) {
var query = $createFuncQuery(procName, values);
return obj.query(query, null, queryResult.one | queryResult.none);
};

// transactions support;
obj.tx = function (cb) {
var txDB = {};
var internal; // internal connection flag;

function attach(obj, int) {
txDB.client = obj.client;
txDB.done = obj.done;
if (int) {
internal = true;
$notify(true, txDB, options);
}
}

function detach() {
if (internal) {
$notify(false, txDB, options);
txDB.done();
}
txDB.client = null;
}

var tx = {
query: function (query, values, qrm) {
if (!txDB.client) {
throw new Error("Unexpected call outside of transaction.");
}
return $query(txDB.client, query, values, qrm, options);
}
};
$extendProtocol(tx, null, txDB, options);
return $p(function (resolve, reject) {
if (cn) {
$connect(cn)
.then(function (obj) {
attach(obj, true);
return $transact(tx, cb);
}, function (reason) {
reject(reason);
})
.then(function (data) {
detach();
resolve(data);
}, function (reason) {
detach();
reject(reason);
});
} else {
attach(db);
return $transact(tx, cb)
.then(function (data) {
detach();
resolve(data);
}, function (reason) {
detach();
reject(reason);
});
}
});
};
}

// Implements a transaction logic;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "0.5.5",
"version": "0.6.0",
"description": "PG + Promises/A+, with transactions support.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 60723b4

Please sign in to comment.