@forestadmin-experimental/rpc-agent

中文文档
2.11.0发布于 2026/10/26GPL-3.0TypeScriptCommonJS构建来源已验证近 30 天下载 3341

创建 RPC 代理是为了将您的数据分割为微服务,结合一个真实的代理和一个 RPC 数据源,您可以将您的收藏全部输入,因为这是一个正常的代理.

中文使用文档
文档目录5 节
  1. 实例
  2. 案件1 宣布网关内的关系
  3. 案件2使用RPC数据源在RPC代理内部
  4. 案例2 宣布RPC数据源在RPC代理上的关系
  5. 实例

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);

版本与发布记录

@forestadmin-experimental/rpc-agent 共发布 20 个版本,首次发布于 2024/07/10;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.11.0latest2026/10/2620 天前
2.10.12026/06/164 个月前
2.10.02026/05/275 个月前
2.9.22026/05/215 个月前
2.9.12026/05/066 个月前
2.9.02026/03/277 个月前

关于 @forestadmin-experimental/rpc-agent 的常见问题

@forestadmin-experimental/rpc-agent 是什么?

创建 RPC 代理是为了将您的数据分割为微服务,结合一个真实的代理和一个 RPC 数据源,您可以将您的收藏全部输入,因为这是一个正常的代理.

如何安装 @forestadmin-experimental/rpc-agent?

在项目目录运行 npm install @forestadmin-experimental/rpc-agent,或使用 pnpm add / yarn add / bun add。

@forestadmin-experimental/rpc-agent 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@forestadmin-experimental/rpc-agent 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@forestadmin-experimental/rpc-agent 可以商用吗?

@forestadmin-experimental/rpc-agent 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@forestadmin-experimental/rpc-agent 的下载量有多少?

根据 npm 官方下载统计,@forestadmin-experimental/rpc-agent 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,341 次,日均约 111 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@forestadmin-experimental/rpc-agent 还在维护吗?

最新版本 2.11.0 发布于 2026/10/26(20 天前);至今共发布 20 个版本,其中过去 12 个月发布了 7 个;首次发布于 2024/07/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。