Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to dynamically connect to database in loopback #3886

Closed
rockyshukla opened this issue May 22, 2018 · 15 comments
Closed

how to dynamically connect to database in loopback #3886

rockyshukla opened this issue May 22, 2018 · 15 comments
Labels

Comments

@rockyshukla
Copy link

how I can switch database dynamically i.e connection of each database is stored in the master database. for eg:- if the user wants his database in Mumbai region then my code check whether Mumbai database exists in the master database if yes then make the connection to Mumbai database now all model should dynamically connect to Mumbai database.if not then I m passing error message that database does not exist. now problem is that my user model create method first connected to the master database and afterward it should connect to other databases if exists in master for eg:-

        User.beforeRemote('create', function (ctx, user, next) {
           var dataname=ctx.args.data.dbname  //this line passes db name eg:-mumbai
             User.find({where:{dbname:dataname}},function(err,res){ //this connect to master
               if(err){
                var err = new Error('Database Does not Exists');
                err.statusCode = 444;
                next(err)
                }
                  else{ 
                next();  //this should connected to other database if exist in master 
                  }
              })
         });

all other methods of user model should dynamically connect to Mumbai database how would I connect to database dynamically in above code and another thing

     "user": {
        "dataSource": "db",//what should be here i.e if connection is dynamically switch then it should be  here
           "public": true
          },
     "company": {
            "dataSource": "db",//what should be here i.e if connection is dynamically switch then it should be here 
           "public": true
            },

now ideas would be highly appreciated.

@rockyshukla rockyshukla changed the title how to dynamically connect to database after login in loopback how to dynamically connect to database in loopback May 22, 2018
@jannyHou
Copy link
Contributor

Hi @rockyshukla you can take a look of the solution in #1885.

@rockyshukla
Copy link
Author

rockyshukla commented May 29, 2018

@jannyHou I saw that solution but it does not work. let me explain this issue in detail

  1. when my app starts it will always connect to master database -- there is no issue

  2. in front end:- login page I have 3 input named as username, password, region. from region input, I
    will get database name -- there is no issue

    --- issue start here

  3. before login it will find database suppose named as A which is getting for region input as
    described in point 2 and search in the master database if it finds then connect to that database A

  4. after getting database A. it connects to database A login credentials exist in database A

  5. all model should connect to database A

@cineca-catalano
Copy link

I would like to implement multitenancy in the same way.
Have you solved it?

@rockyshukla
Copy link
Author

@cineca-catalano, no actually I did not get enough time to try but I got an idea how I can achieve that
here is an idea
before login, I m creating session i.e stored in pgsql master table. (storing session in pgsql or any kind of database you can use express session store for eg in my case I m using pgsql so i m going to use https://www.npmjs.com/package/connect-pg-simple )
after that, I going to create middleware which run before entering into modal and search for database credential in session if find it will connect to that database or else it stays connect to master eg:-db.attach(session)
at initial all modal connected to a master

@cineca-catalano
Copy link

@rockyshukla, what happens if multiple requests of different tenant ( different database) are processed concurrently?

@rockyshukla
Copy link
Author

@cineca-catalano yes it will processed concurrently if you found anything else please post solution here it will help other

@cineca-catalano
Copy link

Hi @rockyshukla,
I tried that solution but it fail when 2 request are processed concurrently.
For example:
Request 1 to model A (that need Database one)
Request 2 to model A (that need Database two)

if request 2 is made before request 1 has finished, that request 1 is made in database 2.
to switch the database I used attachTo function.

@rockyshukla
Copy link
Author

rockyshukla commented Jul 4, 2018

@cineca-catalano can you show me your sample code so i can know you issue, did you used middleware ware ?

@rockyshukla
Copy link
Author

@cineca-catalano did you find solution for this problem

@cata86
Copy link

cata86 commented Sep 6, 2018

Hi @rockyshukla,
yes I found a solution, I changed the loopback-connector-mongodb module ( we use mongodb as datasource).
On the context __ctx we added a clientId field.
On MongoDB.prototype.execute (lib/mongodb.js) function we make a new connection each time clientId change.

It is not optimal for the performances, the next step is to create a connection pool for each clientId.

@rockyshukla
Copy link
Author

@cata86 can you post some demo code (required code to switch db) it will help me as well as other and thanks for your patience

@DarikaShasai
Copy link

@cata86 hi, would you like to post the solution or part of your solution? i have the same problem and i really appreciated if you could help me.

@stale
Copy link

stale bot commented Nov 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 2, 2019
@stale
Copy link

stale bot commented Nov 16, 2019

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Nov 16, 2019
@vilasshetkar
Copy link

Anyone found solution on this.
I'm facing same issue. It's very urgent. Please anyone has solution please share code.

@cata86 would you like to post the solution or part of your solution? I have the same problem and I really appreciated if you could help me.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants