2022-10-31 11:52:17 +08:00
# BelongsToManyRepository
2023-01-27 22:24:04 +08:00
`BelongsToManyRepository` is the `Relation Repository` for handling `BelongsToMany` relationships.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08: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 12:35:52 +08:00
## Class Methods
2022-10-31 11:52:17 +08:00
### `find()`
2023-01-27 22:24:04 +08:00
Find associated objects.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async find(options?: FindOptions): Promise<M[]>`
2023-01-27 22:24:04 +08:00
**Detailed Information**
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
Query parameters are the same as [`Repository.find()` ](../repository.md#find ).
2022-10-31 11:52:17 +08:00
### `findOne()`
2023-01-27 22:24:04 +08:00
Find associated objects, only to return one record.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async findOne(options?: FindOneOptions): Promise<M>`
< embed src = "../shared/find-one.md" > < / embed >
### `count()`
2023-01-27 22:24:04 +08:00
Return the number of records matching the query criteria.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async count(options?: CountOptions)`
2023-01-27 22:24:04 +08:00
**Type**
2022-10-31 11:52:17 +08:00
```typescript
interface CountOptions extends Omit< SequelizeCountOptions , ' distinct ' | ' where ' | ' include ' > , Transactionable {
filter?: Filter;
}
```
### `findAndCount()`
2023-01-27 22:24:04 +08:00
Find datasets from the database with the specified filtering conditions and return the number of results.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
2023-01-27 22:24:04 +08:00
**Type**
2022-10-31 11:52:17 +08:00
```typescript
type FindAndCountOptions = CommonFindOptions
```
### `create()`
2023-01-27 22:24:04 +08:00
Create associated objects.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async create(options?: CreateOptions): Promise<M>`
< embed src = "../shared/create-options.md" > < / embed >
### `update()`
2023-01-27 22:24:04 +08:00
Update associated objects that match the conditions.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async update(options?: UpdateOptions): Promise<M>`
< embed src = "../shared/update-options.md" > < / embed >
### `destroy()`
2023-01-27 22:24:04 +08:00
Delete associated objects.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>`
< embed src = "../shared/destroy-options.md" > < / embed >
### `add()`
2023-01-27 22:24:04 +08:00
Add new associated objects.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async add(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions
): Promise< void > `
2023-01-27 22:24:04 +08:00
**Type**
2022-10-31 11:52:17 +08:00
```typescript
type PrimaryKeyWithThroughValues = [TargetKey, Values];
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[];
}
```
2023-01-27 22:24:04 +08:00
**Detailed Information**
Pass the `targetKey` of the associated object directly, or pass the `targetKey` along with the field values of the intermediate table.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```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 22:24:04 +08:00
// Pass in the targetKey
2022-10-31 11:52:17 +08:00
PostTagRepository.add([
t1.id, t2.id
]);
2023-01-27 22:24:04 +08:00
// Pass in intermediate table fields
2022-10-31 11:52:17 +08:00
PostTagRepository.add([
[t1.id, { tagged_at: '123' }],
[t2.id, { tagged_at: '456' }],
]);
```
### `set()`
2023-01-27 22:24:04 +08:00
Set the associated objects.
**Signature**
2022-10-31 11:52:17 +08:00
* async set(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
): Promise< void >
2023-01-27 22:24:04 +08:00
**Detailed Information**
Parameters are the same as [add() ](#add ).
2022-10-31 11:52:17 +08:00
### `remove()`
2023-01-27 22:24:04 +08:00
Remove the association with the given objects.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
2023-01-27 22:24:04 +08:00
**Type**
2022-10-31 11:52:17 +08:00
```typescript
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[];
}
```
### `toggle()`
2023-01-27 22:24:04 +08: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.
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>`
2023-01-27 22:24:04 +08:00
**Detailed Information**
2022-10-31 11:52:17 +08:00
2023-01-27 22:24:04 +08: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.