跳到主要内容

rxdb-plugin-graph

Implements: US-503 图数据插件

RxDB 图数据插件,提供有向/无向图、可选边权重与属性、邻居查询和路径查询。

安装​

pnpm add @aiao/rxdb @aiao/rxdb-adapter-wa-sqlite @aiao/rxdb-plugin-graph

使用​

import { PropertyType, RxDB, SyncType } from '@aiao/rxdb';
import { RxDBAdapterWaSqlite } from '@aiao/rxdb-adapter-wa-sqlite';
import { GraphEntity, GraphEntityBase, rxDBPluginGraph } from '@aiao/rxdb-plugin-graph';
// SQLite 实现从子路径导入 —— 根入口不再强制拉入它,
// 只用 PGlite / Supabase 时不会为此付出打包体积(GRAPH-010)
import { SqliteGraphRepository } from '@aiao/rxdb-plugin-graph/sqlite';

@GraphEntity({
name: 'Person',
properties: [{ name: 'name', type: PropertyType.string }],
features: `{ graph: { type: 'directed-graph', weight: true } }`
})
class Person extends GraphEntityBase {
name!: string;
}

const rxdb = new RxDB({
dbName: 'social',
entities: [Person],
sync: `{ type: SyncType.None, local: { adapter: 'wa-sqlite' } }`
});

rxdb.use(rxDBPluginGraph).adapter(
'wa-sqlite',
db =>
new RxDBAdapterWaSqlite(db, {
vfs: 'MemoryAsyncVFS',
async: true,
wasmPath: '/wa-sqlite/wa-sqlite-async.wasm',
repositories: `{ GraphRepository: SqliteGraphRepository }`
})
);

await rxdb.connect('wa-sqlite');
rxdb.init();

const alice = new Person({ name: 'Alice' });
const bob = new Person({ name: 'Bob' });
await rxdb.entityManager.saveMany([alice, bob]);
await Person.addEdge(alice, bob, 1);

Person.findNeighbors$({ entityId: alice.id, direction: 'out', level: 1 }).subscribe(neighbors => {
console.log(neighbors[0]?.node.name);
});

level=0 不返回邻居。findNeighbors()、countNeighbors() 和 findPaths() 保留 Promise API;对应的 findNeighbors$()、countNeighbors$() 和 findPaths$() 返回会随节点或边变更刷新的 Observable。 邻居查询不包含起始节点,路径查询返回非循环路径及对应边信息。邻居与路径结果默认最多返回 1000 条,调用方可通过 limit 调低或提高,上限为 10000;结果超过 limit 或路径搜索触及内部资源上限时,数组的 truncated 为 true。

连接纪元​

插件声明 lifecycle: 'scoped',唯一的宿主改动——注册 GraphRepository——登记在 install(scope) 收到的作用域上,disconnectAll() 时随作用域一起撤销,宿主不会调用 destroy()。重新 connect() 会重新注册。改造前这条注册没人管,断连后仓库仍挂在 rxdb 上、指向的却是一个已经拆掉的纪元。

Fileoverview​

RxDB Graph 插件 图数据库插件,提供图结构数据存储和查询能力

主要功能:

  • 图结构实体支持(GraphEntity)
  • 邻居节点查询(findNeighbors)
  • 路径查询(findPaths)
  • 边管理(addEdge/removeEdge)

Classes​

ClassDescription
GraphEntityBase图实体装饰器配置 定义了图结构所需的边关系和遍历能力
GraphRepository图结构实体仓库 提供图查询能力:邻居节点查询、路径查询等
RxDBPluginGraphRxDB 插件接口

Interfaces​

InterfaceDescription
CountNeighborsQueryCountNeighbors 查询任务 统计指定节点的邻居节点数量
EdgeFilterOptionsFull边过滤条件(权重 + 属性)
EdgeFilterOptionsWithProperties边过滤条件(带属性)
EdgeFilterOptionsWithWeight边过滤条件(带权重)
EdgeInfo边信息(基础)
EdgeInfoFull边信息(权重 + 属性)
EdgeInfoWithProperties边信息(带属性)
EdgeInfoWithWeight边信息(带权重)
FindNeighborsOptions图邻居查询选项(基础)
FindNeighborsQueryFindNeighbors 查询任务 查找指定节点的邻居节点
FindPathsOptions图路径查询选项(基础)
FindPathsQueryFindPaths 查询任务 查找两个节点之间的所有路径
GraphPath图路径结果
IGraphRepository图结构仓库接口(基础)
NeighborResult图邻居查询结果(基础)

Type Aliases​

Type AliasDescription
EdgeFilterOptions边过滤条件基础接口(无权重、无属性)
GraphEdgeInfoType-
GraphEdgeProperties-
GraphEdgePropertiesRecord-
GraphEdgePropertyValue边属性值类型
GraphEntityType图实体类型 扩展了基础实体静态类型,支持图结构操作的构造函数类型
GraphQueryResult带资源截断状态的图查询数组。
GraphWhere图查询的节点过滤条件
IGraphEntity图实体接口 表示支持图结构操作的实体类型

Variables​

VariableDescription
GRAPH_DEFAULT_MAX_DEPTH路径查询默认最大深度
GRAPH_DEFAULT_RESULT_LIMIT图查询默认最多返回的结果数。
GRAPH_ENTITY_BASE_OPTIONS图实体基类的元数据选项
GRAPH_MAX_DEPTH路径查询最大深度限制
GRAPH_MAX_LEVEL图查询最大层级 防止递归查询层级过深导致性能问题
GRAPH_MAX_PATH_EXPANSIONS单次路径查询允许加入递归 CTE 的最大行数。
GRAPH_MAX_RESULT_LIMIT图查询允许调用方请求的结果数硬上限。
GRAPH_QUERY_TYPES图查询任务类型集合 用于 merge 操作中快速判断是否为图相关查询
rxDBPluginGraph-

Functions​

FunctionDescription
createGraphQueryResult给数组附加不可枚举的截断状态,保持既有数组比较与序列化行为。
GraphEntity图实体装饰器 用于将类标记为图结构实体,并处理图特定的元数据, 生成 edges 表等
normalizeNeighborsOptions规范化邻居查询选项
normalizePathsOptions规范化路径查询选项