From 8f4f57ddeeadb1fe4fc6b042046bb3e767e09983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 2 Jun 2022 09:50:34 +0200 Subject: [PATCH] chore(): improve backward compatibility with v0.2 --- lib/common/typeorm.utils.ts | 5 +++-- lib/typeorm-core.module.ts | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/common/typeorm.utils.ts b/lib/common/typeorm.utils.ts index 4bb2c7b22..a0fa93721 100644 --- a/lib/common/typeorm.utils.ts +++ b/lib/common/typeorm.utils.ts @@ -3,6 +3,7 @@ import { Observable } from 'rxjs'; import { delay, retryWhen, scan } from 'rxjs/operators'; import { AbstractRepository, + Connection, DataSource, DataSourceOptions, EntityManager, @@ -77,11 +78,11 @@ export function getDataSourceToken( | string = DEFAULT_DATA_SOURCE_NAME, ): string | Function | Type { 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`; } diff --git a/lib/typeorm-core.module.ts b/lib/typeorm-core.module.ts index a7312a246..c5004985a 100644 --- a/lib/typeorm-core.module.ts +++ b/lib/typeorm-core.module.ts @@ -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'; @@ -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; @@ -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,