devconcept / multer-gridfs-storage

🍃 GridFS storage engine for Multer to store uploaded files directly to MongoDb
MIT License
237 stars 67 forks source link

Dynamic Configuration for Multiple MongoDB Connections #457

Open Nikitas-io opened 2 years ago

Nikitas-io commented 2 years ago

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,
});
devconcept commented 2 years ago

This is very interesting indeed and I think I have enough material for a new major release. I'll start working on it and see what I can come up with.

Nikitas-io commented 2 years ago

Thank you so much for your interest in the issue! I posted a question about this problem on Stack Overflow before opening this issue and given the fact that it got some upvotes it seems to me that other people would find this feature usefull as well ❤️‍🔥

PanagosAlex commented 2 years ago

Any updates on this issue yet??? 🕵️

Alokpoddar110 commented 2 years ago

please provide solution for this waiting or let us know how much time it will take , thanks in advance