belongsTo

Usage

import { Model } from 'pinia-orm'
import User from './User'
class Phone extends Model {
  static entity = 'users'
  static fields () {
    return {
      id: this.attr(null),
      userId: this.attr(null),
      number: this.string(''),
      user: this.belongsTo(User, 'userId')
    }
  }
}

With Decorator

import { Model } from 'pinia-orm'
import { Attr, BelongsTo, Str } from 'pinia-orm/decorators'
import User from './User'
class User extends Model {
  static entity = 'users'
  
  @Attr(null) declare id: number | null
  @Attr(null) declare userId: number | null
  @Str('') declare number: string
  @BelongsTo(() => User, 'userId') declare user: User
}

Typescript Declarations

function belongsTo(
  related: typeof Model,
  foreignKey: string | string[],
  ownerKey?: string | string[],
): BelongsTo