Function BelongsTo

  • A decorator for defining a "BelongsTo" relationship between entities in a single-table design using DynaRecord. This relationship indicates that the decorated field is a reference to another entity, effectively establishing a parent-child linkage. The decorator dynamically enforces the presence or optionality of this reference based on the nature of the foreign key, enhancing type safety and relationship integrity within the ORM model.

    The decorator must reference a foreign key defined on the model.

    IMPORTANT - If referenced through a NullableForeignKey then set the belongs to attribute to optional.

    The decorator takes into consideration whether the relationship is defined by a NullableForeignKey, allowing the field to be optional if so, otherwise ensuring it is not optional. This behavior aligns with relational database design principles, where a foreign key can either strictly enforce a relationship or allow for its absence.

    Type Parameters

    • T extends default

      The source entity type, from which the relationship originates.

    • K extends default

      The target entity type, to which the relationship points.

    Parameters

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

      A function returning the class (constructor function) of the target entity. This enables late binding and avoids circular dependency issues.

    • props: BelongsToProps<T>

      Configuration object for the relationship, including the name of the foreign key attribute.

    Returns ((_value, context) => void)

    A class field decorator function that initializes the source entity's class prototype, registering the relationship with the ORM's metadata system. This registration process includes specifying the relationship type as "BelongsTo", along with detailing the target entity and foreign key.

    Usage example:

    class Order extends TableClass {
    @ForeignKeyProperty({ alias: "UserId" })
    public readonly userId: ForeignKey;

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

    In this example, @BelongsTo decorates the user field of the Order entity, establishing a "BelongsTo" relationship with the User entity via the userId foreign key. This decoration signifies that each Order instance is related to a specific User instance.

Generated using TypeDoc