Function HasAndBelongsToMany

  • A decorator for defining a many-to-many relationship between entities in a single-table design ORM system. This relationship is facilitated through a join table, representing the connection between the two entities. The decorator simplifies the management of such relationships by automating the setup and handling of the join table metadata, thereby enabling seamless querying and manipulation of related entities.

    Type Parameters

    • T extends default

      The source entity class the decorator is applied to.

    • K extends default

      The target entity class that T has a many-to-many relationship with.

    • J extends JoinTable<T, K>

      The join table entity class that associates T with K.

    • L extends JoinTable<K, T>

      The join table entity class that associates K with T, allowing for bidirectional relationships.

    Parameters

    • getTarget: (() => EntityClass<T>)

      A function that returns the constructor of the target entity class. This enables lazy evaluation and avoids circular dependency issues.

    • props: HasAndBelongsToManyProps<T, K, J, L>

      Configuration options for the many-to-many relationship, including the key on the related entity and the details of the join table (through a "through" function) that facilitates the relationship.

    Returns ((_value, context) => void)

    A class field decorator function that, when applied to a field, registers the many-to-many relationship in the ORM's metadata system. This registration includes information about the join table and the relationship's configuration, essential for the ORM to handle related entities correctly.

    Usage example:

    class User extends BaseEntity {
    @HasAndBelongsToMany(() => Group, {
    targetKey: 'users',
    through: () => ({
    joinTable: UserGroup,
    foreignKey: 'userId'
    })
    })
    public groups: Group[];
    }

    class Group extends BaseEntity {
    @HasAndBelongsToMany(() => User, {
    targetKey: 'groups',
    through: () => ({
    joinTable: UserGroup,
    foreignKey: 'groupId'
    })
    })
    public users: User[];
    }

    class UserGroup extends JoinTable<User, Group> {
    public readonly userId: ForeignKey;
    public readonly groupId: ForeignKey;
    }

    In this example, User entities are related to Group entities through a many-to-many relationship, with UserGroup serving as the join table. The decorator indicates this relationship, allowing for efficient querying and manipulation of related entities.

      • (_value, context): void
      • Parameters

        • _value: undefined
        • context: ClassFieldDecoratorContext<K, T[]>

        Returns void

Generated using TypeDoc