Skip to content

Commit

Permalink
fix(database): Fix searching for default connection
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Apr 22, 2019
1 parent 7ddab11 commit 8e43ce2
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 120 deletions.
22 changes: 20 additions & 2 deletions lib/health-indicators/database/mongoose.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
TimeoutError as PromiseTimeoutError,
checkPackages,
} from '../../utils';
import { HealthIndicatorResult, TimeoutError } from '../../';
import {
HealthIndicatorResult,
TimeoutError,
ConnectionNotFoundError,
} from '../../';
import { HealthIndicator } from '../health-indicator';

export interface MongoosePingCheckSettings {
Expand Down Expand Up @@ -58,7 +62,13 @@ export class MongooseHealthIndicator extends HealthIndicator {
getConnectionToken,
} = require('@nestjs/mongoose/dist/common/mongoose.utils') as typeof NestJSMongoose;

return this.moduleRef.get(getConnectionToken(null));
try {
return this.moduleRef.get(getConnectionToken() as string, {
strict: false,
});
} catch (err) {
return null;
}
}

/**
Expand Down Expand Up @@ -90,6 +100,14 @@ export class MongooseHealthIndicator extends HealthIndicator {
const connection = options.connection || this.getContextConnection();
const timeout = options.timeout || 1000;

if (!connection) {
throw new ConnectionNotFoundError(
this.getStatus(key, isHealthy, {
message: 'Connection provider not found in application context',
}),
);
}

try {
await this.pingDb(connection, timeout);
isHealthy = true;
Expand Down
20 changes: 17 additions & 3 deletions lib/health-indicators/database/typeorm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HealthCheckError } from '@godaddy/terminus';
import * as NestJSTypeOrm from '@nestjs/typeorm';

import { HealthIndicatorResult } from '../../interfaces/health-indicator.interface';
import { TimeoutError } from '../../errors';
import { TimeoutError, ConnectionNotFoundError } from '../../errors';
import {
TimeoutError as PromiseTimeoutError,
promiseTimeout,
Expand Down Expand Up @@ -54,12 +54,18 @@ export class TypeOrmHealthIndicator extends HealthIndicator {
/**
* Returns the connection of the current DI context
*/
private getContextConnection(): Connection {
private getContextConnection(): Connection | null {
const {
getConnectionToken,
} = require('@nestjs/typeorm/dist/common/typeorm.utils') as typeof NestJSTypeOrm;

return this.moduleRef.get(getConnectionToken() as string);
try {
return this.moduleRef.get(getConnectionToken() as string, {
strict: false,
});
} catch (err) {
return null;
}
}

/**
Expand Down Expand Up @@ -93,6 +99,14 @@ export class TypeOrmHealthIndicator extends HealthIndicator {
options.connection || this.getContextConnection();
const timeout = options.timeout || 1000;

if (!connection) {
throw new ConnectionNotFoundError(
this.getStatus(key, isHealthy, {
message: 'Connection provider not found in application context',
}),
);
}

try {
await this.pingDb(connection, timeout);
isHealthy = true;
Expand Down
Loading

0 comments on commit 8e43ce2

Please sign in to comment.