logologo
开始
手册
开发
插件
API
English
简体中文
开始
手册
开发
插件
API
English
简体中文
logologo
API Overview

@nocobase/auth

AuthManager
Auth
BaseAuth

@nocobase/cache

CacheManager
Cache

@nocobase/cli

NocoBase CLI
全局环境变量

@nocobase/client

Application
Plugin

@nocobase/database

Collection
Field

interfaces

BaseInterface
Filter Operators

RelationRepository

BelongsToManyRepository
belongs-to-repository
HasManyRepository
HasOneRepository
Repository

shared

create-options
destroy-options
find-one
find-options
transaction
update-options

@nocobase/data-source-manager

DataSourceManager
DataSource (abstract)
ICollectionManager
ICollection
IField
IModel
IRepository

@nocobase/flow-engine

DataSourceManager
FlowContext
FlowEngine
FlowModel
FlowResource

@nocobase/logger

Logger

@nocobase/server

AppCommand
Application
AuditManager
Context
Migration
Plugin

@nocobase/sdk

Auth
Storage
Previous PageRelationRepository
Next Pagebelongs-to-repository

#BelongsToManyRepository

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)

类型

interface CountOptions
  extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>,
    Transactionable {
  filter?: Filter;
}

#findAndCount()

从数据库查询特定条件的数据集和结果数。

签名

  • async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>

类型

type FindAndCountOptions = CommonFindOptions;

#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>

类型

type PrimaryKeyWithThroughValues = [TargetKey, Values];

interface AssociatedOptions extends Transactionable {
  tk?:
    | TargetKey
    | TargetKey[]
    | PrimaryKeyWithThroughValues
    | PrimaryKeyWithThroughValues[];
}

详细信息

可以直接传入关联对象的 targetKey,也可将 targetKey 与中间表的字段值一并传入。

示例

const t1 = await Tag.repository.create({
  values: { name: 't1' },
});

const t2 = await Tag.repository.create({
  values: { name: 't2' },
});

const p1 = await Post.repository.create({
  values: { title: 'p1' },
});

const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);

// 传入 targetKey
PostTagRepository.add([t1.id, t2.id]);

// 传入中间表字段
PostTagRepository.add([
  [t1.id, { tagged_at: '123' }],
  [t2.id, { tagged_at: '456' }],
]);

#set()

设置关联对象

签名

  • async set( options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions, ): Promise

详细信息

参数同 add()

#remove()

移除与给定对象之间的关联关系

签名

  • async remove(options: TargetKey | TargetKey[] | AssociatedOptions)

类型

interface AssociatedOptions extends Transactionable {
  tk?: TargetKey | TargetKey[];
}

#toggle()

切换关联对象。

在一些业务场景中,经常需要切换关联对象,比如用户收藏商品,用户可以取消收藏,也可以再次收藏。使用 toggle 方法可以快速实现类似功能。

签名

  • async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>

详细信息

toggle 方法会自动判断关联对象是否已经存在,如果存在则移除,如果不存在则添加。