Skip to content

Commit

Permalink
refactor: manager.add now acts as a noop for existing connection
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 9613b62 commit cf4f1f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
17 changes: 4 additions & 13 deletions src/Connection/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,11 @@ export class ConnectionManager extends EventEmitter implements ConnectionManager
*/
public add (connectionName: string, config: ConnectionConfigContract): void {
/**
* Raise an exception when someone is trying to re-add the same connection. We
* should not silently avoid this scanerio, since there is a valid use case
* in which the config has been changed and someone wants to re-add the
* connection with new config. In that case, they must
*
* 1. Close and release the old connection
* 2. Then add the new connection
* Noop when connection already exists. If one wants to change the config, they
* must release the old connection and add a new one
*/
if (this.isConnected(connectionName)) {
throw new Exception(
`Attempt to add duplicate connection ${connectionName} failed`,
500,
'E_DUPLICATE_DB_CONNECTION',
)
if (this.has(connectionName)) {
return
}

this._logger.trace({ connection: connectionName }, 'adding new connection to the manager')
Expand Down
6 changes: 3 additions & 3 deletions test/connection/connection-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ test.group('ConnectionManager', (group) => {
assert.isFalse(manager.isConnected('primary'))
})

test('raise exception when attempt to add a duplication connection', async (assert) => {
test('add duplicate connection must be a noop', async (assert) => {
const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())
manager.connect('primary')

const fn = () => manager.add('primary', getConfig())
assert.throw(fn, 'E_DUPLICATE_DB_CONNECTION: Attempt to add duplicate connection primary failed')
manager.add('primary', Object.assign({}, getConfig(), { client: 'foo' }))
assert.notEqual(manager.get('primary')!.config.client, 'foo')
})

test('patch config when connection is not in open state', async (assert) => {
Expand Down

0 comments on commit cf4f1f8

Please sign in to comment.