Last active
December 22, 2016 08:26
-
-
Save paolochiodi/c4bef568a60684c44e6b7656c409ae7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function db (opts) { | |
const pool = new pg.Pool(config.get('pgdb')) | |
this.query = function query (...args) { | |
pool.query(...args) | |
} | |
this.withTransaction = function withTransaction (tasks, done) { | |
const job = { | |
client: pool, | |
tasks: tasks | |
} | |
async.applyEachSeries([ | |
connect, | |
beginTransaction, | |
buildTransactionClient | |
runTasks, | |
commitTransaction | |
], job, (err, res) => { | |
if (err) return rollbackTransaction(job, err, done) | |
done(null, job) | |
}) | |
} | |
} | |
function transactionClient (client) { | |
this.query = function query (...args) { | |
client.query(...args) | |
} | |
this.withTransaction = function withTransaction(tasks, done) { | |
const job = { | |
client: pool, | |
tasks: tasks | |
} | |
runTasks(job, done) | |
} | |
} | |
function buildTransactionClient (job, next) { | |
job.client = new transactionClient(job.client) | |
next() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment