1

I want to have 2 models with same name (Account in this case) in the same LoopBack app but mounted at different points:

For example: http://localhost:3000/api1/Accounts and http://localhost:3000/api2/Accounts ? Is it possible to do this?

I tried requiring two LoopBack objects, mounting them at different paths in the app and creating the models in them; but when I register the model with the app, the last call to app.model is the one which takes effect. Is there a way to work around this ?

var loopback = require('loopback');
var loopback2 = require('loopback');
var app = loopback();
app.use('/api1', loopback.rest());
app.use('/api2', loopback2.rest());
app.listen(3000);

app.dataSource('testMySql', dsConfig);
var config = { dataSource: 'testMySql'};  

var newmodel = loopback.createModel(model);  
app.model(newmodel, config);

newmodel2 = loopback2.createModel(model);  
app.model(newmodel2, config);
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
SubuKris
  • 71
  • 2
  • Hmm... I'm not sure it would work the way you have it, but are you just trying to [version your API](http://docs.strongloop.com/display/public/LB/Versioning+your+API)? If not, maybe it would just be best to use a more distinct name? Another option would be to really have two different applications (different `server.js` files) and simply reuse some of the modules between them. – Jordan Kasper Mar 12 '15 at 17:45
  • Hi, Thanks very much for the response. I'm not trying to version the API here. I'm trying to handle the case where I could have the same table name (eg. Account) in different databases or even under different schemas in the same database. – SubuKris Mar 16 '15 at 05:49
  • Well, I don't think you can have two models with the exact same name in the LoopBack app, so you might have to "namespace" them like: `Table1Account` and `Table2Account`. Not exactly sure the best way to do that though. – Jordan Kasper Mar 16 '15 at 12:52

0 Answers0