@@ -6,24 +6,29 @@ import {
6
6
Module ,
7
7
OnApplicationShutdown ,
8
8
Provider ,
9
- Type
9
+ Type ,
10
10
} from '@nestjs/common' ;
11
11
import { ModuleRef } from '@nestjs/core' ;
12
12
import { defer , lastValueFrom } from 'rxjs' ;
13
- import { Connection , DataSource , DataSourceOptions } from 'typeorm' ;
13
+ import {
14
+ Connection ,
15
+ createConnection ,
16
+ DataSource ,
17
+ DataSourceOptions ,
18
+ } from 'typeorm' ;
14
19
import {
15
20
generateString ,
16
21
getDataSourceName ,
17
22
getDataSourceToken ,
18
23
getEntityManagerToken ,
19
- handleRetry
24
+ handleRetry ,
20
25
} from './common/typeorm.utils' ;
21
26
import { EntitiesMetadataStorage } from './entities-metadata.storage' ;
22
27
import {
23
28
TypeOrmDataSourceFactory ,
24
29
TypeOrmModuleAsyncOptions ,
25
30
TypeOrmModuleOptions ,
26
- TypeOrmOptionsFactory
31
+ TypeOrmOptionsFactory ,
27
32
} from './interfaces/typeorm-options.interface' ;
28
33
import { TYPEORM_MODULE_ID , TYPEORM_MODULE_OPTIONS } from './typeorm.constants' ;
29
34
@@ -201,14 +206,18 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
201
206
const dataSourceToken = getDataSourceName ( options as DataSourceOptions ) ;
202
207
const createTypeormDataSource =
203
208
dataSourceFactory ??
204
- ( ( options : DataSourceOptions ) => new DataSource ( options ) ) ;
209
+ ( ( options : DataSourceOptions ) => {
210
+ return DataSource === undefined
211
+ ? createConnection ( options )
212
+ : new DataSource ( options ) ;
213
+ } ) ;
205
214
return await lastValueFrom (
206
215
defer ( async ( ) => {
207
216
if ( ! options . autoLoadEntities ) {
208
217
const dataSource = await createTypeormDataSource (
209
218
options as DataSourceOptions ,
210
219
) ;
211
- return dataSource . initialize ( ) ;
220
+ return dataSource . initialize ? dataSource . initialize ( ) : dataSource ;
212
221
}
213
222
214
223
let entities = options . entities ;
@@ -224,7 +233,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
224
233
...options ,
225
234
entities,
226
235
} as DataSourceOptions ) ;
227
- return dataSource . initialize ( ) ;
236
+ return dataSource . initialize ? dataSource . initialize ( ) : dataSource ;
228
237
} ) . pipe (
229
238
handleRetry (
230
239
options . retryAttempts ,
0 commit comments