问题描述
我定义了几个模型,这些模型在我的环境中使用数据源"db"(mysql).
I have defined several models that use a Datasource "db" (mysql) for my environment.
有什么办法可以将多个数据源附加到那些模型上,这样我就可以对不同的数据库执行REST操作?
Is there any way to have several datasources attached to those models, so I would be able to perform REST operations to different databases?
即: GET/api/Things?ds ="db"
i.e: GET /api/Things?ds="db"
获取/api/Things?ds ="anotherdb"
GET /api/Things?ds="anotherdb"
获取/api/Things(将使用默认ds)
GET /api/Things (will use default ds)
推荐答案
如上面@superkhau所述,每个LoopBack模型只能附加到单个数据源.
As @superkhau pointed above, each LoopBack Model can be attached to a single data-source only.
您可以为要使用的每个数据源创建(子类)新模型.然后,您可以通过唯一的REST URL公开这些按数据源的模型,也可以实现包装器模型,该包装器将方法分配给正确的特定于数据源的模型.
You can create (subclass) a new model for each datasource you want to use. Then you can either expose these per-datasource models via unique REST URLs, or you can implement a wrapper model that will dispatch methods to the correct datasource-specific model.
在我的示例中,我将展示如何为附加到db
和anotherdb
的Car
模型公开每个数据源模型. Car
模型以通常的方式通过common/models/car.json
和common/models/car.js
定义.
In my example, I'll show how to expose per-datasource models for a Car
model that is attached to db
and anotherdb
. The Car
model is defined in the usual way via common/models/car.json
and common/models/car.js
.
现在您需要定义每个数据源模型:
Now you need to define per-datasource models:
// common/models/car-db.js
{
"name": "Car-db",
"base": "Car",
"http": {
"path": "/cars:db"
}
}
// common/models/car-anotherdb.js
{
"name": "Car-anotherdb",
"base": "Car",
"http": {
"path": "/cars:anotherdb"
}
}
// server/model-config.json
{
"Car": {
"dataSource": "default"
},
"Car-db": {
"dataSource": "db"
},
"Car-anotherdb": {
"dataSource": "anotherdb"
}
}
现在您可以使用以下URL:
Now you have the following URLs available:
GET /api/Cars:db
GET /api/Cars:anotherdb
GET /api/Cars
上面概述的解决方案有两个局限性:您必须为每个数据源定义一个新模型,并且无法使用查询参数选择该数据源.
The solution outlined above has two limitations: you have to define a new model for each datasource and the datasource cannot be selected using a query parameter.
要解决此问题,您需要使用其他方法.我再次假设已经定义了Car
模型.
To fix that, you need a different approach. I'll again assume there is a Car
model already defined.
现在您需要创建一个调度程序".
Now you need to create a "dispatcher".
// common/models/car-dispatcher.json
{
"name": "CarDispatcher",
"base": "Model", //< important!
"http": {
"path": "/cars"
}
}
// common/models/car-dispatcher.js
var loopback = require('loopback').PersistedModel;
module.exports = function(CarDispatcher) {
Car.find = function(ds, filter, cb) {
var model = this.findModelForDataSource(ds);
model.find(filter, cb);
};
// a modified copy of remoting metadata from loopback/lib/persisted-model.js
Car.remoteMethod('find', {
isStatic: true,
description: 'Find all instances of the model matched by filter from the data source',
accessType: 'READ',
accepts: [
{arg: 'ds', type: 'string', description: 'Name of the datasource to use' },
{arg: 'filter', type: 'object', description: 'Filter defining fields, where, orderBy, offset, and limit'}
],
returns: {arg: 'data', type: [typeName], root: true},
http: {verb: 'get', path: '/'}
});
// TODO: repeat the above for all methods you want to expose this way
Car.findModelForDataSource = function(ds) {
var app = this.app;
var ds = ds && app.dataSources[ds] || app.dataSources.default;
var modelName = this.modelName + '-' + ds;
var model = loopback.findModel(modelName);
if (!model) {
model = loopback.createModel(
modelName,
{},
{ base: this.modelName });
}
return model;
};
};
最后一点是删除Car
并在模型配置中使用CarDispatcher
:
The final bit is to remove Car
and use CarDispatcher
in the model config:
// server/model-config.json
{
"CarDispatcher": {
dataSource: null,
public: true
}
}
这篇关于loopbackjs:将模型附加到不同的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!