Skip to content

Commit

Permalink
chore(mikroorm): add mongodb support
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Jun 27, 2022
1 parent e67d939 commit 6360b1c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 66 deletions.
99 changes: 63 additions & 36 deletions e2e/health-checks/mikro-orm.health.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,72 @@ describe('MikroOrmHealthIndicator', () => {
let app: INestApplication;
let setHealthEndpoint: DynamicHealthEndpointFn;

beforeEach(
() =>
(setHealthEndpoint =
bootstrapTestingModule().withMikroOrm().setHealthEndpoint),
);

describe('#pingCheck', () => {
it('should check if the mikroOrm is available', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([async () => mikroOrm.pingCheck('mikroOrm')]),
).start();
const details = { mikroOrm: { status: 'up' } };
return request(app.getHttpServer()).get('/health').expect(200).expect({
status: 'ok',
info: details,
error: {},
details,
describe('mongo', () => {
beforeEach(
() =>
(setHealthEndpoint = bootstrapTestingModule()
.withMikroOrm()
.andMongo().setHealthEndpoint),
);

describe('#pingCheck', () => {
it('should check if the mikroOrm is available', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([async () => mikroOrm.pingCheck('mikroOrm')]),
).start();
const details = { mikroOrm: { status: 'up' } };
return request(app.getHttpServer()).get('/health').expect(200).expect({
status: 'ok',
info: details,
error: {},
details,
});
});
});
});

describe('mysql', () => {
beforeEach(
() =>
(setHealthEndpoint = bootstrapTestingModule()
.withMikroOrm()
.andMysql().setHealthEndpoint),
);

describe('#pingCheck', () => {
it('should check if the mikroOrm is available', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([async () => mikroOrm.pingCheck('mikroOrm')]),
).start();
const details = { mikroOrm: { status: 'up' } };
return request(app.getHttpServer()).get('/health').expect(200).expect({
status: 'ok',
info: details,
error: {},
details,
});
});

it('should throw an error if runs into timeout error', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([
async () => mikroOrm.pingCheck('mikroOrm', { timeout: 1 }),
]),
).start();

const details = {
mikroOrm: {
status: 'down',
message: 'timeout of 1ms exceeded',
},
};

it('should throw an error if runs into timeout error', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([
async () => mikroOrm.pingCheck('mikroOrm', { timeout: 1 }),
]),
).start();

const details = {
mikroOrm: {
status: 'down',
message: 'timeout of 1ms exceeded',
},
};

return request(app.getHttpServer()).get('/health').expect(503).expect({
status: 'error',
info: {},
error: details,
details,
return request(app.getHttpServer()).get('/health').expect(503).expect({
status: 'error',
info: {},
error: details,
details,
});
});
});
});
Expand Down
45 changes: 31 additions & 14 deletions e2e/helper/bootstrap-testing-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,37 @@ export function bootstrapTestingModule() {
}

function withMikroOrm() {
imports.push(
MikroOrmModule.forRoot({
type: 'mysql',
host: '0.0.0.0',
port: 3306,
user: 'root',
password: 'root',
dbName: 'test',
discovery: { warnWhenNoEntities: false },
strict: true,
}),
);

return { setHealthEndpoint };
return {
andMongo: () => {
imports.push(
MikroOrmModule.forRoot({
type: 'mongo',
dbName: 'test',
discovery: { warnWhenNoEntities: false },
strict: true,
clientUrl: 'mongodb://0.0.0.0:27017'
}),
);

return { setHealthEndpoint };
},
andMysql: () => {
imports.push(
MikroOrmModule.forRoot({
type: 'mysql',
host: '0.0.0.0',
port: 3306,
user: 'root',
password: 'root',
dbName: 'test',
discovery: { warnWhenNoEntities: false },
strict: true,
}),
);

return { setHealthEndpoint };
}
}
}

function withHttp() {
Expand Down
13 changes: 8 additions & 5 deletions lib/health-indicator/database/mikro-orm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotImplementedException, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { HealthCheckError } from '../../health-check/health-check.error';

import * as MikroOrmNestJS from '@mikro-orm/core';
import * as MikroOrm from '@mikro-orm/core';
import { TimeoutError } from '../../errors';
import {
TimeoutError as PromiseTimeoutError,
Expand Down Expand Up @@ -100,12 +100,12 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
/**
* Returns the connection of the current DI context
*/
private getContextConnection(): MikroOrmNestJS.Connection | null {
private getContextConnection(): MikroOrm.Connection | null {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { MikroORM } = require('@mikro-orm/core') as typeof MikroOrmNestJS;
const { MikroORM } = require('@mikro-orm/core') as typeof MikroOrm;
const mikro = this.moduleRef.get(MikroORM, { strict: false });

const connection: MikroOrmNestJS.Connection = mikro.em.getConnection();
const connection: MikroOrm.Connection = mikro.em.getConnection();

if (!connection) {
return null;
Expand All @@ -120,7 +120,7 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
* @param timeout The timeout how long the ping should maximum take
*
*/
private async pingDb(connection: MikroOrmNestJS.Connection, timeout: number) {
private async pingDb(connection: MikroOrm.Connection, timeout: number) {
let check: Promise<any>;
const type = connection.getPlatform().getConfig().get('type');

Expand All @@ -131,6 +131,9 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
case 'sqlite':
check = connection.execute('SELECT 1');
break;
case 'mongo':
check = connection.isConnected();
break;
default:
throw new NotImplementedException(
`${type} ping check is not implemented yet`,
Expand Down
74 changes: 63 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@grpc/grpc-js": "1.6.7",
"@grpc/proto-loader": "0.6.12",
"@mikro-orm/core": "^5.2.1",
"@mikro-orm/mongodb": "^5.2.1",
"@mikro-orm/mysql": "^5.2.1",
"@mikro-orm/nestjs": "^5.0.2",
"@nestjs/axios": "0.0.7",
Expand Down

0 comments on commit 6360b1c

Please sign in to comment.