IGraphRepository<T, V, EdgeInfoType>
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:335
图结构仓库接口(基础)
Extends
IRepository<T>
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends EntityType | - |
V extends EdgeFilterOptions | EdgeFilterOptionsFull |
EdgeInfoType | GraphEdgeInfoType<V> |
Methods
addEdge()
addEdge(
from,
to,
weight?,
properties?
): Promise<void>;
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:443
添加边
Parameters
| Parameter | Type | Description |
|---|---|---|
from | InstanceType<T> | 起始节点 |
to | InstanceType<T> | 目标节点 |
weight? | number | null | 边权重(需要在元数据中启用 features.graph.weight) |
properties? | | GraphEdgeProperties<V> | null | 边属性(需要在元数据中启用 features.graph.properties) |
Returns
Promise<void>
Example
await User.addEdge(userA, userB, 8, {
category: 'colleague',
since: '2024-01-01'
});
count()
count(options): Promise<number>;
Defined in: rxdb/dist/repository/repository.interface.d.ts:33
查询实体数量
Parameters
| Parameter | Type | Description |
|---|---|---|
options | EntityStaticType<T, "countOptions"> | 查询选项(详见 CountOptions) |
Returns
Promise<number>
满足条件的行数
Inherited from
countNeighbors()
countNeighbors(options): Promise<number>;
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:394
统计邻居节点数量
Parameters
| Parameter | Type |
|---|---|
options | FindNeighborsOptions<T, GraphWhere<T>, V> |
Returns
Promise<number>
邻居数量(不包含起始节点)
Remarks
统计规则与 findNeighbors 一致,但性能更高(无需返回完整节点数据)
Example
// 统计好友数量
const count = await User.countNeighbors({
entityId: 'user1',
level: 1
});
create()
create(entity): Promise<InstanceType<T>>;
Defined in: rxdb/dist/repository/repository.interface.d.ts:40
创建实体
Parameters
| Parameter | Type | Description |
|---|---|---|
entity | InstanceType<T> | 实体实例(通常由 EntityManager.instantiate 创建) |
Returns
Promise<InstanceType<T>>
持久化完成后的实体;与入参不是同一引用(实现可换新实例)
Inherited from
find()
find(options): Promise<InstanceType<T>[]>;
Defined in: rxdb/dist/repository/repository.interface.d.ts:26
查询多个实体
Parameters
| Parameter | Type | Description |
|---|---|---|
options | EntityStaticType<T, "findOptions"> | 查询选项(详见 FindOptions) |
Returns
Promise<InstanceType<T>[]>
满足条件的实体数组;无匹配返回空数组(不抛错)
Remarks
返回的是实体实例(带状态机),不是裸数据 —— 调用方后续 save() / remove()
等操作会沿用同一个状态对象,避免"查询回来就丢上下文"。
Inherited from
findNeighbors()
findNeighbors(options): Promise<GraphQueryResult<NeighborResult<T, EdgeInfoType>>>;
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:373
查询邻居节点(带边信息)
Parameters
| Parameter | Type |
|---|---|
options | FindNeighborsOptions<T, GraphWhere<T>, V> |
Returns
Promise<GraphQueryResult<NeighborResult<T, EdgeInfoType>>>
邻居节点数组,每个元素包含节点和边信息
Remarks
- 返回结果不包含起始节点(遍历携带 visited path,存在环/回边时也不会回流)
- 结果按节点去重:同一节点通过不同路径到达时只保留最小 level; 同一最短层级存在多条边时,返回 weight 最小的边(无权图按边端点 id 稳定选取)
- 默认按 level 升序、weight 升序、节点 id 升序排序
Example
// 查询直接好友
const neighbors = await User.findNeighbors({
entityId: 'user1',
level: 1
});
// neighbors[0].node => 邻居节点
// neighbors[0].edge => `{ sourceId, targetId, direction, weight, properties }`
// neighbors[0].level => 1
// 查询高权重的商业伙伴(2度人脉)
const partners = await User.findNeighbors({
entityId: 'user1',
level: 2,
edgeWhere: {
weight: { min: 5 },
properties: `{ category: 'business' }`
}
});
findPaths()
findPaths(options): Promise<GraphQueryResult<GraphPath<T>>>;
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:425
查询两个节点之间的所有路径
Parameters
| Parameter | Type |
|---|---|
options | FindPathsOptions<T, GraphWhere<T>, V> |
Returns
Promise<GraphQueryResult<GraphPath<T>>>
路径数组,按长度和权重排序
Remarks
- 返回所有非循环路径(节点不重复)
fromId === toId时返回的是真实环路(如A→B→A),不含 0 长度的退化路径; 起点自身不构成一条路径,因此该场景下每条结果的length都 ≥ 1- 排序规则:首先按路径长度升序,长度相同时按总权重升序(权重越小优先)
- maxDepth 限制搜索深度,防止指数级爆炸
- 边数组包含完整的边信息(方向、权重、属性)
Example
// 查找城市间的最短路径
const paths = await City.findPaths({
fromId: 'beijing',
toId: 'shanghai',
maxDepth: 5,
edgeWhere: `{ properties: { roadType: 'highway' } }`
});
// paths[0] => 最短路径
// paths[0].nodes => [北京, 天津, 上海]
// paths[0].edges => [{ sourceId, targetId, weight, properties }, ...]
// paths[0].length => 2(经过2条边)
// paths[0].totalWeight => 500(总权重)
remove()
remove(entity): Promise<InstanceType<T>>;
Defined in: rxdb/dist/repository/repository.interface.d.ts:55
删除实体
Parameters
| Parameter | Type | Description |
|---|---|---|
entity | InstanceType<T> | 待删除的实体(按 id 定位) |
Returns
Promise<InstanceType<T>>
删除完成后的实体(字段清空、local 置为 false)
Inherited from
removeEdge()
removeEdge(from, to): Promise<void>;
Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:459
移除边
Parameters
| Parameter | Type | Description |
|---|---|---|
from | InstanceType<T> | 起始节点 |
to | InstanceType<T> | 目标节点 |
Returns
Promise<void>
Remarks
对于无向图,会同时删除双向边
update()
update(entity, patch): Promise<InstanceType<T>>;
Defined in: rxdb/dist/repository/repository.interface.d.ts:48
更新实体
Parameters
| Parameter | Type | Description |
|---|---|---|
entity | InstanceType<T> | 待更新的实体(提供 id 与最新字段) |
patch | Partial<InstanceType<T>> | 字段级 patch;未列出的字段不会被改动 |
Returns
Promise<InstanceType<T>>
持久化完成后的实体