Skip to content

Commit 6295073

Browse files
fix(@nestjs/typeorm): move keepConnectionAlive check into retry loop
Move keepConnectionAlive check into retry loop so that an existing connection can be checked on retry.
1 parent 8e87f25 commit 6295073

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lib/typeorm-core.module.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Logger,
1010
} from '@nestjs/common';
1111
import { ModuleRef } from '@nestjs/core';
12-
import { defer } from 'rxjs';
12+
import { defer, of } from 'rxjs';
1313
import {
1414
Connection,
1515
ConnectionOptions,
@@ -175,22 +175,22 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
175175
options: TypeOrmModuleOptions,
176176
connectionFactory?: TypeOrmConnectionFactory,
177177
): Promise<Connection> {
178-
try {
179-
if (options.keepConnectionAlive) {
180-
const connectionName = getConnectionName(options as ConnectionOptions);
181-
const manager = getConnectionManager();
182-
if (manager.has(connectionName)) {
183-
const connection = manager.get(connectionName);
184-
if (connection.isConnected) {
185-
return connection;
186-
}
187-
}
188-
}
189-
} catch {}
190-
191178
const connectionToken = getConnectionName(options as ConnectionOptions);
192179
const createTypeormConnection = connectionFactory ?? createConnection;
193180
return await defer(() => {
181+
try {
182+
if (options.keepConnectionAlive) {
183+
const connectionName = getConnectionName(options as ConnectionOptions);
184+
const manager = getConnectionManager();
185+
if (manager.has(connectionName)) {
186+
const connection = manager.get(connectionName);
187+
if (connection.isConnected) {
188+
return of(connection);
189+
}
190+
}
191+
}
192+
} catch {}
193+
194194
if (!options.type) {
195195
return createTypeormConnection();
196196
}

0 commit comments

Comments
 (0)