loopbackjs: Attach a model to different datasources

前端 未结 3 1595
予麋鹿
予麋鹿 2021-01-02 16:14

I have defined several models that use a Datasource \"db\" (mysql) for my environment.

Is there any way to have several datasources attached to those models, so I wo

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-02 17:08

    By default, you can only attach data sources on a per-model basis. Meaning you can attach each model to a different data source via datasources.json.

    For your use case, you will to add a remote hook to each endpoint you want for multiple data sources. In your remote hook, you will do something like:

    ...
    var ds1 = Model.app.dataSources.ds1;
    var ds2 = Model.app.dataSources.ds2;
    
    //some logic to pick a data source
    if (context.req.params...
    ...
    

    See http://docs.strongloop.com/display/LB/Remote+hooks for more info.

提交回复
热议问题