2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
# HasManyRepository
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
`HasManyRepository` 是用于处理 `HasMany` 关系的 `Relation Repository`。
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
## 类方法
|
|
|
|
|
|
|
|
### `find()`
|
|
|
|
|
|
|
|
查找关联对象
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async find(options?: FindOptions): Promise<M[]>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**详细信息**
|
2022-10-06 02:29:53 +00:00
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
查询参数与 [`Repository.find()`](../repository.md#find) 一致。
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `findOne()`
|
|
|
|
|
|
|
|
查找关联对象,仅返回一条记录
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async findOne(options?: FindOneOptions): Promise<M>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
<embed src="../shared/find-one.md"></embed>
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
### `count()`
|
|
|
|
|
|
|
|
返回符合查询条件的记录数
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async count(options?: CountOptions)`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**类型**
|
|
|
|
```typescript
|
|
|
|
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
|
|
|
|
filter?: Filter;
|
|
|
|
}
|
|
|
|
```
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `findAndCount()`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
从数据库查询特定条件的数据集和结果数。
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**类型**
|
|
|
|
```typescript
|
|
|
|
type FindAndCountOptions = CommonFindOptions
|
|
|
|
```
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
### `create()`
|
|
|
|
|
|
|
|
创建关联对象
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async create(options?: CreateOptions): Promise<M>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
<embed src="../shared/create-options.md"></embed>
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `update()`
|
|
|
|
|
|
|
|
更新符合条件的关联对象
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async update(options?: UpdateOptions): Promise<M>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
<embed src="../shared/update-options.md"></embed>
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `destroy()`
|
|
|
|
|
|
|
|
删除符合条件的关联对象
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async destroy(options?: TK | DestroyOptions): Promise<M>`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
<embed src="../shared/destroy-options.md"></embed>
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `add()`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
添加对象关联关系
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
**签名**
|
|
|
|
* `async add(options: TargetKey | TargetKey[] | AssociatedOptions)`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**类型**
|
|
|
|
```typescript
|
|
|
|
interface AssociatedOptions extends Transactionable {
|
|
|
|
tk?: TargetKey | TargetKey[];
|
|
|
|
}
|
|
|
|
```
|
2022-10-06 02:29:53 +00:00
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**详细信息**
|
2022-10-06 02:29:53 +00:00
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
* `tk` - 关联对象的 targetKey 值,可以是单个值,也可以是数组。
|
|
|
|
<embed src="../shared/transaction.md"></embed>
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `remove()`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
移除与给定对象之间的关联关系
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
**签名**
|
|
|
|
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**详细信息**
|
2022-10-06 02:29:53 +00:00
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
参数同 [`add()`](#add) 方法。
|
2022-10-06 02:29:53 +00:00
|
|
|
|
|
|
|
### `set()`
|
|
|
|
|
|
|
|
设置当前关系的关联对象
|
|
|
|
|
|
|
|
**签名**
|
|
|
|
|
|
|
|
* `async set(options: TargetKey | TargetKey[] | AssociatedOptions)`
|
|
|
|
|
2022-10-16 11:16:14 +00:00
|
|
|
**详细信息**
|
|
|
|
|
|
|
|
参数同 [`add()`](#add) 方法。
|
|
|
|
|
2022-10-06 02:29:53 +00:00
|
|
|
|