BelongsToManyRepository 是用于处理 BelongsToMany 关系的 Relation Repository。
不同于其他关系类型,BelongsToMany 类型的关系需要通过中间表来记录。
在 NocoBase 中定义关联关系,可自动创建中间表,也可以明确指定中间表。
find()查找关联对象
签名
async find(options?: FindOptions): Promise<M[]>详细信息
查询参数与 Repository.find() 一致。
findOne()查找关联对象,仅返回一条记录
签名
async findOne(options?: FindOneOptions): Promise<M>count()返回符合查询条件的记录数
签名
async count(options?: CountOptions)类型
findAndCount()从数据库查询特定条件的数据集和结果数。
签名
async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>类型
create()创建关联对象
签名
async create(options?: CreateOptions): Promise<M>update()更新符合条件的关联对象
签名
async update(options?: UpdateOptions): Promise<M>destroy()删除符合条件的关联对象
签名
async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>add()添加新的关联对象
签名
async add( options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions ): Promise<void>类型
详细信息
可以直接传入关联对象的 targetKey,也可将 targetKey 与中间表的字段值一并传入。
示例
set()设置关联对象
签名
详细信息
参数同 add()
remove()移除与给定对象之间的关联关系
签名
async remove(options: TargetKey | TargetKey[] | AssociatedOptions)类型
toggle()切换关联对象。
在一些业务场景中,经常需要切换关联对象,比如用户收藏商品,用户可以取消收藏,也可以再次收藏。使用 toggle 方法可以快速实现类似功能。
签名
async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>详细信息
toggle 方法会自动判断关联对象是否已经存在,如果存在则移除,如果不存在则添加。