Function HasMany

  • A decorator for establishing a one-to-many relationship between entities in a single-table design ORM system. This relationship indicates that a single instance of the entity where this decorator is applied can be associated with multiple instances of another entity. The decorator facilitates the definition and management of such relationships by automatically handling the necessary metadata registration, thereby simplifying the implementation of relational data models.

    Type Parameters

    • T extends default

      The source entity class that has many instances of another entity. This is the entity class to which the decorator is applied.

    • K extends default

      The target entity class that is related to T through a one-to-many relationship.

    Parameters

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

      A function that returns the constructor of the target entity class T. This function is crucial for establishing the relationship dynamically and avoids issues related to circular dependencies.

    • props: HasManyProps<T>

      Configuration options for the one-to-many relationship, specifically the foreign key in the target entity that links back to the source entity. This configuration ensures the correct association and navigation between related entities.

    Returns ((_value, context) => void)

    A class field decorator function that, when applied to a field of type array of T, registers the one-to-many relationship in the ORM's metadata system. This registration is essential for the ORM to recognize and correctly handle the relationship between the source and target entities, facilitating operations such as retrieval and update of related entities.

    Usage example:

    class User extends TableClass {
    @HasMany(() => Post, { foreignKey: 'userId' })
    public posts: Post[];
    }

    class Post extends TableClass {
    @ForeignKeyProperty()
    public readonly userId: ForeignKey;

    @BelongsTo(() => User, { foreignKey: "userId" })
    public readonly user: User;
    }

    In this example, each User entity is associated with multiple Post entities through a one-to-many relationship. The @HasMany decorator is applied to the posts field of the User class, indicating that a user can have many posts. The foreignKey property specifies the attribute in the Post entity that establishes the connection back to the User entity, enabling the ORM to manage the relationship effectively.

      • (_value, context): void
      • Parameters

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

        Returns void

Generated using TypeDoc