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.
1 Answer
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.
1 Comment
varun chauhan
i will try this approach. Thanks for the help