Skip to content

Commit 7427a8e

Browse files
committed
Added check for duplicate connection name
1 parent 0f6e090 commit 7427a8e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function fastifyPostgres (fastify, options, next) {
2929
fastify.decorate('pg', {})
3030
}
3131

32+
if (fastify.pg[name]) {
33+
return next(new Error('Connection name has already been registered: ' + name))
34+
}
35+
3236
fastify.pg[name] = db
3337
} else {
3438
if (fastify.pg) {

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,21 @@ test('fastify.pg.test use native module', t => {
234234
})
235235
})
236236
})
237+
238+
test('fastify.pg.test should throw with duplicate connection names', t => {
239+
t.plan(1)
240+
241+
const fastify = Fastify()
242+
243+
fastify.register(fastifyPostgres, {
244+
name: 'test',
245+
connectionString: 'postgres://postgres@localhost/postgres'
246+
})
247+
248+
t.throws(fastify.register(fastifyPostgres, {
249+
name: 'test',
250+
connectionString: 'postgres://postgres@localhost/postgres'
251+
}),
252+
new Error('Connection name has already been registered: test'),
253+
'should throw')
254+
})

0 commit comments

Comments
 (0)