2.1.0 • Published 6 years ago

with-conn-pg v2.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

with-conn-pg  Build Status

Calls a function injecting a pg connection, and release it afterwards

Install

npm install with-conn-pg --save

API

  • withConnPg()
  • withConn()
  • withConn.end()

withConnPg(config)

Returns a new instance of withConn. Config can be both an object or a string. In case of an object, check pg. The connection string is parsed with: pg-connection-string.


withConn(func(conn, args.., done))

Wraps the passed function so that the first argument is what is returned by pool.connect() and release it afterwards.

this is preserved, and any arguments will be passed through.

If multiple functions are passed, they will be wrapped in a fastfall.

Example:

var connString = 'postgres://localhost/with_conn'
var withConn = require('with-conn-pg')(connString)
var func = withConn(function (client, arg, done) {
  console.log('input is', arg)
  client.query('SELECT $1::int AS number', [arg], function (err, result) {
    done(err, result.rows[0])
  })
})

func(42, function (err, result) {
  console.log('output is', result.number)
})

withConn.end()

Wraps pool.end() to release the connection pool (useful during testing).

License

MIT