Skip to content

postgrelize / BelongsTo

Variable: BelongsTo ​

BelongsTo: (target, options?) => ModelPropertyDecorator

Defined in: src/decorators/association-decorators.ts:27

Adds a belongsTo association; the foreign key is on this model, marked with @ForeignKey for the target unless options.foreignKey is given. The property name is the alias (as).

Parameters ​

target ​

ModelGetter

Function returning the associated model.

options? ​

Readonly<{ as?: string | Readonly<{ plural: string; singular: string; }>; constraints?: boolean; foreignKey?: string | Readonly<{ allowNull?: boolean; field?: string; name?: string; }>; hooks?: boolean; onDelete?: string; onUpdate?: string; scope?: Readonly<Record<string, unknown>>; sourceKey?: string; targetKey?: string; }>

Association options (foreignKey, targetKey, onDelete, …).

Returns ​

ModelPropertyDecorator

An attribute decorator.

Example ​

ts
class Post extends Model {
  @ForeignKey(() => User)
  @Column(DataTypes.INTEGER)
  userId!: number;

  @BelongsTo(() => User)
  user!: User;
}

Released under the MIT License.