Skip to content

Commit 67308cc

Browse files
committed
feature: multiple connections
1 parent d027600 commit 67308cc

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@ function fastifyPostgres (fastify, options, next) {
1313
}
1414
}
1515

16+
const name = options.name
17+
delete options.name
18+
1619
const pool = new pg.Pool(options)
1720

18-
fastify.decorate('pg', {
21+
if (!fastify.pg) {
22+
fastify.decorate('pg', {});
23+
}
24+
25+
fastify.pg[name] = {
1926
connect: pool.connect.bind(pool),
2027
pool: pool,
2128
Client: pg.Client,
2229
query: pool.query.bind(pool)
23-
})
30+
};
2431

25-
fastify.addHook('onClose', onClose)
32+
fastify.addHook('onClose', (fastify, done) => pool.end(done))
2633

2734
next()
2835
}
2936

30-
function onClose (fastify, done) {
31-
fastify.pg.pool.end(done)
32-
}
33-
3437
module.exports = fp(fastifyPostgres, '>=0.13.1')

test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ const Fastify = require('fastify')
66
const fastifyPostgres = require('./index')
77

88
test('fastify.pg namespace should exist', t => {
9-
t.plan(5)
9+
t.plan(6)
1010

1111
const fastify = Fastify()
1212

1313
fastify.register(fastifyPostgres, {
14+
name: 'test',
1415
connectionString: 'postgres://postgres@localhost/postgres'
1516
})
1617

1718
fastify.ready(err => {
1819
t.error(err)
1920
t.ok(fastify.pg)
20-
t.ok(fastify.pg.connect)
21-
t.ok(fastify.pg.pool)
22-
t.ok(fastify.pg.Client)
21+
t.ok(fastify.pg.test)
22+
t.ok(fastify.pg.test.connect)
23+
t.ok(fastify.pg.test.pool)
24+
t.ok(fastify.pg.test.Client)
2325
fastify.close()
2426
})
2527
})
@@ -30,12 +32,13 @@ test('should be able to connect and perform a query', t => {
3032
const fastify = Fastify()
3133

3234
fastify.register(fastifyPostgres, {
35+
name: 'test',
3336
connectionString: 'postgres://postgres@localhost/postgres'
3437
})
3538

3639
fastify.ready(err => {
3740
t.error(err)
38-
fastify.pg.connect(onConnect)
41+
fastify.pg.test.connect(onConnect)
3942
})
4043

4144
function onConnect (err, client, done) {
@@ -55,12 +58,13 @@ test('use query util', t => {
5558
const fastify = Fastify()
5659

5760
fastify.register(fastifyPostgres, {
61+
name: 'test',
5862
connectionString: 'postgres://postgres@localhost/postgres'
5963
})
6064

6165
fastify.ready(err => {
6266
t.error(err)
63-
fastify.pg.query('SELECT NOW()', (err, result) => {
67+
fastify.pg.test.query('SELECT NOW()', (err, result) => {
6468
t.error(err)
6569
t.ok(result.rows)
6670
fastify.close()
@@ -74,12 +78,13 @@ test('use query util with promises', t => {
7478
const fastify = Fastify()
7579

7680
fastify.register(fastifyPostgres, {
81+
name: 'test',
7782
connectionString: 'postgres://postgres@localhost/postgres'
7883
})
7984

8085
fastify.ready(err => {
8186
t.error(err)
82-
fastify.pg
87+
fastify.pg.test
8388
.query('SELECT NOW()')
8489
.then(result => {
8590
t.ok(result.rows)
@@ -98,13 +103,14 @@ test('use native module', t => {
98103
const fastify = Fastify()
99104

100105
fastify.register(fastifyPostgres, {
106+
name: 'test',
101107
connectionString: 'postgres://postgres@localhost/postgres',
102108
native: true
103109
})
104110

105111
fastify.ready(err => {
106112
t.error(err)
107-
fastify.pg
113+
fastify.pg.test
108114
.query('SELECT 1 AS one')
109115
.then(result => {
110116
t.ok(result.rows[0].one === 1)

0 commit comments

Comments
 (0)