即使数据源在环回 3 上不可用,如何使 api 保持活动状态?

How to keep the api alive even if a datasource is not available on loopback 3?

我对数据来源​​有疑问: 上下文:目前我正在做一个项目,我的 API 使用两个数据源:AB.

有时数据源 B 有问题并且不可用,而 A 始终可用。 当 B 不可用时,整个网络服务崩溃。

我的问题:有什么方法可以对 api 进行编程,使其在数据源 B 无法访问?

注意:我正在使用 Loopback 3

lazyConnect:true 将延迟连接,直到您查询附加到它的模型,并在连接失败时向客户端发送错误信息而不会导致服务器崩溃。

  "myDatasource": {
    "name": "myDatasource",
    "host": "ds.com",
    "database": "db",
    "username": "root",
    "password": "",
    "connector": "postgres",
    "lazyConnect": true
  },

My question: Is there any way to program the api to keep it working with the part that only evolves the datasource A when the datasource B is not accessible?

您可以使用数据源的事件来了解何时交换模型。这是我简单测试过的东西。

server/boot/swap.js

function swapModelDatasource(app, model, ds) {
    const name = model.name;
    app.deleteModelByName(name);
    const m = app.model(ds.createModel(name, model.definition.properties, {
        settings: model.settings,
        relations: model.settings.relations,
        acls: model.settings.acls
    }));
}

module.exports = app => {
    const ds1 = app.datasources.aws;
    const m = app.models.Node;
    ds1.on('connected', () => swapModelDatasource(app, m, ds1));
}