Multer GridFsStorage Dynamic Configuration for Multiple MongoDB Connections

386 Views Asked by At

So I'm using MongoDB Atlas, Mongoose, Multer and GridFsStorage to upload files to a single database on the cloud which works fine, but I want to be able to upload files to different databases, using different database connections dynamically.

In order to do this, I want to set up my Multer GridFsStorage configuration so that the "db" property field changes dynamically based on the API request. However, I can't access the HTTP request object inside the "db" property, which would make it possible for me to change the DB connection dynamically. The "db" property inside the GridFsStorage configuration object only accepts the following: enter image description here

In the following example, you can see that I am creating a database connection using a custom getDatabaseConnection() method and then I pass that connection into the DB field that takes in a single static DB connection.

// Multer GridFsStorage configuration.
const storage = new GridFsStorage({
    
    // I NEED TO CHANGE THE DB CONNECTION DYNAMICALLY TO UPLOAD FILES TO DIFFERENT 
    // DATABASES, BUT I HAVE NO ACCESS TO THE HTTP REQUEST OBJECT INSIDE THE DB FIELD!
    db: connectionFactory.getDatabaseConnection(process.env.DB_CONNECT), 
    
    // I can access the request object (req) inside the "file" field.
    file: (req, file) => {
        ...
    }
});

Is there a way to access the HTTP request object inside the GridFsStorage configuration so that I can set the "db" field dynamically with multiple DB connections, or am I approaching this the wrong way??

UPDATE: The owner of the multer-gridfs-storage project has commented on the issue on Github and it seems he's interested in adding it to its next release. You can check out the issue on Github to stay updated.

1

There are 1 best solutions below

3
On

Apparently the functionality you are looking for is missing from the multer-gridfs-storage engine. You could try creating a new issue for a feature request on the GitHub page of this node module.

I noticed that you are looking for a reputable source. It seems that devconcept is the primary contributor to this project. Hopefully he can help you out with this feature by suggesting a workaround, or by adding a function that accesses the HTTP request object in the db property and returns a Mongoose connection object to it (the function could be an alternative input option to the db property aside from the mongoose connection object).

Edit

One other thing you could try is using the createWriteStream() function of the gridfs-stream package instead of using gridfs-store to upload your files. You can see an implementation of this function on the accepted answer of this post.