0

I'm setting up a new nodejs API which will deliver the data on the front end side as soon as the user click a "Search" button. I want to implement a functionality in which user is able to select which db to use i.e( Mysql or MongoDB). So how can I switch between both the database in a single API.

0

1 Answer 1

3

Well you can have instances of Mysql and MongoDb both on Node.js side. You will be sending selected db inside the API and based on the selected db you will use the instance. In this case you will have to implement both Mongo and Mysql queries.

if(selectedDb == "MongoDB"){
   // db.collection.find(query, projection)
   // use Mongo Logic here
}else{
    con.connect(function(err) {
      if (err) throw err;
      con.query("SELECT * FROM customers", function (err, result, fields) {
        if (err) throw err;
        console.log(result);
      });
    });
}

I hope it solves your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

i will try this approach. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.