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:
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.