nocobase/docs/en-US/api/database/relation-repository/has-many-repository.md

134 lines
2.5 KiB
Markdown
Raw Normal View History

# HasManyRepository
2023-01-26 14:58:32 +00:00
`HasManyRepository` is the `Relation Repository` for handling `HasMany` relationships.
2023-02-05 04:35:37 +00:00
## Class Methods
### `find()`
2023-01-26 14:58:32 +00:00
Find associated objects.
2023-01-26 14:58:32 +00:00
**Signature**
* `async find(options?: FindOptions): Promise<M[]>`
2023-01-26 14:58:32 +00:00
**Detailed Information**
2023-01-26 14:58:32 +00:00
Query parameters are the same as [`Repository.find()`](../repository.md#find).
### `findOne()`
2023-01-26 14:58:32 +00:00
Find associated objects, only to return one record.
2023-01-26 14:58:32 +00:00
**Signature**
* `async findOne(options?: FindOneOptions): Promise<M>`
<embed src="../shared/find-one.md"></embed>
### `count()`
2023-01-26 14:58:32 +00:00
Return the number of records matching the query criteria.
2023-01-26 14:58:32 +00:00
**Signature**
* `async count(options?: CountOptions)`
2023-01-26 14:58:32 +00:00
**Type**
2023-01-27 14:24:45 +00:00
```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
}
```
### `findAndCount()`
2023-01-26 14:58:32 +00:00
Find datasets from the database with the specified filtering conditions and return the number of results.
2023-01-26 14:58:32 +00:00
**Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
2023-01-26 14:58:32 +00:00
**Type**
2023-01-27 14:24:45 +00:00
```typescript
type FindAndCountOptions = CommonFindOptions
```
### `create()`
2023-01-26 14:58:32 +00:00
Create associated objects.
2023-01-26 14:58:32 +00:00
**Signature**
* `async create(options?: CreateOptions): Promise<M>`
<embed src="../shared/create-options.md"></embed>
### `update()`
2023-01-26 14:58:32 +00:00
Update associated objects that match the conditions.
2023-01-26 14:58:32 +00:00
**Signature**
* `async update(options?: UpdateOptions): Promise<M>`
<embed src="../shared/update-options.md"></embed>
### `destroy()`
2023-01-26 14:58:32 +00:00
Delete associated objects.
2023-01-26 14:58:32 +00:00
**Signature**
* `async destroy(options?: TK | DestroyOptions): Promise<M>`
<embed src="../shared/destroy-options.md"></embed>
### `add()`
2023-01-26 14:58:32 +00:00
Add association relationships between objects.
2023-01-26 14:58:32 +00:00
**Signature**
2023-01-27 14:24:45 +00:00
* `async add(options: TargetKey | TargetKey[] | AssociatedOptions)`
2023-01-26 14:58:32 +00:00
**Type**
2023-01-27 14:24:45 +00:00
```typescript
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[];
}
```
2023-01-26 14:58:32 +00:00
**Detailed Information**
2023-01-26 14:58:32 +00:00
* `tk` - The targetKey value of the associated object, either as a single value or an array.
<embed src="../shared/transaction.md"></embed>
### `remove()`
2023-01-27 14:24:45 +00:00
Remove the association with the given objects.
2023-01-26 14:58:32 +00:00
**Signature**
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
2023-01-26 14:58:32 +00:00
**Detailed Information**
2023-01-26 14:58:32 +00:00
Same parameters as the [`add()`](#add) method.
### `set()`
2023-01-26 14:58:32 +00:00
Set the associated object of the current relationship.
2023-01-26 14:58:32 +00:00
**Signature**
* `async set(options: TargetKey | TargetKey[] | AssociatedOptions)`
2023-01-26 14:58:32 +00:00
**Detailed Information**
2023-01-26 14:58:32 +00:00
Same parameters as the [`add()`](#add) method.