BelongsToManyRepository is a Relation Repository for handling BelongsToMany relationships.
Unlike other relationship types, BelongsToMany relationships need to be recorded through a junction table.
When defining an association relationship in NocoBase, a junction table can be created automatically, or it can be explicitly specified.
find()Finds associated objects
Signature
async find(options?: FindOptions): Promise<M[]>Details
The query parameters are consistent with Repository.find().
findOne()Finds an associated object, returning only one record
Signature
async findOne(options?: FindOneOptions): Promise<M>count()Returns the number of records that match the query conditions
Signature
async count(options?: CountOptions)Type
findAndCount()Queries the database for a dataset and the total count under specific conditions.
Signature
async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>Type
create()Creates an associated object
Signature
async create(options?: CreateOptions): Promise<M>update()Updates associated objects that meet the conditions
Signature
async update(options?: UpdateOptions): Promise<M>destroy()Deletes associated objects that meet the conditions
Signature
async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>add()Adds new associated objects
Signature
async add( options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions ): Promise<void>Type
Details
You can directly pass the targetKey of the associated object, or pass the targetKey along with the field values of the junction table.
Example
set()Sets associated objects
Signature
Details
Parameters are the same as add()
remove()Removes the association with the given objects
Signature
async remove(options: TargetKey | TargetKey[] | AssociatedOptions)Type
toggle()Toggles associated objects.
In some business scenarios, it is often necessary to toggle associated objects. For example, a user can favorite a product, unfavorite it, and favorite it again. The toggle method can be used to quickly implement such functionality.
Signature
async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>Details
The toggle method automatically checks if the associated object already exists. If it exists, it is removed; if not, it is added.