NestJS - 使用GridFS的Mongoose

问题描述 投票:0回答:2

我想和NestJs一起使用mongoose。我正在使用@nestjs/mongoose中描述的包documentation。当我将它与标准型号一起使用时,它可以正常工作但我需要使用GridFS在我的mongo数据库中存储文件。

我怎么能用这个功能?是否可以与@nestjs/mongoose集成以使用mongoose-gridfs或其他第三方库?或者应该在我的@nestjs/mongoose应用程序中直接使用没有NestJs的mongoose?

node.js mongodb typescript mongoose nestjs
2个回答
0
投票

对于想要将mongoose与其他需要连接的软件包一起使用的人来说,你不应该使用@nestjs/module。以下是使用标准mongoose库的示例:https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base


0
投票

我使用了nestjs / mongoose包中的@InjectConnection()装饰器来实现这个功能:

export class AttachmentsService {
  private readonly attachmentGridFsRepository: any; // This is used to access the binary data in the files
  private readonly attachmentRepository: Model<AttachmentDocument>; // This is used to access file metadata

  constructor(@InjectConnection() private readonly mongooseConnection: Mongoose,
              @Inject(Modules.Logger) private readonly logger: Logger) {
    this.attachmentGridFsRepository = gridfs({
      collection: 'attachments',
      model: Schemas.Attachment,
      mongooseConnection: this.mongooseConnection,
    });

    this.attachmentRepository = this.attachmentGridFsRepository.model;
  }

我的连接使用Mongoose.forRootAsync()在app.tsx中实例化。

© www.soinside.com 2019 - 2024. All rights reserved.