From bb2a84a01ddaf4d0632a0ea8c83475092de4e15e Mon Sep 17 00:00:00 2001 From: delmas_s Date: Sun, 4 Sep 2016 03:50:39 +0200 Subject: [PATCH 1/3] update for new pool connection handler --- lib/plugin.js | 67 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 5ce316d..cc81a03 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -2,30 +2,61 @@ var debug = require("debug")("orm:transaction"); module.exports = Plugin; -function Plugin(db) { +function Plugin(db, opt) { db.transaction = function (cb) { - db.driver.execQuery("BEGIN", function (err) { - if (err) { - return cb(err); - } + if (db.driver.opts.pool) + db.driver.createPool(function (err, id) { + if (err) + return cb(err); + db.driver.execQuery("BEGIN", id, function (err) { + if (err) { + return cb(err); + } - debug("init transaction!"); + debug("init transaction!"); - return cb(null, { - commit: function (cb) { - db.driver.execQuery("COMMIT", function (err) { - debug("transaction committed!"); - return cb(err || null); - }); - }, - rollback: function (cb) { - db.driver.execQuery("ROLLBACK", function (err) { - debug("transaction rolled back!"); - return cb(err || null); + return cb(null, { + id: id, + commit: function (cb) { + db.driver.execQuery("COMMIT", id, function (err) { + debug("transaction committed!"); + db.driver.releasePool(id); + return cb(err || null); + }); + }, + rollback: function (cb) { + db.driver.execQuery("ROLLBACK", id, function (err) { + debug("transaction rolled back!"); + db.driver.releasePool(id); + return cb(err || null); + }); + } }); + }); + }); + else + db.driver.execQuery("BEGIN", function (err) { + if (err) { + return cb(err); } + + debug("init transaction!"); + + return cb(null, { + commit: function (cb) { + db.driver.execQuery("COMMIT", function (err) { + debug("transaction committed!"); + return cb(err || null); + }); + }, + rollback: function (cb) { + db.driver.execQuery("ROLLBACK", function (err) { + debug("transaction rolled back!"); + return cb(err || null); + }); + } + }); }); - }); }; return { From e5d4b74f4bb52a4504f8dcb7526d3b6026e82265 Mon Sep 17 00:00:00 2001 From: delmas_s Date: Sun, 4 Sep 2016 04:03:35 +0200 Subject: [PATCH 2/3] remove unused code --- lib/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugin.js b/lib/plugin.js index cc81a03..12ebb23 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -2,7 +2,7 @@ var debug = require("debug")("orm:transaction"); module.exports = Plugin; -function Plugin(db, opt) { +function Plugin(db) { db.transaction = function (cb) { if (db.driver.opts.pool) db.driver.createPool(function (err, id) { From 7f4d4c8d0484c690cf22110aaa81c05f30684ec3 Mon Sep 17 00:00:00 2001 From: delmas_s Date: Sun, 4 Sep 2016 12:09:36 +0200 Subject: [PATCH 3/3] update README.md with pool example --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f8afa6f..afedb9e 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,43 @@ db.transaction(function (err, transaction) { }); ``` +## Pool Example + +```js +var orm = require("orm"); +var transaction = require("orm-transaction"); + +orm.connect("mysql://username:password@host/database", function (err, db) { + if (err) throw err; + + db.use(transaction); + + var Person = db.define("person", { + name : String, + surname : String, + age : Number + }); + + db.transaction(function (err, t) { + + Person.create({name: "John"}, t.id, function(err, result) { + + Person.find({ surname: "Doe" }).each(function (person) { + person.useConnection(t.id).remove(function () { + t.commit(function (err) { + if (!err) { + console.log("success!"); + } + }); + }); + }); + + }); + + }); +}); +``` + ## Example ```js @@ -48,14 +85,15 @@ orm.connect("mysql://username:password@host/database", function (err, db) { db.transaction(function (err, t) { Person.find({ surname: "Doe" }).each(function (person) { - person.remove(); + person.remove(function () { + t.commit(function (err) { + if (!err) { + console.log("success!"); + } + }); + }); }); - t.commit(function (err) { - if (!err) { - console.log("success!"); - } - }); }); }); ```