I have a doubt about the data sources: Context: Currently I'm working on a project where my API uses two datasources: A and B .
Sometimes the datasource B has troubles and is not available while A is always available. When B is not available the whole web service collapses.
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?
Note: I'm working with Loopback 3
lazyConnect:true
Will defer connection until you query a model attached to it, and send the client an error without crashing the server if the connection fails.
"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?
You can use your datasource's events to know when to swap models. Here is something I tested briefly.
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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.