Skip to content

Commit

Permalink
Merge https://github.com/nestjs/typeorm into fix-connection-error-output
Browse files Browse the repository at this point in the history
  • Loading branch information
ISlavinskyi committed May 25, 2020
2 parents b02e7f9 + 86a0f4f commit 3082e30
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 365 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -183,10 +183,10 @@ Footer should contain a [closing reference to an issue](https://help.github.com/
Samples: (even more [samples](https://github.com/nestjs/nest/commits/master))

```
docs(changelog) update change log to beta.5
docs(changelog): update change log to beta.5
```
```
bugfix(@nestjs/core) need to depend on latest rxjs and zone.js
bugfix(@nestjs/core): need to depend on latest rxjs and zone.js
The version in our package.json gets copied to the one we publish, and users need the latest of these.
```
Expand Down
22 changes: 14 additions & 8 deletions lib/common/typeorm.utils.ts
Expand Up @@ -110,21 +110,27 @@ export function getEntityManagerToken(
export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
connectionName = DEFAULT_CONNECTION_NAME,
verboseRetryLog = false,
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen((e) =>
e.pipe(
scan((errorCount, error: Error) => {
const message = verboseRetryLog
? `Unable to connect to the database. Message: ${
error.message
}. Retrying (${errorCount + 1})...`
: `Unable to connect to the database. Retrying (${
errorCount + 1
})...`;
logger.error(message, error.stack);
const connectionInfo =
connectionName === DEFAULT_CONNECTION_NAME
? ''
: ` (${connectionName})`;
const verboseMessage = verboseRetryLog
? ` Message: ${error.message}.`
: '';
logger.error(
`Unable to connect to the database${connectionInfo}.${verboseMessage} Retrying (${
errorCount + 1
})...`,
error.stack,
);
if (errorCount + 1 >= retryAttempts) {
throw error;
}
Expand Down
9 changes: 3 additions & 6 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 @@ -205,6 +201,7 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
handleRetry(
options.retryAttempts,
options.retryDelay,
connectionToken,
options.verboseRetryLog,
),
)
Expand Down

0 comments on commit 3082e30

Please sign in to comment.