HasMany
Extends:
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()
.
See:
Method Summary
Public Methods | ||
---|---|---|
public | async add(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise Associate one or more target rows with | |
public | Count everything currently associated with this, using an optional where clause. | |
public | Create a new instance of the associated model and associate it with this. | |
public | Get everything currently associated with this, using an optional where clause. | |
public | async has(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise Check if one or more rows are associated with | |
public | async remove(sourceInstance: Model, targetInstances: Model | Model[] | string | string[] | number | number[], options: object): Promise Un-associate one or several target rows. | |
public | async 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. |
Inherited Summary
From class Association | ||
---|---|---|
public | The type of the association. | |
public | ||
public |
Public Methods
public async add(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise source
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.
Params:
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 |
Return:
Promise |
public async count(instance: Model, options: object): Promise<number> source
Count everything currently associated with this, using an optional where clause.
Params:
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 |
public async create(sourceInstance: Model, values: object, options: object): Promise source
Create a new instance of the associated model and associate it with this.
Return:
Promise |
public async get(instances: Model | Array<Model>, options: object): Promise<Array<Model>> source
Get everything currently associated with this, using an optional where clause.
Params:
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 |
See:
- Model.findAll for a full explanation of options
public async has(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise source
Check if one or more rows are associated with this
.
Return:
Promise |
public async remove(sourceInstance: Model, targetInstances: Model | Model[] | string | string[] | number | number[], options: object): Promise source
Un-associate one or several target rows.
Params:
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 |
Return:
Promise |
public async set(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise source
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
Params:
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 |
options | object |
|
Options passed to |
options.validate | object |
|
Run validation for the join model |
Return:
Promise |
Copyright © 2014–present Sequelize contributors
Licensed under the MIT License.
https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html