Skip to content

Instantly share code, notes, and snippets.

@paolochiodi
Last active December 22, 2016 08:26
Show Gist options
  • Save paolochiodi/c4bef568a60684c44e6b7656c409ae7a to your computer and use it in GitHub Desktop.
Save paolochiodi/c4bef568a60684c44e6b7656c409ae7a to your computer and use it in GitHub Desktop.
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