Class HasMany<S, T, SourceKey, TargetKey, TargetPrimaryKey>

One-to-many association. See hasMany

Like with HasOne, the foreign key will be defined on the target model.

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().

Type Parameters

  • S extends Model = Model

    The model on which hasMany has been called, on which the association methods will be added.

  • T extends Model = Model

    The model passed to hasMany. This model will receive the Foreign Key attribute.

  • SourceKey extends AttributeNames<S> = any

    The name of the attribute that the foreign key in the target model will reference.

  • TargetKey extends AttributeNames<T> = any

    The name of the Foreign Key attribute on the Target model.

  • TargetPrimaryKey extends AttributeNames<T> = any

    The name of the Primary Key attribute of the Target model. Used by HasManySetAssociationsMixin & others.

Hierarchy

Constructors

Properties

#private: any
inverse: BelongsTo<T, S, TargetKey, SourceKey>
isAliased: boolean
isSelfAssociation: boolean
options: NormalizedHasManyOptions<SourceKey, TargetKey>
parentAssociation: null | Association<Model<any, any>, Model<any, any>, string, NormalizedAssociationOptions<string>>

A reference to the association that created this one.

source: ModelStatic<S>
target: ModelStatic<T>

Accessors

  • get as(): string
  • The identifier of the relation on the source model.

    Returns string

  • get associationType(): string
  • The type of the association. One of HasMany, BelongsTo, HasOne, BelongsToMany

    Returns string

  • get foreignKey(): TargetKey
  • Returns TargetKey

  • get identifierField(): string
  • The column name of the foreign key (on the target model)

    Returns string

  • get isMultiAssociation(): boolean
  • Returns boolean

  • get isSingleAssociation(): boolean
  • Deprecated

    negate isMultiAssociation instead

    Returns boolean

  • get name(): {
        plural: string;
        singular: string;
    }
  • Returns {
        plural: string;
        singular: string;
    }

    • plural: string
    • singular: string
  • get scope(): undefined | AssociationScope
  • Returns undefined | AssociationScope

  • get sourceKey(): SourceKey
  • The name of the attribute the foreign key points to.

    This key is on the Source Model. The foreignKey is on the Target Model.

    Returns SourceKey

  • get sourceKeyAttribute(): SourceKey
  • Deprecated

    use sourceKey

    Returns SourceKey

  • get sourceKeyField(): string
  • Returns string

  • get isMultiAssociation(): boolean
  • Returns boolean

Methods

  • 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.

    Parameters

    • sourceInstance: S

      the source instance

    • Optional rawTargetInstances: AllowArray<T | Exclude<T[TargetPrimaryKey], any[]>>

      A single instance or primary key, or a mixed array of persisted instances or primary keys

    • Optional options: HasManyAddAssociationsMixinOptions<T>

      Options passed to target.update.

    Returns Promise<void>

  • Count everything currently associated with this, using an optional where clause.

    Parameters

    Returns Promise<number>

  • Check if one or more rows are associated with this.

    Parameters

    • sourceInstance: S

      the source instance

    • targetInstances: AllowArray<T | Exclude<T[TargetPrimaryKey], any[]>>

      Can be an array of instances or their primary keys

    • Optional options: HasManyHasAssociationsMixinOptions<T>

      Options passed to getAssociations

    Returns Promise<boolean>

  • Un-associate one or several target rows.

    Parameters

    • sourceInstance: S

      instance to un associate instances with

    • targetInstances: AllowArray<T | Exclude<T[TargetPrimaryKey], any[]>>

      Can be an Instance or its primary key, or a mixed array of instances and primary keys

    • Optional options: HasManyRemoveAssociationsMixinOptions<T>

      Options passed to target.update

    Returns Promise<void>

  • 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

    Parameters

    • sourceInstance: S

      source instance to associate new instances with

    • rawTargetInstances: null | AllowArray<T | Exclude<T[TargetPrimaryKey], any[]>>

      An array of persisted instances or primary key of instances to associate with this. Pass null to remove all associations.

    • Optional options: HasManySetAssociationsMixinOptions<T>

      Options passed to target.findAll and update.

    Returns Promise<void>

  • Private

    Normalize input

    Returns

    built objects

    Parameters

    • input: AllowArray<T | Exclude<T[TargetPrimaryKey], any[]>>

      it may be array or single obj, instance or primary key

    Returns T[]

Generated using TypeDoc