Skip to content

Commit

Permalink
feat(): add get connection token alias for backward comp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Apr 8, 2022
1 parent 2f91da4 commit 3d3a3f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/common/typeorm.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 47 additions & 19 deletions lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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<Provider | Function> = [
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,
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 3d3a3f1

Please sign in to comment.