Skip to main content

Function: HasOne()

HasOne(target, options): PropertyDecorator

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

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

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

Parameters​

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

Returns​

PropertyDecorator

Usecase​

Model one-to-one associations (e.g. User has one Profile, Order has one Invoice).

Example​

@HasOne(() => Profile, { foreignKey: 'userId', lazy: true })
profile?: LazyRelation<Profile>;