Skip to main content
zerotal

Documentation


Documentation / @zerotal/orm / index / belongsTo

Function: belongsTo()

belongsTo(related, options): (_value, context) => void

Defined in: packages/orm/src/model/decorators/belongsTo.ts:30

Declare the inverse of a one-to-many / one-to-one: this model belongs to a single related row referenced by a foreign key on this model's table (e.g. a Post belongs to a User).

Parameters

() => unknown

Lazy factory returning the related model class.

options

BelongsToOptions

Foreign/local key configuration, plus optional withDefault.

Returns

(_value, context) => void

Example

class Post extends BaseModel {
  \@belongsTo(() => User, { foreignKey: 'user_id' })
  author!: BelongsTo<User>;
}