Skip to content

Commit

Permalink
Merge branch 'dsbert-log-conn-name'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 25, 2020
2 parents ac077cb + 3c11893 commit bf3580d
Show file tree
Hide file tree
Showing 4 changed files with 2,417 additions and 2,591 deletions.
7 changes: 6 additions & 1 deletion lib/common/typeorm.utils.ts
Expand Up @@ -110,14 +110,19 @@ export function getEntityManagerToken(
export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
connectionName = DEFAULT_CONNECTION_NAME,
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen(e =>
e.pipe(
scan((errorCount, error: Error) => {
const connectionInfo =
connectionName === DEFAULT_CONNECTION_NAME
? ''
: ` (${connectionName})`;
logger.error(
`Unable to connect to the database. Retrying (${errorCount +
`Unable to connect to the database${connectionInfo}. Retrying (${errorCount +
1})...`,
error.stack,
);
Expand Down
12 changes: 5 additions & 7 deletions lib/typeorm-core.module.ts
Expand Up @@ -28,11 +28,7 @@ import {
TypeOrmModuleOptions,
TypeOrmOptionsFactory,
} from './interfaces/typeorm-options.interface';
import {
DEFAULT_CONNECTION_NAME,
TYPEORM_MODULE_ID,
TYPEORM_MODULE_OPTIONS,
} from './typeorm.constants';
import { TYPEORM_MODULE_ID, TYPEORM_MODULE_OPTIONS } from './typeorm.constants';

@Global()
@Module({})
Expand Down Expand Up @@ -177,6 +173,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
}
} catch {}

const connectionToken = getConnectionName(options as ConnectionOptions);
return await defer(() => {
if (!options.type) {
return createConnection();
Expand All @@ -185,7 +182,6 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
return createConnection(options as ConnectionOptions);
}

const connectionToken = options.name || DEFAULT_CONNECTION_NAME;
let entities = options.entities;
if (entities) {
entities = entities.concat(
Expand All @@ -201,7 +197,9 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
entities,
} as ConnectionOptions);
})
.pipe(handleRetry(options.retryAttempts, options.retryDelay))
.pipe(
handleRetry(options.retryAttempts, options.retryDelay, connectionToken),
)
.toPromise();
}
}

0 comments on commit bf3580d

Please sign in to comment.