Skip to main content

Function: HasMany()

HasMany(target, options): PropertyDecorator

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

Property decorator establishing a one-to-many relationship with another entity.

Enables eager loading through context.set(Parent).include('children') or lazy loading via LazyRelation.

Parameters​

ParameterTypeDescription
target() => FunctionLambda returning the child entity constructor.
optionsstring | HasManyOptionsForeign key property on the child entity pointing to this parent, or options object.

Returns​

PropertyDecorator

Usecase​

Model one-to-many associations (e.g. User has many Posts, Order has many Items).

Example​

@HasMany(() => Post, { foreignKey: 'userId', lazy: true })
posts!: LazyRelation<Post[]>;