Nodejs Select database when logging in

Asked

Viewed 44 times

-2

Hello, good afternoon.

I’m dealing with a project that will work as a service, the client has some knowledge in IT and requested that users can select the database when logging in, for example:

On the login screen besides the user and password, there would be a dropdown with the list of companies, when I went to log in I would pick company X and everything I did would be stored in company X database. When company Y user logs in it would be the same way so that same application connects to N databases.

the reason is that in addition to providing service it will give daily backups to customers and will also give access to the bank.

My question is: Is it possible to do this with Nodejs? is it feasible from a performance point of view? (taking into account that it would be a single instance of the application running)

1 answer

1


Yes it is possible in many ways! But let’s abstract which bank and talk about architecture.

I create a database with the connection information of each COMPANY. Can include which DB to use, passwords, connectionstrings..

If I think about BACKEND abstracting the front, we need to think that each request for API vc should have all the information necessary to know which BASE vc should make the queries.

So when doing a LOGIN on the platform, I would create a JWT with some identifier from which base the user is consulting, a more practical idea.

Example of a JWT (https://jwt.io/)

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJRRzIiLCJpYXQiOjE1Nzg0NDc1NTcsImV4cCI6MTYwOTk4MzU1NywiYXVkIjoicWcyLmNvbS5iciIsInN1YiI6ImFydGh1ckBxZzIuY29tLmJyIiwidXVpZCI6IjM4OTk0ZTg2LWNhMTktNGYyMi04MTM0LTIxN2MxYTUzZjM5MCJ9.bJWf_Rk0eG2Kp-TVxENCI-QQLr2CDQzvTnsVEKaes2A

If you paste this JWT in the site reported above has the following result

{
 "iss": "QG2",
 "iat": 1578447557,
 "exp": 1609983557,
 "aud": "qg2.com.br",
 "sub": "[email protected]",
 "uuid": "38994e86-ca19-4f22-8134-217c1a53f390"
}

uuid would be the identifier in my database where I know it is COMPANY X and there I have information to connect in the database, and do the necessary query or INSERT finally.

You can create a class in Nodejs that receives the UUID and there she solves it for you ;)

I hope I have help

abs!

  • Thank you very much, Arthur. I was having difficulty even to research, I did not know very well nor how to do the search, his answer helped to open my rsrs horizons. It is an apparently simple solution to implement and solves the problem well. Again thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.