Skip to content

Commit 5a7a3dd

Browse files
authored
Merge pull request #23 from cemremengu/patch-1
Check duplicate connection names
2 parents 0f6e090 + 185cbbe commit 5a7a3dd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,22 @@ 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+
.register(fastifyPostgres, {
248+
name: 'test',
249+
connectionString: 'postgres://postgres@localhost/postgres'
250+
})
251+
252+
fastify.ready(err => {
253+
t.is(err.message, 'Connection name has already been registered: test')
254+
})
255+
})

0 commit comments

Comments
 (0)