Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Asynchronously run node-postgres queries in a postgres transaction, rolling back on error

License

Notifications You must be signed in to change notification settings

dcousens/pg-async-transaction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg-async-transaction

Trivially wraps BEGIN and COMMIT to provide behind-the-scenes transaction support for your POSTGRES queries.

If callback(err) happens, the wrapper calls ROLLBACK and upon success returns the err object.

If ROLLBACK errors, preference is given to the ROLLBACK error in the spot of err.

Example

const pg = require('pg')
const pat = require('pg-async-transaction')
const parallel = require('run-parallel')

function foo (done) {
  pg.connect(process.env.POSTGRES, (err, client, free) => {
    if (err) return done(err)

    pat(client, (callback) => {
      parallel([
        (next) => client.query('INSERT ...', next),
        (next) => client.query('INSERT ...', next),
        (next) => client.query('INSERT ...', next)
      ], callback)
    }, (err, results) => {
      if (err) free()

      free()
      done(err)
    })
  })
}

// ...

LICENSE MIT

About

Asynchronously run node-postgres queries in a postgres transaction, rolling back on error

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published