Skip to content

Commit

Permalink
chore(): improve backward compatibility with v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 2, 2022
1 parent 213b147 commit 8f4f57d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/common/typeorm.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Observable } from 'rxjs';
import { delay, retryWhen, scan } from 'rxjs/operators';
import {
AbstractRepository,
Connection,
DataSource,
DataSourceOptions,
EntityManager,
Expand Down Expand Up @@ -77,11 +78,11 @@ export function getDataSourceToken(
| string = DEFAULT_DATA_SOURCE_NAME,
): string | Function | Type<DataSource> {
return DEFAULT_DATA_SOURCE_NAME === dataSource
? DataSource
? DataSource ?? Connection
: 'string' === typeof dataSource
? `${dataSource}DataSource`
: DEFAULT_DATA_SOURCE_NAME === dataSource.name || !dataSource.name
? DataSource
? DataSource ?? Connection
: `${dataSource.name}DataSource`;
}

Expand Down
23 changes: 16 additions & 7 deletions lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ import {
Module,
OnApplicationShutdown,
Provider,
Type
Type,
} from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { defer, lastValueFrom } from 'rxjs';
import { Connection, DataSource, DataSourceOptions } from 'typeorm';
import {
Connection,
createConnection,
DataSource,
DataSourceOptions,
} from 'typeorm';
import {
generateString,
getDataSourceName,
getDataSourceToken,
getEntityManagerToken,
handleRetry
handleRetry,
} from './common/typeorm.utils';
import { EntitiesMetadataStorage } from './entities-metadata.storage';
import {
TypeOrmDataSourceFactory,
TypeOrmModuleAsyncOptions,
TypeOrmModuleOptions,
TypeOrmOptionsFactory
TypeOrmOptionsFactory,
} from './interfaces/typeorm-options.interface';
import { TYPEORM_MODULE_ID, TYPEORM_MODULE_OPTIONS } from './typeorm.constants';

Expand Down Expand Up @@ -201,14 +206,18 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
const dataSourceToken = getDataSourceName(options as DataSourceOptions);
const createTypeormDataSource =
dataSourceFactory ??
((options: DataSourceOptions) => new DataSource(options));
((options: DataSourceOptions) => {
return DataSource === undefined
? createConnection(options)
: new DataSource(options);
});
return await lastValueFrom(
defer(async () => {
if (!options.autoLoadEntities) {
const dataSource = await createTypeormDataSource(
options as DataSourceOptions,
);
return dataSource.initialize();
return dataSource.initialize ? dataSource.initialize() : dataSource;
}

let entities = options.entities;
Expand All @@ -224,7 +233,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
...options,
entities,
} as DataSourceOptions);
return dataSource.initialize();
return dataSource.initialize ? dataSource.initialize() : dataSource;
}).pipe(
handleRetry(
options.retryAttempts,
Expand Down

0 comments on commit 8f4f57d

Please sign in to comment.