📝MongoDB

This service is responsible for connecting to the MongoDB database.

Imports

import { MongoDBService } from 'niro-health';

Environment Variables

Do you create variables an .env file.

name
description

MONGODB_USERNAME

Username of the mongodb.

MONGODB_PASSWORD

Password of the mongodb.

MONGODB_HOST

Host of the mongodb.

MONGODB_PORT

Port of the mongodb.

MONGODB_NAME

Database name of the mongodb.

MONGODB_GRIDFS_NAME

Database gridfs name of the mongodb.

MONGODB_CONNECTION_SSL

SSL connection of the mongodb.

MONGODB_PROJECT_NAME

Project name of the mongodb.

Check the example file.

.env
MONGODB_USERNAME=mongodb
MONGODB_PASSWORD=mongodb
MONGODB_HOST=localhost
MONGODB_PORT=27017
MONGODB_NAME=project_development
MONGODB_GRIDFS_NAME=project_development_gridfs
MONGODB_CONNECTION_SSL=false
MONGODB_PROJECT_NAME=niro_health

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import {
  AppHostService,
  MongoDBService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  DebugService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IAppHostService',
      useClass: AppHostService,
    },
    {
      provide: 'IMongoDBService',
      useClass: MongoDBService,
    },
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
    {
      provide: 'IDebugService',
      useClass: DebugService,
    },
  ],
})
export class AppModule {}

Injecting the module into our service.

import { Inject, Injectable } from '@nestjs/common';
import type { IAppHostService, IMongoDBService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IAppHostService')
    private readonly appHostService: IAppHostService,
    @Inject('IMongoDBService')
    private readonly mongoDBService: IMongoDBService,
  ) {}

  async enableShutdownHooks() {
    await this.mongoDBService.enableShutdownHooks(this.appHostService.app);
  }
}

Modifying our main.ts

main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module';
import { Logger } from '@nestjs/common';
import { AppHostModule, AppHostService } from 'niro-health';
import { AppModule } from './app.module';
import { AppService } from './app.service';

(async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new Logger('NestApplication', {
      timestamp: true,
    }),
    cors: true,
  });
  app.select(AppHostModule).get(AppHostService).setApp(app);
  app.select(AppModule).get(AppService).enableShutdownHooks();
})();

With dependencies

You will need to inject the following interfaces.

Interface
Service

IAppHostService

IConfigurationService

IValidatorRegexpService

IStringExService

IDebugService

Properties

Property
Scope
Description

app

public

The application instance.

_mongoConfig

private

The MongoDB configuration.

Methods

Method
Scope
Description

onModuleInit

public

Initialize the module.

_initialize

private

Initialize the MongoDB connection.

_uri

private

Create the MongoDB connection URI.

_connectionLogs

private

MongoDB connection logs.

_connectionClose

private

MongoDB connection close.

_connectionOpen

private

MongoDB connection open.

getDB

public

Create a new Db instance sharing the current socket connections.

shutdown

public

Close the current connection.

onApplicationShutdown

public

Application shutdown hook.

enableShutdownHooks

public

Enable shutdown hooks.

Last updated