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

3

7

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?

Serb answered 1/7, 2015 at 23:30 Comment(0)
S
10

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.

Shanghai answered 3/7, 2015 at 13:25 Comment(6)
Thanks. This should work as it changes the datasource of the model before you save. The only trouble part is that you have to always set it before you save or read just to make sure you are hitting the right datasource. There is actually no way to defined different read/write source in model-config.json right now.Serb
I haven't tested this scenario if a REST call for Create and it switched to WriteDB and a REST call for READ come in. Would this cause the Read REST to read from the wrong DB?Serb
#5153992 answers my question above. So it is safe to switch database attachment.Serb
@MianLeow some operations may trigger async operations which can cause unwanted behaviour. for example if you are doing a find operation with include(relations) your operation is not atomic anymore because it has async operation in it.Goshorn
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?Saccharin
This answer https://mcmap.net/q/1480398/-strongloop-loopback-change-connection-string-based-on-route-value says that concurrency's still gonna be an issue.Tempe
H
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.

Haemo answered 19/5, 2017 at 11:42 Comment(0)
S
-1

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

Swashbuckler answered 2/7, 2015 at 18:2 Comment(1)
I know you can create as many datasources in datasources.json but i can only attach one datasource to a particular model in model-config.json. I was interested in figuring out how to define different datasource for WRITE and READ for the same Model.Serb

© 2022 - 2024 — McMap. All rights reserved.