Skip to main content

Function: BelongsTo()

BelongsTo(target, options): PropertyDecorator

Defined in: src/decorators/BelongsTo.ts:22

Property decorator establishing a child-to-parent inverse relationship.

Enables eager loading through context.set(Child).include('user') or lazy loading via LazyRelation.

Parameters​

ParameterTypeDescription
target() => FunctionLambda returning the parent entity constructor.
optionsstring | BelongsToOptionsForeign key property on this child entity pointing to the parent.

Returns​

PropertyDecorator

Usecase​

Model child-to-parent associations (e.g. Post belongs to User, OrderItem belongs to Order).

Example​

@BelongsTo(() => User, { foreignKey: 'userId', lazy: true })
user?: LazyRelation<User>;