Skip to content

Commit 3ad877c

Browse files
committed
feat: create openConnections static method
1 parent d3dc471 commit 3ad877c

2 files changed

Lines changed: 44 additions & 15 deletions

File tree

src/Database.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export class Database implements DatabaseContract {
3838
DriverFactory.createDriver(name, driver)
3939
}
4040

41+
static async openConnections(...connections: string[]): Promise<void> {
42+
const promises = connections.map(connection =>
43+
DriverFactory.generateConnectionClient(connection, {}, true),
44+
)
45+
46+
await Promise.all(promises)
47+
}
48+
4149
static async closeDriver(...drivers: string[]): Promise<void> {
4250
const promises = drivers.map(driver =>
4351
DriverFactory.closeDriverConnection(driver),

src/Utils/DriverFactory.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,7 @@ export class DriverFactory {
3939
}
4040

4141
static fabricate(conName: string, runtimeConfig: any = {}): DriverContract {
42-
if (conName === 'default') conName = Config.get('database.default')
43-
44-
const conConfig = Config.get(`database.connections.${conName}`)
45-
46-
if (!conConfig) {
47-
throw new NotImplementedException(
48-
`Connection ${conName} is not configured inside database.connections object from config/database file`,
49-
)
50-
}
51-
52-
if (!this.drivers.has(conConfig.driver)) {
53-
throw new NotImplementedException(
54-
`Driver ${conConfig.driver} does not exist, use Database.build method to create a new driver`,
55-
)
56-
}
42+
const conConfig = this.getConnectionConfig(conName)
5743

5844
const { Driver, clientConnection } = this.drivers.get(conConfig.driver)
5945

@@ -143,4 +129,39 @@ export class DriverFactory {
143129

144130
return client
145131
}
132+
133+
static async generateConnectionClient(
134+
conName?: string,
135+
configs: any = {},
136+
saveOnDriver = true,
137+
) {
138+
const conConfig = this.getConnectionConfig(conName)
139+
140+
await this.generateDriverClient(
141+
conConfig.driver,
142+
conName,
143+
configs,
144+
saveOnDriver,
145+
)
146+
}
147+
148+
private static getConnectionConfig(conName: string) {
149+
if (conName === 'default') conName = Config.get('database.default')
150+
151+
const conConfig = Config.get(`database.connections.${conName}`)
152+
153+
if (!conConfig) {
154+
throw new NotImplementedException(
155+
`Connection ${conName} is not configured inside database.connections object from config/database file`,
156+
)
157+
}
158+
159+
if (!this.drivers.has(conConfig.driver)) {
160+
throw new NotImplementedException(
161+
`Driver ${conConfig.driver} does not exist, use Database.build method to create a new driver`,
162+
)
163+
}
164+
165+
return conConfig
166+
}
146167
}

0 commit comments

Comments
 (0)