adonisjs-state-machines

Model state transitions with guards and after-hooks for Lucid models.

2|1|Updated Mar 16, 2026
One-click install
npx skills add https://github.com/omakei/adonisjs-architecture-skill --skill adonisjs-state-machines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: adonisjs-state-machines
Source: https://github.com/omakei/adonisjs-architecture-skill/tree/main/adonisjs/skills/adonisjs-state-machine
Command: npx skills add https://github.com/omakei/adonisjs-architecture-skill --skill adonisjs-state-machines

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Managing complex domain lifecycles in AdonisJS Lucid models is error-prone without a structured pattern. This Skill provides a zero-dependency, type-safe state machine approach that enforces valid state transitions, supports guards and side effects, and optionally logs transitions for auditing.

Core Features & Use Cases

  • Static transition maps defined on the state machine class to eliminate per-instance closures and prevent memory leaks.
  • Guards that throw on invalid transitions to ensure business rules are always enforced.
  • After-transition hooks to perform side effects (emails, events) after a successful transition without retaining model references.
  • Optional audit trail via a state_transitions table to track model_type, column, and state changes for traceability.
  • Real-world pattern: model an Order lifecycle from Draft to Completed with terminal states and guards.

Quick Start

Define the enum, create a StateMachine subclass with transitions and guards, attach it to a model via HasStateMachine, then perform transitions using model.stateMachine('column').transitionTo(TargetState).

Frequently Asked Questions about adonisjs-state-machines

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage complex lifecycle logic on AdonisJS Lucid models?

A state machine enforces valid transitions, guards business rules, triggers side effects via hooks, and optionally logs changes to an audit table, providing a structured approach to managing complex lifecycles on Lucid models.

How do I prevent invalid state transitions in an AdonisJS application?

Guards throw errors on invalid state transitions, ensuring business rules are always enforced when moving a Lucid model between defined states like Draft or Completed.

Do I need external packages to implement state machines in AdonisJS?

No, you can implement state machines without external packages using a zero-dependency approach that defines static transition maps on the state machine class to eliminate per-instance closures and prevent memory leaks.

How do I track state changes on AdonisJS Lucid models for auditing?

You can track state changes for auditing by optionally logging transitions to a state_transitions table, which records model_type, column, and state changes for full traceability across multiple steps.

How do I trigger side effects after a successful state transition in AdonisJS?

After-transition hooks perform side effects like sending emails or emitting events after a successful state transition without retaining model references, keeping the process memory-safe.

What is the best way to model an Order status workflow in AdonisJS?

The best way is modeling the Order lifecycle using a state machine with defined states, guards, and terminal states, moving transitions via a static map applied directly to the Lucid model.