我想和NestJs一起使用mongoose。我正在使用@nestjs/mongoose
中描述的包documentation。当我将它与标准型号一起使用时,它可以正常工作但我需要使用GridFS在我的mongo数据库中存储文件。
我怎么能用这个功能?是否可以与@nestjs/mongoose
集成以使用mongoose-gridfs
或其他第三方库?或者应该在我的@nestjs/mongoose
应用程序中直接使用没有NestJs
的mongoose?
对于想要将mongoose与其他需要连接的软件包一起使用的人来说,你不应该使用@nestjs/module
。以下是使用标准mongoose库的示例:https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base
我使用了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中实例化。