如何在Strongloop Loopback框架中为模型配置两个不同的数据源? [英] How to configure two different datasource for a model in Strongloop Loopback framework?
本文介绍了如何在Strongloop Loopback框架中为模型配置两个不同的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们的MySQL数据库设置有Write集群和Read集群,是否可以设置Strongloop回送模型(例如,用户)以写入MySQL主机A并从MySQL主机B读取?
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?
推荐答案
如果要更改单个模型的数据源,请尝试使用attachTo().例如
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();
您将必须在datasources.json文件中定义readDS和writeDS数据源:
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.
这篇关于如何在Strongloop Loopback框架中为模型配置两个不同的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文