> 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/file-gridfs.md).

# File GridFS

The module that handles the file system in the database (MongoDB).

### Imports

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

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

{% endtab %}

{% tab title="Module" %}

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

{% endtab %}

{% tab title="Interface" %}

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

{% endtab %}
{% endtabs %}

### Environment Variables

<table><thead><tr><th width="260">name</th><th>description</th></tr></thead><tbody><tr><td><code>MONGODB_GRIDFS_NAME</code></td><td><code>The mongodb database gridfs name.</code></td></tr><tr><td><code>MONGODB_NAME</code></td><td><code>The mongodb database name.</code></td></tr></tbody></table>

Write **`.env`** file with environment variable. Below has an example:

{% code title=".env" %}

```bash
MONGODB_GRIDFS_NAME=project_development_gridfs
MONGODB_NAME=project_development
```

{% 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 {
  FileGridfsService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  MongoDBService,
  DebugService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

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

{% endcode %}

Injecting the module into our service.

> Get a example file to test.

{% file src="/files/ma2b0IoLlIVWvYXS5RUF" %}

```typescript
import { Inject, Injectable } from '@nestjs/common';
import type { IFileGridfsService } from 'niro-health';

import * as fs from 'fs';
import * as path from 'path';

@Injectable()
export class AppService {
  constructor(
    @Inject('IFileGridfsService')
    private readonly fileGridfsService: IFileGridfsService,
  ) {}

  async openUploadStream() {
    const file = fs.createReadStream(path.join(__dirname, 'test', 'text.txt'));
    return await this.fileGridfsService.openUploadStream(file, {
      filename: 'text',
      authorId: '123456def',
      fileext: '.txt',
      version: 1,
      size: '12B',
      status: 'Active',
    })
  }
}
```

### 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>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>IMongoDBService</code></td><td><a href="/niro-health/modules/core/mongodb.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="301.3333333333333">Property</th><th width="127" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>dbName</code></td><td align="center"><code>private</code></td><td><code>Return database name.</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="236.33333333333331">Method</th><th width="121" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>openUploadStream</code></td><td align="center"><code>public</code></td><td><code>Open Stream(GridFSBucketWriteStream) for writing data in database.</code></td></tr><tr><td><code>openDownloadStream</code></td><td align="center"><code>public</code></td><td><code>Open Stream(GridFSBucketReadStream) for reading data in database.</code></td></tr><tr><td><code>getDownloadStream</code></td><td align="center"><code>public</code></td><td><code>Return Stream(GridFSBucketReadStream) for reading data in database.</code></td></tr><tr><td><code>getVersion</code></td><td align="center"><code>public</code></td><td><code>Return version of file in database.</code></td></tr><tr><td><code>rename</code></td><td align="center"><code>public</code></td><td><code>Rename file in database.</code></td></tr><tr><td><code>delete</code></td><td align="center"><code>public</code></td><td><code>Delete file in database.</code></td></tr></tbody></table>
