跳到主要内容

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

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

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图实体类型 扩展了基础实体静态类型,支持图结构操作的构造函数类型
GraphWhere图查询的节点过滤条件
IGraphEntity图实体接口 表示支持图结构操作的实体类型

Variables

VariableDescription
GRAPH_ENTITY_BASE_OPTIONS-
rxDBPluginGraph-

Functions

FunctionDescription
GraphEntity图实体装饰器 用于将类标记为图结构实体,并处理图特定的元数据, 生成 edges 表等
normalizeNeighborsOptions规范化邻居查询选项
normalizePathsOptions规范化路径查询选项