Skip to content

Commit

Permalink
fix: use for of loop to iterate over registered connections
Browse files Browse the repository at this point in the history
FIXES: #536
  • Loading branch information
thetutlage committed Apr 30, 2020
1 parent d39424a commit bfdf53a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { ProfilerContract } from '@ioc:Adonis/Core/Profiler'

import {
DatabaseConfig,
ConnectionNode,
DatabaseContract,
DatabaseClientOptions,
TransactionClientContract,
Expand Down Expand Up @@ -83,10 +82,12 @@ export class Database implements DatabaseContract {
* health checker on demand.
*/
private findIfHealthChecksAreEnabled () {
this.hasHealthChecksEnabled = !!Object.keys(this.manager.connections).find((key) => {
const connection = this.manager.connections[key] as ConnectionNode
return !!connection.config.healthCheck
})
for (let [,conn] of this.manager.connections) {
if (conn.config.healthCheck) {
this.hasHealthChecksEnabled = true
break
}
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/database/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ test.group('Database', (group) => {

await db.manager.closeAll()
})

test(
'set hasHealthChecks enabled flag to true, when one ore more connections are using health checks',
async (assert) => {
const config = {
connection: 'primary',
connections: { primary: Object.assign({}, getConfig(), { healthCheck: true }) },
}

const db = new Database(config, getLogger(), getProfiler(), getEmitter())
assert.isTrue(db.hasHealthChecksEnabled)
await db.manager.closeAll()
}
)
})

test.group('Database | extend', (group) => {
Expand Down

0 comments on commit bfdf53a

Please sign in to comment.