> For the complete documentation index, see [llms.txt](https://guilhermesantos.gitbook.io/niro-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guilhermesantos.gitbook.io/niro-health/modules/core/mongodb.md).

# MongoDB

This service is responsible for connecting to the MongoDB database.

### Imports

{% tabs %}
{% tab title="Service" %}

```typescript
import { MongoDBService } from 'niro-health';
```

{% endtab %}

{% tab title="Module" %}

```typescript
import { MongoDBModule } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { IMongoDBService } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Environment Variables

Do you create variables an `.env` file.

<table><thead><tr><th width="291">name</th><th width="460">description</th></tr></thead><tbody><tr><td><code>MONGODB_USERNAME</code></td><td><code>Username of the mongodb.</code></td></tr><tr><td><code>MONGODB_PASSWORD</code></td><td><code>Password of the mongodb.</code></td></tr><tr><td><code>MONGODB_HOST</code></td><td><code>Host of the mongodb.</code></td></tr><tr><td><code>MONGODB_PORT</code></td><td><code>Port of the mongodb.</code></td></tr><tr><td><code>MONGODB_NAME</code></td><td><code>Database name of the mongodb.</code></td></tr><tr><td><code>MONGODB_GRIDFS_NAME</code></td><td><code>Database gridfs name of the mongodb.</code></td></tr><tr><td><code>MONGODB_CONNECTION_SSL</code></td><td><code>SSL connection of the mongodb.</code></td></tr><tr><td><code>MONGODB_PROJECT_NAME</code></td><td><code>Project name of the mongodb.</code></td></tr></tbody></table>

Check the example file.

{% code title=".env" %}

```properties
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
```

{% endcode %}

### Method of Use

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

{% code overflow="wrap" %}

```typescript
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 {}
```

{% endcode %}

Injecting the module into our service.

```typescript
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 <mark style="color:blue;">**`main.ts`**</mark>

{% code title="main.ts" %}

```typescript
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();
})();
```

{% endcode %}

### With dependencies

You will need to inject the following interfaces.

<table><thead><tr><th width="308">Interface</th><th>Service</th></tr></thead><tbody><tr><td><code>IAppHostService</code></td><td><a href="/niro-health/modules/app-host.md">Link to Service documentation</a></td></tr><tr><td><code>IConfigurationService</code></td><td><a href="/niro-health/modules/configuration.md">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/niro-health/modules/validator-regexp.md">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/niro-health/modules/stringex.md">Link to Service documentation</a></td></tr><tr><td><code>IDebugService</code></td><td><a href="/niro-health/modules/debug.md">Link to Service documentation</a></td></tr></tbody></table>

### Properties

<table><thead><tr><th width="233">Property</th><th width="116.33333333333331" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>app</code></td><td align="center"><code>public</code></td><td><code>The application instance.</code></td></tr><tr><td><code>_mongoConfig</code></td><td align="center"><code>private</code></td><td><code>The MongoDB configuration.</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="265.3333333333333">Method</th><th width="119" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>onModuleInit</code></td><td align="center"><code>public</code></td><td><code>Initialize the module.</code></td></tr><tr><td><code>_initialize</code></td><td align="center"><code>private</code></td><td><code>Initialize the MongoDB connection.</code></td></tr><tr><td><code>_uri</code></td><td align="center"><code>private</code></td><td><code>Create the MongoDB connection URI.</code></td></tr><tr><td><code>_connectionLogs</code></td><td align="center"><code>private</code></td><td><code>MongoDB connection logs.</code></td></tr><tr><td><code>_connectionClose</code></td><td align="center"><code>private</code></td><td><code>MongoDB connection close.</code></td></tr><tr><td><code>_connectionOpen</code></td><td align="center"><code>private</code></td><td><code>MongoDB connection open.</code></td></tr><tr><td><code>getDB</code></td><td align="center"><code>public</code></td><td><code>Create a new Db instance sharing the current socket connections.</code></td></tr><tr><td><code>shutdown</code></td><td align="center"><code>public</code></td><td><code>Close the current connection.</code></td></tr><tr><td><code>onApplicationShutdown</code></td><td align="center"><code>public</code></td><td><code>Application shutdown hook.</code></td></tr><tr><td><code>enableShutdownHooks</code></td><td align="center"><code>public</code></td><td><code>Enable shutdown hooks.</code></td></tr></tbody></table>
