Skip to content

Commit d653234

Browse files
authored
Add acquire event (brianc#16)
* Add acquire event Add acquire event which fires every time a client is acquired from the pool. * Update README.md
1 parent ce173f8 commit d653234

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

README.md

+44-23
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ npm i pg-pool pg
1515
to use pg-pool you must first create an instance of a pool
1616

1717
```js
18-
const Pool = require('pg-pool')
18+
var Pool = require('pg-pool')
1919

2020
//by default the pool uses the same
2121
//configuration as whatever `pg` version you have installed
22-
const pool = new Pool()
22+
var pool = new Pool()
2323

2424
//you can pass properties to the pool
2525
//these properties are passed unchanged to both the node-postgres Client constructor
2626
//and the node-pool (https://github.com/coopernurse/node-pool) constructor
2727
//allowing you to fully configure the behavior of both
28-
const pool2 = new Pool({
28+
var pool2 = new Pool({
2929
database: 'postgres',
3030
user: 'brianc',
3131
password: 'secret!',
@@ -38,20 +38,20 @@ const pool2 = new Pool({
3838

3939
//you can supply a custom client constructor
4040
//if you want to use the native postgres client
41-
const NativeClient = require('pg').native.Client
42-
const nativePool = new Pool({ Client: NativeClient })
41+
var NativeClient = require('pg').native.Client
42+
var nativePool = new Pool({ Client: NativeClient })
4343

4444
//you can even pool pg-native clients directly
45-
const PgNativeClient = require('pg-native')
46-
const pgNativePool = new Pool({ Client: PgNativeClient })
45+
var PgNativeClient = require('pg-native')
46+
var pgNativePool = new Pool({ Client: PgNativeClient })
4747
```
4848

4949
### acquire clients with a promise
5050

5151
pg-pool supports a fully promise-based api for acquiring clients
5252

5353
```js
54-
const pool = new Pool()
54+
var pool = new Pool()
5555
pool.connect().then(client => {
5656
client.query('select $1::text as name', ['pg-pool']).then(res => {
5757
client.release()
@@ -71,10 +71,10 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o
7171
```js
7272
// with async/await
7373
(async () => {
74-
const pool = new Pool()
75-
const client = await pool.connect()
74+
var pool = new Pool()
75+
var client = await pool.connect()
7676
try {
77-
const result = await client.query('select $1::text as name', ['brianc'])
77+
var result = await client.query('select $1::text as name', ['brianc'])
7878
console.log('hello from', result.rows[0])
7979
} finally {
8080
client.release()
@@ -83,9 +83,9 @@ this ends up looking much nicer if you're using [co](https://github.com/tj/co) o
8383

8484
// with co
8585
co(function * () {
86-
const client = yield pool.connect()
86+
var client = yield pool.connect()
8787
try {
88-
const result = yield client.query('select $1::text as name', ['brianc'])
88+
var result = yield client.query('select $1::text as name', ['brianc'])
8989
console.log('hello from', result.rows[0])
9090
} finally {
9191
client.release()
@@ -98,16 +98,16 @@ co(function * () {
9898
because its so common to just run a query and return the client to the pool afterward pg-pool has this built-in:
9999

100100
```js
101-
const pool = new Pool()
102-
const time = await pool.query('SELECT NOW()')
103-
const name = await pool.query('select $1::text as name', ['brianc'])
101+
var pool = new Pool()
102+
var time = await pool.query('SELECT NOW()')
103+
var name = await pool.query('select $1::text as name', ['brianc'])
104104
console.log(name.rows[0].name, 'says hello at', time.rows[0].name)
105105
```
106106

107107
you can also use a callback here if you'd like:
108108

109109
```js
110-
const pool = new Pool()
110+
var pool = new Pool()
111111
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
112112
console.log(res.rows[0].name) // brianc
113113
})
@@ -123,7 +123,7 @@ clients back to the pool after the query is done.
123123
pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years:
124124

125125
```js
126-
const pool = new Pool()
126+
var pool = new Pool()
127127
pool.connect((err, client, done) => {
128128
if (err) return done(err)
129129

@@ -143,8 +143,8 @@ When you are finished with the pool if all the clients are idle the pool will cl
143143
will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows:
144144

145145
```js
146-
const pool = new Pool()
147-
const client = await pool.connect()
146+
var pool = new Pool()
147+
var client = await pool.connect()
148148
console.log(await client.query('select now()'))
149149
client.release()
150150
await pool.end()
@@ -159,7 +159,7 @@ The pool should be a __long-lived object__ in your application. Generally you'l
159159
160160
// correct usage: create the pool and let it live
161161
// 'globally' here, controlling access to it through exported methods
162-
const pool = new pg.Pool()
162+
var pool = new pg.Pool()
163163
164164
// this is the right way to export the query method
165165
module.exports.query = (text, values) => {
@@ -173,7 +173,7 @@ module.exports.connect = () => {
173173
// every time we called 'connect' to get a new client?
174174
// that's a bad thing & results in creating an unbounded
175175
// number of pools & therefore connections
176-
const aPool = new pg.Pool()
176+
var aPool = new pg.Pool()
177177
return aPool.connect()
178178
}
179179
```
@@ -228,7 +228,28 @@ pool
228228

229229
```
230230

231-
This allows you to do custom bootstrapping and manipulation of clients after they have been successfully connected to the PostgreSQL backend, but before any queries have been issued.
231+
#### acquire
232+
233+
Fired whenever the a client is acquired from the pool
234+
235+
Example:
236+
237+
This allows you to count the number of clients which have ever been acquired from the pool.
238+
239+
```js
240+
var Pool = require('pg-pool')
241+
var pool = new Pool()
242+
243+
var acquireCount = 0
244+
pool.on('acquire', function (client) {
245+
console.log('acquired', (++acquireCount), 'clients')
246+
})
247+
248+
for (var i = 0; i < 200; i++) {
249+
pool.query('SELECT NOW()')
250+
}
251+
252+
```
232253

233254
### environment variables
234255

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Pool.prototype.connect = function (cb) {
6060
}
6161

6262
this.log('acquire client')
63+
this.emit('acquire', client)
6364

6465
client.release = function (err) {
6566
delete client.release

test/events.js

+21
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,25 @@ describe('events', function () {
2121
done()
2222
})
2323
})
24+
25+
it('emits acquire every time a client is acquired', function (done) {
26+
var pool = new Pool()
27+
var acquireCount = 0
28+
pool.on('acquire', function (client) {
29+
expect(client).to.be.ok()
30+
acquireCount++
31+
})
32+
for (var i = 0; i < 10; i++) {
33+
pool.connect(function (err, client, release) {
34+
err ? done(err) : release()
35+
release()
36+
if (err) return done(err)
37+
})
38+
pool.query('SELECT now()')
39+
}
40+
setTimeout(function () {
41+
expect(acquireCount).to.be(20)
42+
pool.end(done)
43+
}, 40)
44+
})
2445
})

0 commit comments

Comments
 (0)