Skip to content

Commit 530c555

Browse files
authored
Merge pull request #11 from wedgwood/features/multi-connections
feature: multiple connections
2 parents d027600 + 9bece86 commit 530c555

File tree

2 files changed

+138
-8
lines changed

2 files changed

+138
-8
lines changed

index.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,34 @@ function fastifyPostgres (fastify, options, next) {
1313
}
1414
}
1515

16-
const pool = new pg.Pool(options)
16+
const name = options.name
17+
delete options.name
1718

18-
fastify.decorate('pg', {
19+
const pool = new pg.Pool(options)
20+
const db = {
1921
connect: pool.connect.bind(pool),
2022
pool: pool,
2123
Client: pg.Client,
2224
query: pool.query.bind(pool)
23-
})
25+
}
2426

25-
fastify.addHook('onClose', onClose)
27+
if (name) {
28+
if (!fastify.pg) {
29+
fastify.decorate('pg', {})
30+
}
2631

27-
next()
28-
}
32+
fastify.pg[name] = db
33+
} else {
34+
if (fastify.pg) {
35+
next(new Error('fastify-postgres has already registered'))
36+
} else {
37+
fastify.pg = db
38+
}
39+
}
2940

30-
function onClose (fastify, done) {
31-
fastify.pg.pool.end(done)
41+
fastify.addHook('onClose', (fastify, done) => pool.end(done))
42+
43+
next()
3244
}
3345

3446
module.exports = fp(fastifyPostgres, '>=0.13.1')

test.js

+118
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,121 @@ test('use native module', t => {
116116
})
117117
})
118118
})
119+
120+
test('fastify.pg.test namespace should exist', t => {
121+
t.plan(6)
122+
123+
const fastify = Fastify()
124+
125+
fastify.register(fastifyPostgres, {
126+
name: 'test',
127+
connectionString: 'postgres://postgres@localhost/postgres'
128+
})
129+
130+
fastify.ready(err => {
131+
t.error(err)
132+
t.ok(fastify.pg)
133+
t.ok(fastify.pg.test)
134+
t.ok(fastify.pg.test.connect)
135+
t.ok(fastify.pg.test.pool)
136+
t.ok(fastify.pg.test.Client)
137+
fastify.close()
138+
})
139+
})
140+
141+
test('fastify.pg.test should be able to connect and perform a query', t => {
142+
t.plan(4)
143+
144+
const fastify = Fastify()
145+
146+
fastify.register(fastifyPostgres, {
147+
name: 'test',
148+
connectionString: 'postgres://postgres@localhost/postgres'
149+
})
150+
151+
fastify.ready(err => {
152+
t.error(err)
153+
fastify.pg.test.connect(onConnect)
154+
})
155+
156+
function onConnect (err, client, done) {
157+
t.error(err)
158+
client.query('SELECT NOW()', (err, result) => {
159+
done()
160+
t.error(err)
161+
t.ok(result.rows)
162+
fastify.close()
163+
})
164+
}
165+
})
166+
167+
test('fastify.pg.test use query util', t => {
168+
t.plan(3)
169+
170+
const fastify = Fastify()
171+
172+
fastify.register(fastifyPostgres, {
173+
name: 'test',
174+
connectionString: 'postgres://postgres@localhost/postgres'
175+
})
176+
177+
fastify.ready(err => {
178+
t.error(err)
179+
fastify.pg.test.query('SELECT NOW()', (err, result) => {
180+
t.error(err)
181+
t.ok(result.rows)
182+
fastify.close()
183+
})
184+
})
185+
})
186+
187+
test('fastify.pg.test use query util with promises', t => {
188+
t.plan(2)
189+
190+
const fastify = Fastify()
191+
192+
fastify.register(fastifyPostgres, {
193+
name: 'test',
194+
connectionString: 'postgres://postgres@localhost/postgres'
195+
})
196+
197+
fastify.ready(err => {
198+
t.error(err)
199+
fastify.pg.test
200+
.query('SELECT NOW()')
201+
.then(result => {
202+
t.ok(result.rows)
203+
fastify.close()
204+
})
205+
.catch(err => {
206+
t.fail(err)
207+
fastify.close()
208+
})
209+
})
210+
})
211+
212+
test('fastify.pg.test use native module', t => {
213+
t.plan(2)
214+
215+
const fastify = Fastify()
216+
217+
fastify.register(fastifyPostgres, {
218+
name: 'test',
219+
connectionString: 'postgres://postgres@localhost/postgres',
220+
native: true
221+
})
222+
223+
fastify.ready(err => {
224+
t.error(err)
225+
fastify.pg.test
226+
.query('SELECT 1 AS one')
227+
.then(result => {
228+
t.ok(result.rows[0].one === 1)
229+
fastify.close()
230+
})
231+
.catch(err => {
232+
t.fail(err)
233+
fastify.close()
234+
})
235+
})
236+
})

0 commit comments

Comments
 (0)