|
| 1 | +# node-pg-pool |
| 2 | +A connection pool for node-postgres |
| 3 | + |
| 4 | +## install |
| 5 | +```sh |
| 6 | +npm i pg-pool pg |
| 7 | +``` |
| 8 | + |
| 9 | +## use |
| 10 | + |
| 11 | +to use pg-pool you must first create an instance of a pool |
| 12 | + |
| 13 | +```js |
| 14 | +//by default the pool uses the same |
| 15 | +//configuration as whatever `pg` version you have installed |
| 16 | +const pool = new Pool() |
| 17 | + |
| 18 | +//you can pass properties to the pool |
| 19 | +//these properties are passed unchanged to both the node-postgres Client constructor |
| 20 | +//and the node-pool (https://github.com/coopernurse/node-pool) constructor |
| 21 | +//allowing you to fully configure the behavior of both |
| 22 | +const pool2 = new Pool({ |
| 23 | + database: 'postgres', |
| 24 | + user: 'brianc', |
| 25 | + password: 'secret!', |
| 26 | + port: 5432, |
| 27 | + ssl: true, |
| 28 | + max: 20, //set pool max size to 20 |
| 29 | + min: 4, //set min pool size to 4 |
| 30 | + idleTimeoutMillis: 1000 //close idle clients after 1 second |
| 31 | +}) |
| 32 | + |
| 33 | +//you can supply a custom client constructor |
| 34 | +//if you want to use the native postgres client |
| 35 | +const NativeClient = require('pg').native.Client |
| 36 | +const nativePool = new Pool({ Client: NativeClient }) |
| 37 | + |
| 38 | +//you can even pool pg-native clients directly |
| 39 | +const PgNativeClient = require('pg-native') |
| 40 | +const pgNativePool = new Pool({ Client: PgNativeClient }) |
| 41 | +``` |
| 42 | + |
| 43 | +pg-pool supports a fully promise-based api for acquiring clients |
| 44 | + |
| 45 | +```js |
| 46 | +const pool = new Pool() |
| 47 | +pool.connect().then(client => { |
| 48 | + client.query('select $1::text as name', ['pg-pool']).then(res => { |
| 49 | + client.release() |
| 50 | + console.log('hello from', res.rows[0].name) |
| 51 | + }) |
| 52 | + .catch(e => { |
| 53 | + client.release() |
| 54 | + console.error('query error', e.message, e.stack) |
| 55 | + }) |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +pg-pool supports the traditional callback api for acquiring a client that node-postgres has shipped with internally for years: |
| 60 | + |
| 61 | +```js |
| 62 | +const pool = new Pool() |
| 63 | +pool.connect((err, client, done) => { |
| 64 | + if (err) return done(err) |
| 65 | + |
| 66 | + client.query('SELECT $1::text as name', ['pg-pool'], (err, res) => { |
| 67 | + done() |
| 68 | + if (err) { |
| 69 | + return console.error('query error', e.message, e.stack) |
| 70 | + } |
| 71 | + console.log('hello from', res.rows[0].name) |
| 72 | + }) |
| 73 | +}) |
| 74 | +``` |
| 75 | + |
| 76 | +When you are finished with the pool if all the clients are idle the pool will close them after `config.idleTimeoutMillis` and your app |
| 77 | +will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows: |
| 78 | + |
| 79 | +```js |
| 80 | +const pool = new Pool() |
| 81 | +const client = await pool.connect() |
| 82 | +console.log(await client.query('select now()')) |
| 83 | +client.release() |
| 84 | +await pool.end() |
| 85 | +``` |
| 86 | + |
| 87 | +## tests |
| 88 | + |
| 89 | +To run tests clone the repo, `npm i` in the working dir, and then run `npm test` |
| 90 | + |
| 91 | +## license |
| 92 | + |
| 93 | +The MIT License (MIT) |
| 94 | +Copyright (c) 2016 Brian M. Carlson |
| 95 | + |
| 96 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 97 | + |
| 98 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 99 | + |
| 100 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments