static/v5/class/lib/associations/has-many.js~HasMany.html
publicclass| source
Association → HasMany
One-to-many association
In the API reference below, add the name of the association to the method, e.g. for User.hasMany(Project) the getter will be user.getProjects(). If the association is aliased, use the alias instead, e.g. User.hasMany(Project, { as: 'jobs' }) will be user.getJobs().
| Public Methods | | public |
add(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: Object): Promise
Associate one or more target rows with this.
| | | public |
count(instance: Model, options: Object): Promise<number>
Count everything currently associated with this, using an optional where clause.
| | | public |
create(sourceInstance: Model, values: Object, options: Object): Promise
Create a new instance of the associated model and associate it with this.
| | | public |
get(instances: Model | Array<Model>, options: Object): Promise<Array<Model>>
Get everything currently associated with this, using an optional where clause.
| | | public |
has(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: Object): Promise
Check if one or more rows are associated with this.
| | | public |
remove(sourceInstance: Model, targetInstances: Model | Model[] | string | string[] | number | number[], options: Object): Promise
Un-associate one or several target rows.
| | | public |
set(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: Object): Promise
Set the associated models by passing an array of persisted instances or their primary keys.
| |
| From class Association | | public |
The type of the association.
| | | public |
| | | public |
| |
Associate one or more target rows with this. This method accepts a Model / string / number to associate a single row, or a mixed array of Model / string / numbers to associate multiple rows.
| Name | Type | Attribute | Description | | sourceInstance | Model | |
the source instance
| | targetInstances | Model | Model[] | string[] | string | number[] | number |
|
A single instance or primary key, or a mixed array of persisted instances or primary keys
| | options | Object |
|
Options passed to target.update.
|
| Promise |
Count everything currently associated with this, using an optional where clause.
| Name | Type | Attribute | Description | | instance | Model | |
the source instance
| | options | Object |
|
find & count options
| | options.where | Object |
|
An optional where clause to limit the associated models
| | options.scope | string | boolean |
|
Apply a scope on the related model, or remove its default scope by passing false
|
Create a new instance of the associated model and associate it with this.
| Name | Type | Attribute | Description | | sourceInstance | Model | |
source instance
| | values | Object |
|
values for target model instance
| | options | Object |
|
Options passed to target.create
|
| Promise |
Get everything currently associated with this, using an optional where clause.
| Name | Type | Attribute | Description | | instances | Model | Array<Model> | |
source instances
| | options | Object |
|
find options
| | options.where | Object |
|
An optional where clause to limit the associated models
| | options.scope | string | boolean |
|
Apply a scope on the related model, or remove its default scope by passing false
| | options.schema | string |
|
Apply a schema on the related model
|
Check if one or more rows are associated with this.
| Name | Type | Attribute | Description | | sourceInstance | Model | |
the source instance
| | targetInstances | Model | Model[] | string[] | string | number[] | number |
|
Can be an array of instances or their primary keys
| | options | Object |
|
Options passed to getAssociations
|
| Promise |
Un-associate one or several target rows.
| Name | Type | Attribute | Description | | sourceInstance | Model | |
instance to un associate instances with
| | targetInstances | Model | Model[] | string | string[] | number | number[] |
|
Can be an Instance or its primary key, or a mixed array of instances and primary keys
| | options | Object |
|
Options passed to target.update
|
| Promise |
Set the associated models by passing an array of persisted instances or their primary keys. Everything that is not in the passed array will be un-associated
| Name | Type | Attribute | Description | | sourceInstance | Model | |
source instance to associate new instances with
| | targetInstances | Model | Model[] | string[] | string | number[] | number |
|
An array of persisted instances or primary key of instances to associate with this. Pass null or undefined to remove all associations.
| | options | Object |
|
Options passed to target.findAll and update.
| | options.validate | Object |
|
Run validation for the join model
|
| Promise |