Function HasOne

  • A decorator for defining a one-to-one relationship between entities in a single-table design ORM system. This relationship implies that an instance of the entity to which this decorator is applied can be associated with at most one instance of another entity. The HasOne decorator plays a crucial role in establishing and managing such relationships by automatically registering the necessary metadata, thereby enabling the ORM to recognize and navigate these associations effectively.

    Type Parameters

    • T extends default

      The source entity class that has a one-to-one relationship with another entity. This is the class on which the decorator is applied.

    • K extends default

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

    Parameters

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

      A function returning the constructor of the target entity class T. This function is essential for dynamically establishing the relationship, helping to circumvent circular dependency issues.

    • props: HasOneProps<T>

      Configuration options for the one-to-one relationship, particularly the foreign key within the target entity that links it back to the source entity. This setup is critical for accurately linking and managing the entities involved.

    Returns ((_value, context) => void)

    A class field decorator function that, when applied to a field, registers the one-to-one relationship in the ORM's metadata system. The registration includes the target entity and the specified foreign key, ensuring that the ORM correctly interprets and maintains the relationship.

    Usage example:

    class User extends TableClass {
    @HasOne(() => Profile, { foreignKey: 'userId' })
    public profile?: Profile;
    }

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

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

    Here, the @HasOne decorator is applied to the profile field of the User class, establishing a one-to-one relationship between a User and a Profile. The foreign key (userId) in the Profile entity points back to the User entity, enabling the ORM to manage this relationship by linking a user to at most one profile.

      • (_value, context): void
      • Parameters

        • _value: undefined
        • context: ClassFieldDecoratorContext<K, Optional<T>>

        Returns void

Generated using TypeDoc