// Generated by moonorm from models.mbt. Edit those files, not this one.

///|
let team_model : @moonorm.Model[Team] = @moonorm.Model::from_fields(
  "teams",
  [
    @moonorm.field(
      "id",
      BigIntType,
      h => h.id,
      primary_key=true,
      autoincrement=true,
    ),
    @moonorm.field(
      "name",
      VarcharType(64),
      h => h.name,
      nullable=false,
      unique=true,
    ),
  ],
  r => { id: r.read(0), name: r.read(1) },
)

///|
pub fn Team::model() -> @moonorm.Model[Team] {
  team_model
}

///|
pub fn Team::heroes() -> @moonorm.Relation[Team, Hero] {
  @moonorm.has_many(Hero::model(), "team_id", h => h.id)
}

///|
let hero_model : @moonorm.Model[Hero] = @moonorm.Model::from_fields(
  "heroes",
  [
    @moonorm.field(
      "id",
      BigIntType,
      h => h.id,
      primary_key=true,
      autoincrement=true,
    ),
    @moonorm.field("name", TextType, h => h.name, nullable=false),
    @moonorm.field(
      "secret_name",
      TextType,
      h => h.secret,
      nullable=false,
      unique=true,
    ),
    @moonorm.field(
      "age",
      IntType,
      h => h.age,
      checks=["age >= 0", "age < 1000"],
    ),
    @moonorm.field("power", RealType, h => h.power, nullable=false, default="0"),
    @moonorm.field(
      "team_id",
      BigIntType,
      h => h.team_id,
      references=("teams", "id"),
      on_delete="SET NULL",
    ),
    @moonorm.field("email", TextType, h => h.email, nullable=false),
  ],
  r => {
    id: r.read(0),
    name: r.read(1),
    secret: r.read(2),
    age: r.read(3),
    power: r.read(4),
    team_id: r.read(5),
    email: r.read(6),
    visits: Default::default(),
  },
  indexes=[@moonorm.index(["power"]), @moonorm.index(["name", "age"])],
)

///|
pub fn Hero::model() -> @moonorm.Model[Hero] {
  hero_model
}

///|
pub fn Hero::team() -> @moonorm.Relation[Hero, Team] {
  @moonorm.belongs_to(Team::model(), "id", h => h.team_id)
}