with-conn-pg

2.1.0 • Public • Published

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(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

Package Sidebar

Install

npm i with-conn-pg

Weekly Downloads

1

Version

2.1.0

License

MIT

Last publish

Collaborators

  • matteo.collina