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, SqliteGraphRepository } from '@aiao/rxdb-plugin-graph';
@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() 返回非循环路径及对应边信息。
Fileoverview
RxDB Graph Plugin 图数据库插件,提供图结构数据存储和查询能力
主要功能:
- 图结构实体支持(GraphEntity)
- 邻居节点查询(findNeighbors)
- 路径查询(findPaths)
- 边管理(addEdge/removeEdge)
Classes
| Class | Description |
|---|---|
| GraphEntityBase | 图实体装饰器配置 定义了图结构所需的边关系和遍历能力 |
| GraphRepository | 图结构实体仓库 提供图查询能力:邻居节点查询、路径查询等 |
| RxDBPluginGraph | RxDB 插件接口 |
| SqliteGraphRepository | SQLite 图实体仓库 实现基于 SQLite 递归 CTE 的图查询 |
Interfaces
| Interface | Description |
|---|---|
| CountNeighborsQuery | CountNeighbors 查询任务 统计指定节点的邻居节点数量 |
| EdgeFilterOptionsFull | 边过滤条件(权重 + 属性) |
| EdgeFilterOptionsWithProperties | 边过滤条件(带属性) |
| EdgeFilterOptionsWithWeight | 边过滤条件(带权重) |
| EdgeInfo | 边信息(基础) |
| EdgeInfoFull | 边信息(权重 + 属性) |
| EdgeInfoWithProperties | 边信息(带属性) |
| EdgeInfoWithWeight | 边信息(带权重) |
| FindNeighborsOptions | 图邻居查询选项(基础) |
| FindNeighborsQuery | FindNeighbors 查询任务 查找指定节点的邻居节点 |
| FindPathsOptions | 图路径查询选项(基础) |
| FindPathsQuery | FindPaths 查询任务 查找两个节点之间的所有路径 |
| GraphPath | 图路径结果 |
| IGraphRepository | 图结构仓库接口(基础) |
| NeighborResult | 图邻居查询结果(基础) |
Type Aliases
| Type Alias | Description |
|---|---|
| EdgeFilterOptions | 边过滤条件基础接口(无权重、无属性) |
| GraphEdgeInfoType | - |
| GraphEdgeProperties | - |
| GraphEdgePropertiesRecord | - |
| GraphEdgePropertyValue | 边属性值类型 |
| GraphEntityType | 图实体类型 扩展了基础实体静态类型,支持图结构操作的构造函数类型 |
| GraphWhere | 图查询的节点过滤条件 |
| IGraphEntity | 图实体接口 表示支持图结构操作的实体类型 |
Variables
| Variable | Description |
|---|---|
| GRAPH_ENTITY_BASE_OPTIONS | - |
| rxDBPluginGraph | - |
Functions
| Function | Description |
|---|---|
| GraphEntity | 图实体装饰器 用于将类标记为图结构实体,并处理图特定的元数据, 生成 edges 表等 |
| normalizeNeighborsOptions | 规范化邻居查询选项 |
| normalizePathsOptions | 规范化路径查询选项 |