Need a special offer?Find out if your project fits.
+

MongoDB Connector - dynamically pass database name

Answered
Ravi Kaikini asked on August 22, 2022

Is there a way to dynamically pass the database name from the frontend to the Node MongoDB connector vs having to set it statically in the node connector ?

6 answers

Public
Maksym Diachenko Maksym Diachenko Flexmonster August 25, 2022

Hello, Ravi!

Our apologies for the late response.

By default, the flexmonster-mongo-connector only supports setting the collection name on the client side. This can be done by specifying the index property of the dataSource object. However, it is possible to implement a workaround, allowing you to get the database name from the frontend.

You can use the dataSource.index property to pass the database and collection names. We suggest using some delimiter character (it can be;,/,,, etc.) between them, so it is easy to split the `dataSource.index` into two variables:

"dataSource": {
"type": "api",
"url": "http://localhost:9204/mongo",
"index": "fm-product-sales;flexmonster"
}

Then, you should create the connection pool object, where all the database connections will be stored on a server. If the connection was already established, the existing connection to the database is used. Otherwise, a new connection is created.

Please see the example of the /fields request, which connects to a database specified in the dataSource.index property:

let dboList: any = {};

mongo.post("/fields", async (req: Request, res: Response) => {
    try {
//Retrieve the "database" and "collection" from the "index"
        let connectionString = req.body.index;
        let collection = connectionString.split(";")[0];
        let database = connectionString.split(";")[1];
        let result;
//Check if connection was already established
        if (dboList[database]) {
//Get fields from the database
            result = await _apiReference.getSchema(dboList[database], collection);
            res.json(result);
        } else {
//Connect to the database and get fields from it
            await MongoClient.connect('mongodb://read:only@olap.flexmonster.com:27017',
                async (err, db) => {
                    if (err) throw err;
                    let DBO = db.db(database);
                    dboList[database] = DBO;
                    result = await _apiReference.getSchema(dboList[database], collection);
                    res.json(result);
                });
        } 
    } catch (err) {
        handleError(err, res);
    }
});

After the /fields request, there is no need to check if the connection to the database is already established.

Please let us know if you have any additional questions.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster September 1, 2022

Hello, Ravi!

Hope you are doing well.
We would like to know if you had time to check the provided solution for passing the database name into Flexmonster MongoDB Connector.
Please let us know if everything works for you.

Best Regards,
Maksym

Public
Ravi Kaikini September 1, 2022

Hi Maksym, sorry for the radio silence. Conceptually I think this will work and appreciate your help and time. I have sent this over to our frontend team and waiting for feedback though and will report back as soon as I hear from them. Thanks again.

Public
Maksym Diachenko Maksym Diachenko Flexmonster September 2, 2022

Hi, Ravi!

Thank you for your reply.
We are looking forward to hearing feedback from your developer team.

Best Regards,
Maksym

Public
Ravi Kaikini September 6, 2022

Hi Maksym,
I think this should work for us. Appreciate the help. 
I will reach out if we have any further questions.
Regards,
Ravi

Public
Nadia Khodakivska Nadia Khodakivska Flexmonster September 7, 2022

Hi, Ravi,

Thank you for the response.

We are glad to hear that the suggested solution works for you!

As always, feel free to contact us if other questions arise.

Kind regards,
Nadia

Please login or Register to Submit Answer