Skip to content

Commit

Permalink
feat(@nestjs/typeorm): added connectionFactory option
Browse files Browse the repository at this point in the history
Added a connectionFactory option to TypeOrmModuleAsyncOptions that allows providing a custom connection factory.
  • Loading branch information
IRCraziestTaxi committed Jul 7, 2021
1 parent 1c75a77 commit 8e87f25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/interfaces/typeorm-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type } from '@nestjs/common';
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { ConnectionOptions } from 'typeorm';
import { Connection, ConnectionOptions } from 'typeorm';

export type TypeOrmModuleOptions = {
/**
Expand Down Expand Up @@ -41,6 +41,10 @@ export interface TypeOrmOptionsFactory {
): Promise<TypeOrmModuleOptions> | TypeOrmModuleOptions;
}

export type TypeOrmConnectionFactory = (
options?: ConnectionOptions,
) => Promise<Connection>;

export interface TypeOrmModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
name?: string;
Expand All @@ -49,5 +53,6 @@ export interface TypeOrmModuleAsyncOptions
useFactory?: (
...args: any[]
) => Promise<TypeOrmModuleOptions> | TypeOrmModuleOptions;
connectionFactory?: TypeOrmConnectionFactory;
inject?: any[];
}
25 changes: 17 additions & 8 deletions lib/typeorm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from './common/typeorm.utils';
import { EntitiesMetadataStorage } from './entities-metadata.storage';
import {
TypeOrmConnectionFactory,
TypeOrmModuleAsyncOptions,
TypeOrmModuleOptions,
TypeOrmOptionsFactory,
Expand Down Expand Up @@ -70,12 +71,18 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
provide: getConnectionToken(options as ConnectionOptions) as string,
useFactory: async (typeOrmOptions: TypeOrmModuleOptions) => {
if (options.name) {
return await this.createConnectionFactory({
...typeOrmOptions,
name: options.name,
});
return await this.createConnectionFactory(
{
...typeOrmOptions,
name: options.name,
},
options.connectionFactory,
);
}
return await this.createConnectionFactory(typeOrmOptions);
return await this.createConnectionFactory(
typeOrmOptions,
options.connectionFactory,
);
},
inject: [TYPEORM_MODULE_OPTIONS],
};
Expand Down Expand Up @@ -166,6 +173,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {

private static async createConnectionFactory(
options: TypeOrmModuleOptions,
connectionFactory?: TypeOrmConnectionFactory,
): Promise<Connection> {
try {
if (options.keepConnectionAlive) {
Expand All @@ -181,12 +189,13 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
} catch {}

const connectionToken = getConnectionName(options as ConnectionOptions);
const createTypeormConnection = connectionFactory ?? createConnection;
return await defer(() => {
if (!options.type) {
return createConnection();
return createTypeormConnection();
}
if (!options.autoLoadEntities) {
return createConnection(options as ConnectionOptions);
return createTypeormConnection(options as ConnectionOptions);
}

let entities = options.entities;
Expand All @@ -199,7 +208,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
connectionToken,
);
}
return createConnection({
return createTypeormConnection({
...options,
entities,
} as ConnectionOptions);
Expand Down

0 comments on commit 8e87f25

Please sign in to comment.