# File GridFS

### 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="<https://717359273-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxaCLvdAgAIA124Mfl6Io%2Fuploads%2FE8L0Ihc0CktU7kpseoYy%2Ftext.txt?alt=media&token=7371b39a-5f86-4591-895b-a9429c19d50b>" %}

```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="configuration">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="validator-regexp">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="stringex">Link to Service documentation</a></td></tr><tr><td><code>IMongoDBService</code></td><td><a href="core/mongodb">Link to Service documentation</a></td></tr><tr><td><code>IDebugService</code></td><td><a href="debug">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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guilhermesantos.gitbook.io/niro-health/modules/file-gridfs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
