Function ForeignKeyAttribute

  • A decorator for annotating class fields as foreign keys within the context of a single-table design entity, aimed at establishing and managing relationships between different entities in a relational manner. This decorator enables the clear and explicit declaration of foreign key relationships, contributing to the ORM's ability to navigate and resolve these associations efficiently.

    The entity can belong to its associated entity has a HasOne or HasMany

    Does not allow property to be optional.

    Supplying the target entity enables DynaRecord to enforce referential integrity for the foreign key even when no relationship decorators are defined (for example when a foreign key is used purely for validation without denormalising related records).

    Type Parameters

    Parameters

    • getTarget: () => EntityClass<TargetEntity>

      A function returning the constructor for the entity referenced by the foreign key. This allows deferred resolution to avoid circular dependency issues.

    • Optionalprops: P

      An optional object of AttributeOptions, including configuration options such as metadata attributes. These options allow for additional customization of the foreign key attribute, including aliasing and metadata tagging.

    Returns (
        _value: undefined,
        context: AttributeDecoratorContext<
            T,
            P["nullable"] extends true
                ? NullableForeignKey<TargetEntity>
                : ForeignKey<TargetEntity>,
            P,
        >,
    ) => void

    A class field decorator function that targets and initializes the class's prototype to register the foreign key with the ORM's metadata system. This registration is crucial for enabling the ORM to correctly interpret and manage the relationships between entities.

    Usage example:

    class Order extends TableClass {
    @ForeignKeyAttribute(() => User, { alias: 'UserID' })
    public userId: ForeignKey<User>; // Foreign key to the User entity. Cannot be optional.

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

    @ForeignKeyAttribute(() => Profile, { alias: 'ProfileId', nullable: true })
    public profileId?: NullableForeignKey<Profile>; // Set to optional. Nullable foreign key to another entity (e.g., UserProfile)

    @BelongsTo(() => Profile, { foreignKey: "profileId" })
    public readonly profile?: Profile; // Set to optional because its linked via a NullableForeignKey
    }

    Here, @ForeignKeyAttribute decorates userId of Order, designating it as a foreign key that references the User entity. This decoration not only clarifies the nature of the relationship but also empowers the ORM to enforce relational integrity and facilitate entity association operations.