-
Notifications
You must be signed in to change notification settings - Fork 72
Description
Feature Request
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" field changes dynamically based on the API request. However, I can't access the HTTP request in the "db" field, which would make it impossible for me to change the DB connection dynamically and it seems to me that it is not currently possible do this.
The solution I'd like to see
I would like to see a way to make the GridFsStorage configuration work dynamically with multiple DB connections.
Ideally, the "db" field in the GridFsStorage configuration would be able to function like the "file" field where the request is accessible through the parameters of a function:
const storage = new GridFsStorage({
// I want to be able to access the request through a function's paramaters. That way I'll be able to return
// the database connection back to the "db" field dynamically based on the request's cookies.
db: (req) => {
// Determine the DB connection dynamically using the req parameter.
dynamicConnection = connectionFactory.getDatabaseConnection(req.cookies.dbName);
// Return the DB connection back to the db field.
return dynamicConnection;
}
// The fact that I have access to the user's HTTP request in this method is excellent.
file: (req, file) => {
...
}
});
const fileUpload = multer({
// Set the storage strategy.
storage: storage,
limits: { fileSize: 1024 * 1024 * 2 },
fileFilter: fileFilter,
});