项目作者: dcousens

项目描述 :
Asynchronously run node-postgres queries in a postgres transaction, rolling back on error
高级语言: JavaScript
项目地址: git://github.com/dcousens/pg-async-transaction.git
创建时间: 2016-09-15T03:40:54Z
项目社区:https://github.com/dcousens/pg-async-transaction

开源协议:MIT License

下载


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

  1. const pg = require('pg')
  2. const pat = require('pg-async-transaction')
  3. const parallel = require('run-parallel')
  4. function foo (done) {
  5. pg.connect(process.env.POSTGRES, (err, client, free) => {
  6. if (err) return done(err)
  7. pat(client, (callback) => {
  8. parallel([
  9. (next) => client.query('INSERT ...', next),
  10. (next) => client.query('INSERT ...', next),
  11. (next) => client.query('INSERT ...', next)
  12. ], callback)
  13. }, (err, results) => {
  14. if (err) free()
  15. free()
  16. done(err)
  17. })
  18. })
  19. }
  20. // ...

LICENSE MIT