📝File GridFS

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

Imports

import { FileGridfsService } from 'niro-health';

Environment Variables

name
description

MONGODB_GRIDFS_NAME

The mongodb database gridfs name.

MONGODB_NAME

The mongodb database name.

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

.env
MONGODB_GRIDFS_NAME=project_development_gridfs
MONGODB_NAME=project_development

Method of Use

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

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

Injecting the module into our service.

Get a example file to test.

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.

Interface
Service

IConfigurationService

IValidatorRegexpService

IStringExService

IMongoDBService

IDebugService

Properties

Property
Scope
Description

get dbName

private

Return database name.

Methods

Method
Scope
Description

openUploadStream

public

Open Stream(GridFSBucketWriteStream) for writing data in database.

openDownloadStream

public

Open Stream(GridFSBucketReadStream) for reading data in database.

getDownloadStream

public

Return Stream(GridFSBucketReadStream) for reading data in database.

getVersion

public

Return version of file in database.

rename

public

Rename file in database.

delete

public

Delete file in database.

Last updated