flyt

14 — Subscriptions

Work that follows a change, whoever made it.

org/domain.flyt
// What happened to an employee.
enum EmployeeEvent {
  Joined { department: DepartmentId }
  Moved { from: DepartmentId, to: DepartmentId }
}
org/entities.flyt
entity employee {
  key: EmployeeId
  state: Employee
  command: Command
  reject: Rejection
  event: EmployeeEvent
  column department: DepartmentId = state.department

  apply(state, command) {
    match state {
      None -> match command {
        Hire { name, department } -> Applied { state: Employee { name, department }, events: [Joined { department }] }
        Move { .. } -> Rejected { reason: NoEmployee }
      }
      Some(e) -> match command {
        Hire { .. } -> Rejected { reason: Hired }
        Move { to } -> Applied { state: { ..e, department: to }, events: [Moved { from: e.department, to }] }
      }
    }
  }
}