Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(database): Fix searching for default connection
Browse files Browse the repository at this point in the history
BrunnerLivio committed Apr 22, 2019

Verified

This commit was signed with the committer’s verified signature.
not-an-aardvark Teddy Katz
1 parent 7ddab11 commit 45bcb06
Showing 4 changed files with 69 additions and 8 deletions.
22 changes: 20 additions & 2 deletions lib/health-indicators/database/mongoose.health.ts
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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;
}
}

/**
@@ -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;
20 changes: 17 additions & 3 deletions lib/health-indicators/database/typeorm.health.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -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;
}
}

/**
@@ -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;
18 changes: 17 additions & 1 deletion sample/000-dogs-app/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { Module } from '@nestjs/common';
import { HealthModule } from './health/health.module';
import { DogModule } from './dog/dog.module';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [HealthModule, DogModule],
imports: [
HealthModule,
DogModule,
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
synchronize: true,
keepConnectionAlive: true,
retryAttempts: 2,
retryDelay: 1000,
}),
],
})
export class ApplicationModule {}
17 changes: 15 additions & 2 deletions sample/000-dogs-app/src/health/terminus-options.service.ts
Original file line number Diff line number Diff line change
@@ -2,18 +2,31 @@ import {
TerminusOptionsFactory,
TerminusEndpoint,
TerminusModuleOptions,
MongooseHealthIndicator,
TypeOrmHealthIndicator,
} from '../../../../lib';
import { DogHealthIndicator } from '../dog/dog.health';
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';

@Injectable()
export class TerminusOptionsService implements TerminusOptionsFactory {
constructor(private readonly dogHealthIndicator: DogHealthIndicator) {}
constructor(
private readonly dogHealthIndicator: DogHealthIndicator,
private typeorm: TypeOrmHealthIndicator,
@InjectConnection() private connection,
) {}

public createTerminusOptions(): TerminusModuleOptions {
const healthEndpoint: TerminusEndpoint = {
url: '/health',
healthIndicators: [async () => this.dogHealthIndicator.isHealthy('dog')],
healthIndicators: [
// async () => this.dogHealthIndicator.isHealthy('dog'),
async () =>
this.typeorm.pingCheck('mongoose', {
timeout: 1000,
}),
],
};
return {
endpoints: [healthEndpoint],

0 comments on commit 45bcb06

Please sign in to comment.