RPC代理的创建是为了将您的数据分割成微服务,结合一个真实的代理和RPC的数据源, 因为它是一个正常的代理.
安装
- 安装软件包
@forestadmin-experimental/rpc-agent。 。 。 . - 提供像真代理一样的选项.
const { createRpcAgent } = require('@forestadmin-experimental/rpc-agent');
const agent = createRpcAgent({
authSecret: process.env.FOREST_AUTH_SECRET,
isProduction: process.env.NODE_ENV === 'production',
loggerLevel: 'Info',
});
// use the agent like a real one.
处理嵌入式 RPC 数据源和关系
在RPC剂内使用RPC数据源时要谨慎. 由于RPC的代理行为与真实的类似,因此RPC的数据源创建并使用一个计划. 因此,它所增加的所有藏品都应在这个计划之内,这可能造成重复问题.
与 removeCollection,则 markCollectionsAsRpc 函数从计划中删除收藏,但保留定义的关系。 如果在标记为RPC的收藏上声明关系,应当使用 提供的插件 在进行调节的网关上.
实例
鉴于这一结构:
- 一个RPC代理用于管理用户.
- 一个RPC代理公司用来管理Groups.
- 一个RPC代理用来管理项目.
- 一个特工作为网关.
案件1 宣布网关内的关系
照常声明你们的关系如下:
gateway
.createRpcDataSource(/*from the user RPC agent*/)
.createRpcDataSource(/*from the project RPC agent*/)
.createRpcDataSource(/*from the group RPC agent*/)
.customizeCollection('user', collection => {
collection.addManyToOneRelation('group', 'group', {
foreignKey: 'group_id',
});
})
.customizeCollection('project', collection => {
collection.addManyToOneRelation('group', 'group', {
foreignKey: 'group_id',
});
})
.customizeCollection('group', collection => {
collection
.addOneToManyRelation('users', 'user', {
originKey: 'group_id',
})
.addOneToManyRelation('projects', 'project', {
originKey: 'group_id',
});
});
案件2使用RPC数据源在RPC代理内部
在网关上使用数据源之前,您应当合并数据源。 在此示例中,您只使用用户代理中的组代理,而不使用多处代理. 因此,没有必要使用 markCollectionsAsRpc,如下所示:
/* define user agent and use group agent as a RPC data source
* define some relationship between both data source
*/
userAgent
.createcreateSqlDataSource(...)
.createRpcDataSource(/*from the group RPC agent*/)
.customizeCollection('user', collection => {
collection.addManyToOneRelation('group', 'group', {
foreignKey: 'group_id',
});
})
.customizeCollection('group', collection => {
collection.addOneToManyRelation('users', 'user', {
originKey: 'group_id',
});
});
/* define the gateway and use agragated agent
* all collection and relationship defined in the user agent will be import as a datasource
*/
gateway
.createRpcDataSource(/*from the user RPC agent*/)
.createRpcDataSource(/*from the project RPC agent*/)
案例2 宣布RPC数据源在RPC代理上的关系
如果数据源关系和用途复杂,请查看如何使用 markCollectionsAsRpc 编辑 reconciliateRpc 下面的插件 .
/* define user agent and use group agent as a RPC data source
* define some relationship between both data source
*/
userAgent
.createcreateSqlDataSource(...)
.createRpcDataSource(/*from the group RPC agent*/)
.customizeCollection('user', collection => {
collection.addManyToOneRelation('group', 'group', {
foreignKey: 'group_id',
});
})
.customizeCollection('group', collection => {
collection.addOneToManyRelation('users', 'user', {
originKey: 'group_id',
});
})
.markCollectionsAsRpc('group');
/* define project agent and use group agent as a RPC data source
* define some relationship between both data source
*/
projectAgent
.createcreateSqlDataSource(...)
.createRpcDataSource(/*from the group RPC agent*/)
.customizeCollection('project', collection => {
collection.addManyToOneRelation('group', 'group', {
foreignKey: 'group_id',
});
})
.customizeCollection('group', collection => {
collection.addOneToManyRelation('projects', 'project', {
originKey: 'group_id',
});
})
.markCollectionsAsRpc('group');
// define the gateway
gateway
.createRpcDataSource(/*from the user RPC agent*/)
.createRpcDataSource(/*from the project RPC agent*/)
.createRpcDataSource(/*from the group RPC agent*/)
.use(reconciliateRpc);
处理禁用Search定制
来做 disableSearch 网关上的定制工作,请使用 提供的插件
实例
/* define project agent and disable the search behavior on the collection
*/
projectAgent
.createcreateSqlDataSource(...)
.customizeCollection('project', collection => {
collecion.disableSearch();
});
// define the gateway
gateway
.createRpcDataSource(/*from the project RPC agent*/)
.use(reconciliateRpc);