77 * file that was distributed with this source code.
88 */
99
10- import {
11- InternalServerException ,
12- NotImplementedException ,
13- } from '@secjs/exceptions'
14-
15- import { Drivers } from './Drivers/Drivers'
1610import { JoinType } from './Contracts/JoinType'
17- import { Config , PaginatedResponse , Path } from '@secjs/utils '
11+ import { DriverFactory } from './Utils/DriverFactory '
1812import { DriverContract } from './Contracts/DriverContract'
13+ import { Config , PaginatedResponse , Path } from '@secjs/utils'
1914import { DatabaseContract } from './Contracts/DatabaseContract'
2015import { TransactionContract } from './Contracts/TransactionContract'
2116
@@ -24,71 +19,57 @@ export class Database implements DatabaseContract {
2419 private connectionName : string
2520 private driver : DriverContract
2621
27- static build (
28- name : string ,
29- driver : new ( connection : string , configs ?: any ) => DriverContract ,
30- ) {
31- if ( Drivers [ name ] )
32- throw new InternalServerException ( `Driver ${ name } already exists` )
22+ constructor ( runtimeConfig : any = { } ) {
23+ new Config ( ) . safeLoad ( Path . config ( 'database' ) )
3324
34- Drivers [ name ] = driver
25+ this . runtimeConfig = runtimeConfig
26+ this . connectionName = 'default'
27+ this . driver = DriverFactory . fabricate ( 'default' , runtimeConfig )
3528 }
3629
3730 static get drivers ( ) : string [ ] {
38- return Object . keys ( Drivers )
31+ return DriverFactory . availableDrivers ( )
3932 }
4033
41- private createDriverInstance ( connectionName ?: string ) {
42- connectionName = connectionName || Config . get ( 'database.default' )
34+ static build (
35+ name : string ,
36+ driver : new ( connection : string , configs ?: any ) => DriverContract ,
37+ ) {
38+ DriverFactory . createDriver ( name , driver )
39+ }
4340
44- const connectionConfig = Config . get (
45- `database.connections.${ connectionName } ` ,
41+ static async closeDriver ( ...drivers : string [ ] ) : Promise < void > {
42+ const promises = drivers . map ( driver =>
43+ DriverFactory . closeDriverConnection ( driver ) ,
4644 )
4745
48- if ( ! connectionConfig ) {
49- throw new NotImplementedException (
50- `Connection ${ connectionName } is not configured inside database.connections object from config/database file` ,
51- )
52- }
53-
54- if ( ! Drivers [ connectionConfig . driver ] ) {
55- throw new NotImplementedException (
56- `Driver ${ connectionConfig . driver } does not exist, use Database.build method to create a new driver` ,
57- )
58- }
59-
60- this . connectionName = connectionName
61-
62- return new Drivers [ connectionConfig . driver ] (
63- connectionName ,
64- this . runtimeConfig ,
65- )
46+ await Promise . all ( promises )
6647 }
6748
68- constructor ( runtimeConfig : any = { } ) {
69- new Config ( ) . safeLoad ( Path . config ( 'database' ) )
70-
71- this . runtimeConfig = runtimeConfig
72- this . driver = this . createDriverInstance ( )
49+ static async closeAllDrivers ( ) : Promise < void > {
50+ return DriverFactory . closeAllDriversConnection ( )
7351 }
7452
53+ // DriverContract Methods
54+
7555 connection ( connection : string , runtimeConfig : any = { } ) : DatabaseContract {
7656 this . runtimeConfig = runtimeConfig
7757
78- this . driver . close ( )
79- this . driver = this . createDriverInstance ( connection )
58+ this . connectionName = connection
59+ this . driver = DriverFactory . fabricate ( connection , runtimeConfig )
8060
8161 return this
8262 }
8363
84- // DriverContract Methods
85-
8664 setQueryBuilder ( query : any ) : void {
8765 this . driver . setQueryBuilder ( query )
8866 }
8967
90- async connect ( ) : Promise < DatabaseContract > {
91- await this . driver . connect ( )
68+ async connect (
69+ force ?: boolean ,
70+ saveOnDriver ?: boolean ,
71+ ) : Promise < DatabaseContract > {
72+ await this . driver . connect ( force , saveOnDriver )
9273
9374 return this
9475 }
@@ -101,10 +82,10 @@ export class Database implements DatabaseContract {
10182 return this . driver . cloneQuery ( )
10283 }
10384
104- async clone ( ) : Promise < DatabaseContract > {
105- const database : any = await new Database ( )
106- . connection ( this . connectionName )
107- . connect ( )
85+ clone ( connection ?: string ) : DatabaseContract {
86+ const database : any = new Database ( ) . connection (
87+ connection || this . connectionName ,
88+ )
10889
10990 database . setQueryBuilder ( this . cloneQuery ( ) )
11091
0 commit comments