跳到主要内容

IGraphRepository<T, V, EdgeInfoType>

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:268

图结构仓库接口(基础)

Extends

Type Parameters

Type ParameterDefault type
T extends EntityType-
VEdgeFilterOptionsFull
EdgeInfoTypeEdgeInfoFull

Methods

addEdge()

addEdge(
from,
to,
weight?,
properties?): Promise<void>;

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:371

添加边

Parameters

ParameterTypeDescription
fromInstanceType<T>起始节点
toInstanceType<T>目标节点
weight?number边权重(需要在元数据中启用 features.graph.weight)
properties?Record<string, any>边属性(需要在元数据中启用 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:15

查询实体数量

Parameters

ParameterTypeDescription
optionsEntityStaticType<T, "countOptions">查询选项

Returns

Promise<number>

Inherited from

IRepository.count


countNeighbors()

countNeighbors(options): Promise<number>;

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:324

统计邻居节点数量

Parameters

ParameterType
optionsFindNeighborsOptions<T, any, 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:20

创建实体

Parameters

ParameterType
entityInstanceType<T>

Returns

Promise<InstanceType<T>>

Inherited from

IRepository.create


find()

find(options): Promise<InstanceType<T>[]>;

Defined in: rxdb/dist/repository/repository.interface.d.ts:10

查询多个实体

Parameters

ParameterTypeDescription
optionsEntityStaticType<T, "findOptions">查询选项

Returns

Promise<InstanceType<T>[]>

Inherited from

IRepository.find


findNeighbors()

findNeighbors(options): Promise<NeighborResult<T, EdgeInfoType>[]>;

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:305

查询邻居节点(带边信息)

Parameters

ParameterType
optionsFindNeighborsOptions<T, any, V>

Returns

Promise<NeighborResult<T, EdgeInfoType>[]>

邻居节点数组,每个元素包含节点和边信息

Remarks

  • 返回结果不包含起始节点
  • 结果自动去重(同一节点只返回最短路径)
  • 默认按 level 升序、weight 升序排序

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<GraphPath<T>[]>;

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:353

查询两个节点之间的所有路径

Parameters

ParameterType
optionsFindPathsOptions<T, any, V>

Returns

Promise<GraphPath<T>[]>

路径数组,按长度和权重排序

Remarks

  • 返回所有非循环路径(节点不重复)
  • 排序规则:首先按路径长度升序,长度相同时按总权重升序(权重越小优先)
  • 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:31

删除实体

Parameters

ParameterType
entityInstanceType<T>

Returns

Promise<InstanceType<T>>

Inherited from

IRepository.remove


removeEdge()

removeEdge(from, to): Promise<void>;

Defined in: rxdb-plugin-graph/src/graph-repository.interface.ts:382

移除边

Parameters

ParameterTypeDescription
fromInstanceType<T>起始节点
toInstanceType<T>目标节点

Returns

Promise<void>

Remarks

对于无向图,会同时删除双向边


update()

update(entity, patch): Promise<InstanceType<T>>;

Defined in: rxdb/dist/repository/repository.interface.d.ts:26

更新实体

Parameters

ParameterType
entityInstanceType<T>
patchPartial<InstanceType<T>>

Returns

Promise<InstanceType<T>>

Inherited from

IRepository.update