3
3
Connection as TypeORMConnection ,
4
4
ConnectionOptions as TypeORMConnectionOptions ,
5
5
createConnection as TypeORMCreateConnection ,
6
+ getConnection as TypeORMGetConnection ,
6
7
} from 'typeorm'
7
8
import { printError } from './utils/log.util'
8
9
@@ -32,6 +33,7 @@ if ((global as any)[KEY] === undefined) {
32
33
configureOption : defaultConfigureOption ,
33
34
ormConfig : undefined ,
34
35
connection : undefined ,
36
+ overrideConnectionOptions : { } ,
35
37
}
36
38
}
37
39
@@ -42,8 +44,13 @@ export const configureConnection = (option: ConfigureOption = {}) => {
42
44
}
43
45
}
44
46
47
+ export const setConnectionOptions = ( options : TypeORMConnectionOptions ) : void => {
48
+ ; ( global as any ) [ KEY ] . overrideConnectionOptions = options
49
+ }
50
+
45
51
export const getConnectionOptions = async ( ) : Promise < ConnectionOptions > => {
46
52
const ormConfig = ( global as any ) [ KEY ] . ormConfig
53
+ const overrideConnectionOptions = ( global as any ) [ KEY ] . overrideConnectionOptions
47
54
if ( ormConfig === undefined ) {
48
55
const configureOption = ( global as any ) [ KEY ] . configureOption
49
56
const connection = configureOption . connection
@@ -58,6 +65,12 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
58
65
options = filteredOptions
59
66
}
60
67
}
68
+ if ( options . length > 1 ) {
69
+ const filteredOptions = options . filter ( ( o ) => o . name === 'default' )
70
+ if ( filteredOptions . length === 1 ) {
71
+ options = filteredOptions
72
+ }
73
+ }
61
74
if ( options . length === 1 ) {
62
75
const option = options [ 0 ]
63
76
if ( ! option . factories ) {
@@ -66,15 +79,19 @@ export const getConnectionOptions = async (): Promise<ConnectionOptions> => {
66
79
if ( ! option . seeds ) {
67
80
option . seeds = [ process . env . TYPEORM_SEEDING_SEEDS || 'src/database/seeds/**/*{.ts,.js}' ]
68
81
}
69
- ; ( global as any ) [ KEY ] . ormConfig = option
70
- return option
82
+ ; ( global as any ) [ KEY ] . ormConfig = {
83
+ ...option ,
84
+ ...overrideConnectionOptions ,
85
+ }
86
+ return ( global as any ) [ KEY ] . ormConfig
71
87
}
72
88
printError ( 'There are multiple connections please provide a connection name' )
73
89
}
74
90
return ormConfig
75
91
}
76
92
77
93
export const createConnection = async ( option ?: TypeORMConnectionOptions ) : Promise < TypeORMConnection > => {
94
+ const configureOption = ( global as any ) [ KEY ] . configureOption
78
95
let connection = ( global as any ) [ KEY ] . connection
79
96
let ormConfig = ( global as any ) [ KEY ] . ormConfig
80
97
@@ -83,7 +100,12 @@ export const createConnection = async (option?: TypeORMConnectionOptions): Promi
83
100
}
84
101
85
102
if ( connection === undefined ) {
86
- connection = await TypeORMCreateConnection ( ormConfig )
103
+ try {
104
+ connection = await TypeORMGetConnection ( configureOption . name )
105
+ } catch ( _ ) { }
106
+ if ( connection === undefined ) {
107
+ connection = await TypeORMCreateConnection ( ormConfig )
108
+ }
87
109
; ( global as any ) [ KEY ] . connection = connection
88
110
}
89
111
return connection
0 commit comments