Multiple database switching dynamic in node-express

Multiple database switching dynamic in node-express

i have searched lot to get a solution to my problem. but didn't got it. if anyone have the experience in such situations please help me.

i have searched lot to get a solution to my problem. but didn't got it. if anyone have the experience in such situations please help me.

i have created a application server in node express with MySQL a database. and successfully create REST API endpoints which works successfully.

but our projects scaled up. a new client approaches so we need to serve those clients too. those client may have 1k users.but the database schema is same.

solution 1: create a separate server and database for each client with different port no. but i don't think this is good solution because if we have 100 client we can't maintain the code base.

solution 2: create a separate database for each client and switch database connection at run time. but i don't understand how to implement solution 2. any suggestion highly appreciated.

if more than one client requesting same server how to know which database need to connect using the endpoint URL. i there any alternate way to tackle this situation.

my solution: create a middle ware to find out the which database is required and return the connection string.is it good idea.

middleware. in below example i use JWT token which contain database name.

const dbHelper=new db();

class DbChooser {

constructor(){

this. db=
    {
        wesa:{
            host: "xxx",
            user: "xxxx",
            password: "xxxxx",
            database: "hdgh",
            connectionLimit:10,
            connectTimeout:30000,
            multipleStatements:true,
            charset:"utf8mb4"
        },
        svn:{
            host: "x.x.x.x.",
            user: "xxxx",
            password: "xxx",
            database: "xxx",
            connectionLimit:10,
            connectTimeout:30000,
            multipleStatements:true,
            charset:"utf8mb4"
        }

    };

}

async getConnectiontring(req,res,next){ //console.log(req.decoded);

let d=new DbChooser();
let con=d.db[req.decoded.userId];
console.log(mysql.createPool(con));
next();

}

} module.exports=DbChooser;

javascript node-js angular express

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Angular 8 Node & Express JS File Upload

In this Angular 8 and Node.js tutorial, we are going to look at how to upload files on the Node server. To create Angular image upload component, we will be using Angular 8 front-end framework along with ng2-file-upload NPM package; It’s an easy to use Angular directives for uploading the files.

How to resumable File Upload in Angular and Node.js

While uploading a large file to server there is a very high chance of internet disconnection so giving a resumable option in upload will be very useful in uploading a large file to the server.

How to creating and managing sessions in Node.js and Express.js for Beginners

In this article, I am going to Creating and managing sessions in in Node.js. We will use express as a framework and various other modules such as body-parser to handle form data.

Создание сайта на Mongo DB, Express JS, Node JS и Angular

Видео курс по изучению стека MEAN. В курсе вы научитесь создавать сайты при помощи Node JS, Express JS, Angular JS и баз данных MongoDB. Вы ознакомитесь со всеми моментами разработки и в конце курса выгрузите сайт на удаленный сервер.