Mixin BelongsToMany
Many-to-many association with a join table.
When the join table has additional attributes, these can be passed in the options object:
All methods allow you to pass either a persisted instance, its primary key, or a mixture:
});
In the API reference below, replace Assocation(s)
with the actual name of your association, e.g. for User.belongsToMany(Project)
the getter will be user.getProjects()
.
Get everything currently associated with this, using an optional where clause.
Params:
setAssociations([newAssociations], [options]) -> Promise
Set the associated models by passing an array of instances or their primary keys. Everything that it not in the passed array will be un-associated.
Params:
Name | Type | Description |
---|---|---|
[newAssociations] | Array.<Instance | String | Number> | An array of persisted instances or primary key of instances to associate with this. Pass or undefined to remove all associations. |
[options] | Object | Options passed to through.findAll , bulkCreate , update and destroy . Can also hold additional attributes for the join table |
[options.validate] | Object | Run validation for the join model |
addAssociations([newAssociations], [options]) -> Promise
Associate several persisted instances with this.
Params:
Name | Type | Description |
---|---|---|
[newAssociations] | Array.<Instance | String | Number> | An array of persisted instances or primary key of instances to associate with this. |
[options] | Object | Options passed to , bulkCreate and update . Can also hold additional attributes for the join table. |
[options.validate] | Object | Run validation for the join model. |
Associate a persisted instance with this.
Params:
createAssociation([values], [options]) -> Promise
Create a new instance of the associated model and associate it with this.
Params:
Name | Type | Description |
---|---|---|
[values] | Object | |
[options] | Object | Options passed to create and add. Can also hold additional attributes for the join table |
removeAssociation([oldAssociated], [options]) -> Promise
Un-associate the instance.
Params:
Name | Type | Description |
---|---|---|
[oldAssociated] | Instance | String | Number | Can be an Instance or its primary key |
[options] | Object | Options passed to |
Un-associate several instances.
Params:
hasAssociation([instance], [options]) -> Promise
Check if an instance is associated with this.
Params:
Name | Type | Description |
---|---|---|
[instance] | Instance | String | Number | Can be an Instance or its primary key |
[options] | Object | Options passed to getAssociations |
hasAssociations([instances], [options]) -> Promise
Check if all instances are associated with this.
Params:
Name | Type | Description |
---|---|---|
[instances] | Array.<Instance | String | Number> | Can be an array of instances or their primary keys |
[options] | Object | Options passed to getAssociations |
Count everything currently associated with this, using an optional where clause.
This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on , open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc and