|
1 | 1 | import {
|
2 | 2 | DynamicModule,
|
3 | 3 | Global,
|
4 |
| - Inject, Logger, Module, |
| 4 | + Inject, |
| 5 | + Logger, |
| 6 | + Module, |
5 | 7 | OnApplicationShutdown,
|
6 | 8 | Provider,
|
7 |
| - Type |
| 9 | + Type, |
8 | 10 | } from '@nestjs/common';
|
9 | 11 | import { ModuleRef } from '@nestjs/core';
|
10 |
| -import { defer, lastValueFrom } from 'rxjs'; |
| 12 | +import { defer, lastValueFrom, of } from 'rxjs'; |
11 | 13 | import {
|
12 | 14 | Connection,
|
13 | 15 | ConnectionOptions,
|
14 | 16 | createConnection,
|
15 |
| - getConnectionManager |
| 17 | + getConnectionManager, |
16 | 18 | } from 'typeorm';
|
17 | 19 | import {
|
18 | 20 | generateString,
|
19 | 21 | getConnectionName,
|
20 | 22 | getConnectionToken,
|
21 | 23 | getEntityManagerToken,
|
22 |
| - handleRetry |
| 24 | + handleRetry, |
23 | 25 | } from './common/typeorm.utils';
|
24 | 26 | import { EntitiesMetadataStorage } from './entities-metadata.storage';
|
25 | 27 | import {
|
| 28 | + TypeOrmConnectionFactory, |
26 | 29 | TypeOrmModuleAsyncOptions,
|
27 | 30 | TypeOrmModuleOptions,
|
28 |
| - TypeOrmOptionsFactory |
| 31 | + TypeOrmOptionsFactory, |
29 | 32 | } from './interfaces/typeorm-options.interface';
|
30 | 33 | import { TYPEORM_MODULE_ID, TYPEORM_MODULE_OPTIONS } from './typeorm.constants';
|
31 | 34 |
|
@@ -68,12 +71,18 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
|
68 | 71 | provide: getConnectionToken(options as ConnectionOptions) as string,
|
69 | 72 | useFactory: async (typeOrmOptions: TypeOrmModuleOptions) => {
|
70 | 73 | if (options.name) {
|
71 |
| - return await this.createConnectionFactory({ |
72 |
| - ...typeOrmOptions, |
73 |
| - name: options.name, |
74 |
| - }); |
| 74 | + return await this.createConnectionFactory( |
| 75 | + { |
| 76 | + ...typeOrmOptions, |
| 77 | + name: options.name, |
| 78 | + }, |
| 79 | + options.connectionFactory, |
| 80 | + ); |
75 | 81 | }
|
76 |
| - return await this.createConnectionFactory(typeOrmOptions); |
| 82 | + return await this.createConnectionFactory( |
| 83 | + typeOrmOptions, |
| 84 | + options.connectionFactory, |
| 85 | + ); |
77 | 86 | },
|
78 | 87 | inject: [TYPEORM_MODULE_OPTIONS],
|
79 | 88 | };
|
@@ -164,52 +173,56 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
|
164 | 173 |
|
165 | 174 | private static async createConnectionFactory(
|
166 | 175 | options: TypeOrmModuleOptions,
|
| 176 | + connectionFactory?: TypeOrmConnectionFactory, |
167 | 177 | ): Promise<Connection> {
|
168 |
| - try { |
169 |
| - if (options.keepConnectionAlive) { |
170 |
| - const connectionName = getConnectionName(options as ConnectionOptions); |
171 |
| - const manager = getConnectionManager(); |
172 |
| - if (manager.has(connectionName)) { |
173 |
| - const connection = manager.get(connectionName); |
174 |
| - if (connection.isConnected) { |
175 |
| - return connection; |
| 178 | + const connectionToken = getConnectionName(options as ConnectionOptions); |
| 179 | + const createTypeormConnection = connectionFactory ?? createConnection; |
| 180 | + return await lastValueFrom( |
| 181 | + defer(() => { |
| 182 | + try { |
| 183 | + if (options.keepConnectionAlive) { |
| 184 | + const connectionName = getConnectionName( |
| 185 | + options as ConnectionOptions, |
| 186 | + ); |
| 187 | + const manager = getConnectionManager(); |
| 188 | + if (manager.has(connectionName)) { |
| 189 | + const connection = manager.get(connectionName); |
| 190 | + if (connection.isConnected) { |
| 191 | + return of(connection); |
| 192 | + } |
| 193 | + } |
176 | 194 | }
|
177 |
| - } |
178 |
| - } |
179 |
| - } catch {} |
| 195 | + } catch {} |
180 | 196 |
|
181 |
| - const connectionToken = getConnectionName(options as ConnectionOptions); |
182 |
| - return lastValueFrom(defer(() => { |
183 |
| - if (!options.type) { |
184 |
| - return createConnection(); |
185 |
| - } |
186 |
| - if (!options.autoLoadEntities) { |
187 |
| - return createConnection(options as ConnectionOptions); |
188 |
| - } |
| 197 | + if (!options.type) { |
| 198 | + return createTypeormConnection(); |
| 199 | + } |
| 200 | + if (!options.autoLoadEntities) { |
| 201 | + return createTypeormConnection(options as ConnectionOptions); |
| 202 | + } |
189 | 203 |
|
190 |
| - let entities = options.entities; |
191 |
| - if (entities) { |
192 |
| - entities = entities.concat( |
193 |
| - EntitiesMetadataStorage.getEntitiesByConnection(connectionToken), |
194 |
| - ); |
195 |
| - } else { |
196 |
| - entities = EntitiesMetadataStorage.getEntitiesByConnection( |
197 |
| - connectionToken, |
198 |
| - ); |
199 |
| - } |
200 |
| - return createConnection({ |
201 |
| - ...options, |
202 |
| - entities, |
203 |
| - } as ConnectionOptions); |
204 |
| - }) |
205 |
| - .pipe( |
| 204 | + let entities = options.entities; |
| 205 | + if (entities) { |
| 206 | + entities = entities.concat( |
| 207 | + EntitiesMetadataStorage.getEntitiesByConnection(connectionToken), |
| 208 | + ); |
| 209 | + } else { |
| 210 | + entities = |
| 211 | + EntitiesMetadataStorage.getEntitiesByConnection(connectionToken); |
| 212 | + } |
| 213 | + return createTypeormConnection({ |
| 214 | + ...options, |
| 215 | + entities, |
| 216 | + } as ConnectionOptions); |
| 217 | + }).pipe( |
206 | 218 | handleRetry(
|
207 | 219 | options.retryAttempts,
|
208 | 220 | options.retryDelay,
|
209 | 221 | connectionToken,
|
210 | 222 | options.verboseRetryLog,
|
211 | 223 | options.toRetry,
|
212 | 224 | ),
|
213 |
| - )) |
| 225 | + ), |
| 226 | + ); |
214 | 227 | }
|
215 | 228 | }
|
0 commit comments