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

187 lines
4.1 KiB
Markdown
Raw Normal View History

# BelongsToManyRepository
2023-01-27 14:24:04 +00:00
`BelongsToManyRepository` is the `Relation Repository` for handling `BelongsToMany` relationships.
2023-01-27 14:24:04 +00:00
Unlike other relationship types, the `BelongsToMany` type of relationship needs to be recorded through an intermediate table. The intermediate table can be created automatically or explicitly specified when defining association relationships in NocoBase.
2023-02-05 04:35:52 +00:00
## Class Methods
### `find()`
2023-01-27 14:24:04 +00:00
Find associated objects.
2023-01-27 14:24:04 +00:00
**Signature**
* `async find(options?: FindOptions): Promise<M[]>`
2023-01-27 14:24:04 +00:00
**Detailed Information**
2023-01-27 14:24:04 +00:00
Query parameters are the same as [`Repository.find()`](../repository.md#find).
### `findOne()`
2023-01-27 14:24:04 +00:00
Find associated objects, only to return one record.
2023-01-27 14:24:04 +00:00
**Signature**
* `async findOne(options?: FindOneOptions): Promise<M>`
<embed src="../shared/find-one.md"></embed>
### `count()`
2023-01-27 14:24:04 +00:00
Return the number of records matching the query criteria.
2023-01-27 14:24:04 +00:00
**Signature**
* `async count(options?: CountOptions)`
2023-01-27 14:24:04 +00:00
**Type**
```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
}
```
### `findAndCount()`
2023-01-27 14:24:04 +00:00
Find datasets from the database with the specified filtering conditions and return the number of results.
2023-01-27 14:24:04 +00:00
**Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
2023-01-27 14:24:04 +00:00
**Type**
```typescript
type FindAndCountOptions = CommonFindOptions
```
### `create()`
2023-01-27 14:24:04 +00:00
Create associated objects.
2023-01-27 14:24:04 +00:00
**Signature**
* `async create(options?: CreateOptions): Promise<M>`
<embed src="../shared/create-options.md"></embed>
### `update()`
2023-01-27 14:24:04 +00:00
Update associated objects that match the conditions.
2023-01-27 14:24:04 +00:00
**Signature**
* `async update(options?: UpdateOptions): Promise<M>`
<embed src="../shared/update-options.md"></embed>
### `destroy()`
2023-01-27 14:24:04 +00:00
Delete associated objects.
2023-01-27 14:24:04 +00:00
**Signature**
* `async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>`
<embed src="../shared/destroy-options.md"></embed>
### `add()`
2023-01-27 14:24:04 +00:00
Add new associated objects.
2023-01-27 14:24:04 +00:00
**Signature**
* `async add(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions
): Promise<void>`
2023-01-27 14:24:04 +00:00
**Type**
```typescript
type PrimaryKeyWithThroughValues = [TargetKey, Values];
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[];
}
```
2023-01-27 14:24:04 +00:00
**Detailed Information**
Pass the `targetKey` of the associated object directly, or pass the `targetKey` along with the field values of the intermediate table.
2023-01-27 14:24:04 +00:00
**Example**
```typescript
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);
2023-01-27 14:24:04 +00:00
// Pass in the targetKey
PostTagRepository.add([
t1.id, t2.id
]);
2023-01-27 14:24:04 +00:00
// Pass in intermediate table fields
PostTagRepository.add([
[t1.id, { tagged_at: '123' }],
[t2.id, { tagged_at: '456' }],
]);
```
### `set()`
2023-01-27 14:24:04 +00:00
Set the associated objects.
**Signature**
* async set(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
): Promise<void>
2023-01-27 14:24:04 +00:00
**Detailed Information**
Parameters are the same as [add()](#add).
### `remove()`
2023-01-27 14:24:04 +00:00
Remove the association with the given objects.
2023-01-27 14:24:04 +00:00
**Signature**
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
2023-01-27 14:24:04 +00:00
**Type**
```typescript
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[];
}
```
### `toggle()`
2023-01-27 14:24:04 +00:00
Toggle the associated object.
In some business scenarios, it is often needed to toggle the associated object. For example, user adds a product into collection, and the user cancels the collection and collect it again. Using the `toggle` method can quickly implement similar functions.
2023-01-27 14:24:04 +00:00
**Signature**
* `async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>`
2023-01-27 14:24:04 +00:00
**Detailed Information**
2023-01-27 14:24:04 +00:00
The `toggle` method automatically checks whether the associated object already exists, and removes it if it does, or adds it if it does not.