Skip to content

Commit 8f4f57d

Browse files
chore(): improve backward compatibility with v0.2
1 parent 213b147 commit 8f4f57d

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/common/typeorm.utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Observable } from 'rxjs';
33
import { delay, retryWhen, scan } from 'rxjs/operators';
44
import {
55
AbstractRepository,
6+
Connection,
67
DataSource,
78
DataSourceOptions,
89
EntityManager,
@@ -77,11 +78,11 @@ export function getDataSourceToken(
7778
| string = DEFAULT_DATA_SOURCE_NAME,
7879
): string | Function | Type<DataSource> {
7980
return DEFAULT_DATA_SOURCE_NAME === dataSource
80-
? DataSource
81+
? DataSource ?? Connection
8182
: 'string' === typeof dataSource
8283
? `${dataSource}DataSource`
8384
: DEFAULT_DATA_SOURCE_NAME === dataSource.name || !dataSource.name
84-
? DataSource
85+
? DataSource ?? Connection
8586
: `${dataSource.name}DataSource`;
8687
}
8788

lib/typeorm-core.module.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ import {
66
Module,
77
OnApplicationShutdown,
88
Provider,
9-
Type
9+
Type,
1010
} from '@nestjs/common';
1111
import { ModuleRef } from '@nestjs/core';
1212
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';
1419
import {
1520
generateString,
1621
getDataSourceName,
1722
getDataSourceToken,
1823
getEntityManagerToken,
19-
handleRetry
24+
handleRetry,
2025
} from './common/typeorm.utils';
2126
import { EntitiesMetadataStorage } from './entities-metadata.storage';
2227
import {
2328
TypeOrmDataSourceFactory,
2429
TypeOrmModuleAsyncOptions,
2530
TypeOrmModuleOptions,
26-
TypeOrmOptionsFactory
31+
TypeOrmOptionsFactory,
2732
} from './interfaces/typeorm-options.interface';
2833
import { TYPEORM_MODULE_ID, TYPEORM_MODULE_OPTIONS } from './typeorm.constants';
2934

@@ -201,14 +206,18 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
201206
const dataSourceToken = getDataSourceName(options as DataSourceOptions);
202207
const createTypeormDataSource =
203208
dataSourceFactory ??
204-
((options: DataSourceOptions) => new DataSource(options));
209+
((options: DataSourceOptions) => {
210+
return DataSource === undefined
211+
? createConnection(options)
212+
: new DataSource(options);
213+
});
205214
return await lastValueFrom(
206215
defer(async () => {
207216
if (!options.autoLoadEntities) {
208217
const dataSource = await createTypeormDataSource(
209218
options as DataSourceOptions,
210219
);
211-
return dataSource.initialize();
220+
return dataSource.initialize ? dataSource.initialize() : dataSource;
212221
}
213222

214223
let entities = options.entities;
@@ -224,7 +233,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
224233
...options,
225234
entities,
226235
} as DataSourceOptions);
227-
return dataSource.initialize();
236+
return dataSource.initialize ? dataSource.initialize() : dataSource;
228237
}).pipe(
229238
handleRetry(
230239
options.retryAttempts,

0 commit comments

Comments
 (0)