5

I have created a mixin in which I am trying to override the getDataSource method of all the models like:

Model.getDataSource = function() {
   console.log('Overriding the getDataSource for model ' + Model.modelName);
   // Code to attach the new datasource based on some logic by
   // invoking Model.attachTo(datasourcename) method
}

I have models created which have datasource set to "db" initially. When I make a request to the application it should attach the datasource dynamically to the models. I am not sure what I am doing wrong above as above method is never invoked for the models which I have created initially but it gets invoked for all dynamically created models(using createModel method).

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Satyendra
  • 51
  • 4
  • What are you trying to do ultimately, and why? There may be a better method for doing so. – Jordan Kasper Jan 11 '16 at 21:42
  • I was trying to switch the datasource dynamically based on the request from client. ![Switching of ds](https://openmerchantaccount.com/img2/img_so_que_sat.png). But later I realized that this won't work since the models are shared across different api calls. For e.g if I do a api call from client C1 which uses model M1 leading to switching datasource to DS1, and at the same time do a call from client C2 which also uses model M1 switching the datascource to DS2. So for now I am deploying different apps to same process using strongloop process manager. Each app has its own datasource now. – Satyendra Mar 29 '16 at 08:10
  • @Satyendra I also came across similar problem. Did you manage to find any workaround? – Mukesh Soni Jun 23 '16 at 15:58
  • 1
    @MukeshSoni I was not able to find a solution for this. So I am now running multiple loopback apps using nginx. Please see this [solution](http://stackoverflow.com/questions/22678387/how-to-run-multiple-strongloop-loopback-apps-on-the-same-server/22680005#22680005) – Satyendra Jun 28 '16 at 06:36
  • actually I am also doing the same thing. Switching the db before request but context switching during run-time is causing issues. If we make two request at same time for different databases, server is throwing 404 because it looks in different db that the one it should. Did you faced this issue ? or do you have any solutions to this problem? – gauravmehla Aug 16 '18 at 09:48

0 Answers0