From 3d3a3f1dc0fa1d058b4edbf10eb2dbbefb30c393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Fri, 8 Apr 2022 11:08:48 +0200 Subject: [PATCH] feat(): add get connection token alias for backward comp --- lib/common/typeorm.utils.ts | 3 ++ lib/typeorm-core.module.ts | 66 ++++++++++++++++++++++++++----------- package.json | 2 +- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/lib/common/typeorm.utils.ts b/lib/common/typeorm.utils.ts index d508a5f36..4bb2c7b22 100644 --- a/lib/common/typeorm.utils.ts +++ b/lib/common/typeorm.utils.ts @@ -85,6 +85,9 @@ export function getDataSourceToken( : `${dataSource.name}DataSource`; } +/** @deprecated */ +export const getConnectionToken = getDataSourceToken; + /** * This function returns a DataSource prefix based on the dataSource name * @param {DataSource | DataSourceOptions | string} [dataSource='default'] This optional parameter is either diff --git a/lib/typeorm-core.module.ts b/lib/typeorm-core.module.ts index dcd1b48a2..ca39b7c06 100644 --- a/lib/typeorm-core.module.ts +++ b/lib/typeorm-core.module.ts @@ -10,7 +10,7 @@ import { } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; import { defer, lastValueFrom } from 'rxjs'; -import { DataSource, DataSourceOptions } from 'typeorm'; +import { Connection, DataSource, DataSourceOptions } from 'typeorm'; import { generateString, getDataSourceName, @@ -44,26 +44,39 @@ export class TypeOrmCoreModule implements OnApplicationShutdown { useValue: options, }; const dataSourceProvider = { - provide: getDataSourceToken(options as DataSourceOptions) as string, + provide: getDataSourceToken(options as DataSourceOptions), useFactory: async () => await this.createDataSourceFactory(options), }; const entityManagerProvider = this.createEntityManagerProvider( options as DataSourceOptions, ); + + const providers = [ + entityManagerProvider, + dataSourceProvider, + typeOrmModuleOptions, + ]; + const exports = [entityManagerProvider, dataSourceProvider]; + + // TODO: "Connection" class is going to be removed in the next version of "typeorm" + if (dataSourceProvider.provide === DataSource) { + providers.push({ + provide: Connection, + useExisting: DataSource, + }); + exports.push(Connection); + } + return { module: TypeOrmCoreModule, - providers: [ - entityManagerProvider, - dataSourceProvider, - typeOrmModuleOptions, - ], - exports: [entityManagerProvider, dataSourceProvider], + providers, + exports, }; } static forRootAsync(options: TypeOrmModuleAsyncOptions): DynamicModule { const dataSourceProvider = { - provide: getDataSourceToken(options as DataSourceOptions) as string, + provide: getDataSourceToken(options as DataSourceOptions), useFactory: async (typeOrmOptions: TypeOrmModuleOptions) => { if (options.name) { return await this.createDataSourceFactory( @@ -88,19 +101,34 @@ export class TypeOrmCoreModule implements OnApplicationShutdown { }; const asyncProviders = this.createAsyncProviders(options); + const providers = [ + ...asyncProviders, + entityManagerProvider, + dataSourceProvider, + { + provide: TYPEORM_MODULE_ID, + useValue: generateString(), + }, + ]; + const exports: Array = [ + entityManagerProvider, + dataSourceProvider, + ]; + + // TODO: "Connection" class is going to be removed in the next version of "typeorm" + if (dataSourceProvider.provide === DataSource) { + providers.push({ + provide: Connection, + useExisting: DataSource, + }); + exports.push(Connection); + } + return { module: TypeOrmCoreModule, imports: options.imports, - providers: [ - ...asyncProviders, - entityManagerProvider, - dataSourceProvider, - { - provide: TYPEORM_MODULE_ID, - useValue: generateString(), - }, - ], - exports: [entityManagerProvider, dataSourceProvider], + providers, + exports, }; } diff --git a/package.json b/package.json index e013d5391..e8f258926 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nestjs/typeorm", - "version": "8.0.3", + "version": "9.0.0-next.2", "description": "Nest - modern, fast, powerful node.js web framework (@typeorm)", "author": "Kamil Mysliwiec", "license": "MIT",