Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/with-conn-pg
Calls a function injecting a pg connection, and release it afterwards
https://github.com/mcollina/with-conn-pg
Last synced: 13 days ago
JSON representation
Calls a function injecting a pg connection, and release it afterwards
- Host: GitHub
- URL: https://github.com/mcollina/with-conn-pg
- Owner: mcollina
- License: mit
- Created: 2015-06-30T09:01:16.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-15T23:05:20.000Z (about 7 years ago)
- Last Synced: 2024-12-17T15:43:19.779Z (23 days ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# with-conn-pg [![Build Status](https://travis-ci.org/mcollina/with-conn-pg.svg)](https://travis-ci.org/mcollina/with-conn-pg)
Calls a function injecting a pg connection, and release it afterwards
## Install
```
npm install with-conn-pg --save
```*
withConnPg()
*withConn()
*withConn.end()
-------------------------------------------------------
Returns a new instance of [`withConn`](#withConn).
Config can be both an object or a string. In case of an object, check
[`pg`](https://www.npmjs.com/package/pg). The connection string is
parsed with:
[`pg-connection-string`](https://www.npmjs.com/package/pg-connection-string).-------------------------------------------------------
### withConn(func(conn, args.., done))Wraps the passed function so that the first argument is what is
returned by
[`pool.connect()`](https://www.npmjs.com/package/pg) 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](http://npm.im/fastfall).Example:
```js
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)
})
```-------------------------------------------------------
Wraps [`pool.end()`](https://www.npmjs.com/package/pg) to release the
connection pool (useful during testing).## License
MIT