From 185cbbeedf55f3abd95d686357805dc2fd174f15 Mon Sep 17 00:00:00 2001 From: cemremengu Date: Wed, 5 Sep 2018 23:42:51 +0300 Subject: [PATCH] Added check for duplicate connection name --- index.js | 4 ++++ test.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/index.js b/index.js index 3bbc468..001dbcb 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,10 @@ function fastifyPostgres (fastify, options, next) { fastify.decorate('pg', {}) } + if (fastify.pg[name]) { + return next(new Error('Connection name has already been registered: ' + name)) + } + fastify.pg[name] = db } else { if (fastify.pg) { diff --git a/test.js b/test.js index d0f8b5d..52c1d6c 100644 --- a/test.js +++ b/test.js @@ -234,3 +234,22 @@ test('fastify.pg.test use native module', t => { }) }) }) + +test('fastify.pg.test should throw with duplicate connection names', t => { + t.plan(1) + + const fastify = Fastify() + + fastify.register(fastifyPostgres, { + name: 'test', + connectionString: 'postgres://postgres@localhost/postgres' + }) + .register(fastifyPostgres, { + name: 'test', + connectionString: 'postgres://postgres@localhost/postgres' + }) + + fastify.ready(err => { + t.is(err.message, 'Connection name has already been registered: test') + }) +})