nestjs , stream question, i dont know my code would synchronization or asynchronous

22 views Asked by At

i dont know my code would synchronization or asynchronous when users request upload files API , i want asynchronous, dont block my program

import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import { Connection } from 'mongoose';
import { GridFSBucket } from 'mongodb';
import { createReadStream } from 'fs';

@Injectable()
export class GridFSService {
    private bucket:GridFSBucket;
    constructor(
      @InjectConnection() private readonly connection: Connection
    ) {
      this.bucket =new GridFSBucket(this.connection.db,{bucketName:'podimages'});
    }
    async uploadFile(file: Express.Multer.File): Promise<any> {
      const readableFile = createReadStream(file.path);
      const writable =  this.bucket.openUploadStream(file.originalname)
      readableFile.pipe(writable);

      return {
        message: 'File uploaded successfully',
        code: 200
      }
    }
}
0

There are 0 answers