Skip to content

Commit e0737b6

Browse files
committed
fix: load config file in static method openConnections
1 parent cfc906c commit e0737b6

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,16 @@ export default {
689689
const name = 'custom'
690690
const driver = CustomDriver
691691

692+
// Will create the driver
692693
Database.build(name, driver)
693694

694-
console.log(Database.drivers) // ['mysql', 'mongo', 'sqlite', 'mssql', 'postgres', 'custom']
695+
// List all Database drivers
696+
console.log(Database.drivers()) // ['mysql', 'mongo', 'sqlite', 'mssql', 'postgres', 'custom']
697+
698+
const onlyConnected = true
699+
700+
// List only Database drivers where there are a connection established
701+
console.log(Database.drivers(onlyConnected)) // ['postgres', 'mongo']
695702
```
696703

697704
> Now, if you have implemented your connection in config/database, you can use him inside Database
@@ -700,6 +707,8 @@ console.log(Database.drivers) // ['mysql', 'mongo', 'sqlite', 'mssql', 'postgres
700707
// Will use CustomDriver to handle the database operations and
701708
// save the shared connection in DriverFactory
702709
await database.connection('myconnection').connect()
710+
711+
console.log(Database.drivers(true)) // ['postgres', 'mongo', 'custom']
703712
```
704713

705714
---

src/Database.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export class Database implements DatabaseContract {
2727
this.driver = DriverFactory.fabricate('default', runtimeConfig)
2828
}
2929

30-
static get drivers(): string[] {
31-
return DriverFactory.availableDrivers()
30+
static drivers(onlyConnected = false): string[] {
31+
return DriverFactory.availableDrivers(onlyConnected)
3232
}
3333

3434
static build(
@@ -39,6 +39,8 @@ export class Database implements DatabaseContract {
3939
}
4040

4141
static async openConnections(...connections: string[]): Promise<void> {
42+
new Config().safeLoad(Path.config('database'))
43+
4244
const promises = connections.map(connection =>
4345
DriverFactory.generateConnectionClient(connection, {}, true),
4446
)

src/Utils/DriverFactory.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,22 @@ export class DriverFactory {
3434
.set('postgres', { Driver: PostgresDriver })
3535
.set('sqlserver', { Driver: SqlServerDriver })
3636

37-
static availableDrivers(): string[] {
38-
return [...this.drivers.keys()]
37+
static availableDrivers(onlyConnected = false): string[] {
38+
const availableDrivers = []
39+
40+
for (const [key, value] of this.drivers.entries()) {
41+
if (onlyConnected) {
42+
if (!value.clientConnection) continue
43+
44+
availableDrivers.push(key)
45+
46+
continue
47+
}
48+
49+
availableDrivers.push(key)
50+
}
51+
52+
return availableDrivers
3953
}
4054

4155
static fabricate(conName: string, runtimeConfig: any = {}): DriverContract {
@@ -86,9 +100,6 @@ export class DriverFactory {
86100
else await client.destroy()
87101

88102
driverObject.clientConnection = null
89-
driverObject.Driver = new driverObject.Driver(
90-
Config.get('database.default'),
91-
)
92103

93104
this.drivers.set(driverName, driverObject)
94105
}
@@ -101,7 +112,6 @@ export class DriverFactory {
101112
const driverObject = this.drivers.get(driverName)
102113

103114
driverObject.clientConnection = clientConnection
104-
driverObject.Driver = new driverObject.Driver(clientConnection)
105115

106116
this.drivers.set(driverName, driverObject)
107117
}

0 commit comments

Comments
 (0)