Reputation: 245
I want to use mongoose with NestJs. I'm using the package @nestjs/mongoose
as describe in the documentation.
It works correctly when I use it with standard models. But I need to use GridFS to store files in my mongo database.
How could I use this feature? Is an integration available with @nestjs/mongoose
to use third party libraries like mongoose-gridfs
or another one?
Or should is use directly mongoose without @nestjs/mongoose
in my NestJs
application?
Upvotes: 1
Views: 1884
Reputation: 173
I used the @InjectConnection() decorator from the nestjs/mongoose package to achieve this functionality:
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;
}
My connection is instantiated in the app.tsx using Mongoose.forRootAsync().
Upvotes: 1
Reputation: 245
For thoose who want to use mongoose with other package which needs connection, you shouldn't use the @nestjs/module
.
Here is an example to use standard mongoose library : https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base
Upvotes: 1