How to configure two different datasource for a model in Strongloop Loopback framework?

*爱你&永不变心* 提交于 2019-12-01 02:20:42

问题


Our MySQL database are set up with Write clusters and Read clusters, is there a way to set up Strongloop Loopback Model (e.g. User) to Write to MySQL Host A and Read from MySQL Host B?


回答1:


Try to use attachTo() if you want to change datasource for a single model. For example

app.models.YourModel.attachTo(app.dataSources.readDS);
readData();
...
app.models.YourModel.attachTo(app.dataSources.writeDS);
writeData();

You will have to define readDS and writeDS datasources in your datasources.json file:

{
 "readDS": {
    "host": "hostA",    
    "database": "dbOnHostA",
    "username": "user",
    "password": "password",
    "name": "readDS",
    "connector": "mysql"
  },

 "writeDS": {
    "host": "hostB",
    "database": "dbOnHostB",
    "username": "user",
    "password": "password",
    "name": "writeDS",
    "connector": "mysql"
  }
}

Or you can create your datasources dynamically.




回答2:


In loopback 2.0, You can try overriding getDataSource method and based on context, you can return different dataSource. However in loopback 3.0, context has been removed, and options is not passed to getDataSource, so it will be a challenge to achieve perfection.




回答3:


You can define as many datasources as you wish as documented here

You should then be able to setup how you control the data by adding an ACL to control the access type. In this case READ or WRITE. Documentation on this can be found here



来源:https://stackoverflow.com/questions/31173302/how-to-configure-two-different-datasource-for-a-model-in-strongloop-loopback-fra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!