The short answer
A status column whose changes have rules is a state machine, so we write it down as one: a table of allowed moves in code shared by the API and the web app. The API refuses any move the table doesn't allow, and a button is shown only when the table says its move is allowed. No guard or button compares status strings on its own.
On this page
Every business app has status columns. A booking is pending, confirmed, seated or cancelled. An order is placed, paid, refunded. A job application moves through stages. Each column has rules: a cancelled booking can’t be seated, and a refund needs a payment first.
In most codebases those rules are spread across the code as string comparisons. One if (status === 'pending') in the API, another in a button, a third in a background job. When a rule changes, someone updates two of the three places. In November 2026 we made one rule for all our products: a status column whose changes have rules gets one transition table in shared code.
What goes wrong with string comparisons?
Take a booking in Forkly, our restaurant platform. Staff can confirm, seat, cancel or mark a no-show, and can undo a seat or a no-show if they pressed the wrong button. A guest can cancel from a link. An unpaid deposit past its deadline cancels a pending booking.
If each of those places writes its own check, they drift apart:
- The API allows a move the UI never offers, or the UI offers a move the API refuses (the user clicks and gets an error).
- A background job changes a status that a person changed a second earlier, and one of the two changes is lost.
- Nobody can answer “can a booking go from no-show back to confirmed?” without reading the whole codebase.
What does the rule look like in code?
One definition per column, in a package both the API and the web app import:
export const BOOKING = defineMachine({
states: ['pending', 'confirmed', 'seated', 'cancelled', 'no_show'],
moves: {
pending: ['confirmed', 'cancelled'],
confirmed: ['seated', 'no_show', 'cancelled'],
seated: ['confirmed'], // staff undo
no_show: ['confirmed'], // staff undo
cancelled: [],
},
});
Everything else asks it:
- The API guards with it. A move is written as an update that only succeeds from an allowed state:
UPDATE ... SET status = 'seated' WHERE id = $1 AND status IN (<states that can move to seated>). The list comes from the machine, not from a string typed in the service. If two people act at once, one update wins and the other changes no row, so the API can answer with a conflict. - The UI asks it before showing a button. “Seat guests” is shown when
BOOKING.can(status, 'seated')is true. Better still, the API returns the allowed actions with the record, and the page renders those. - The documentation is generated from it. Forkly’s state-machine document, with a diagram for every status column, is built from the same file, and a test fails when the document is out of date.
Do read-only comparisons count?
No. A filter like “show the paid orders”, a colour for a status or a label doesn’t decide what may happen, so it can compare strings. The rule is about guards (the API allowing or refusing a move) and button conditions (the UI offering an action).
That distinction matters when you measure. A search for status string comparisons found about 450 to 500 lines in each of Forkly, Mesito and Kommo. Most were filters and labels. The work is in the smaller set that are guards and buttons.
What about money?
Payments, refunds and billing follow the same rule, with extra checks: run the same action twice, and it pays once. Those files move to the machine in their own changes, with their own tests, not in a bulk clean-up.
How do you test a state machine?
Not with one test per arrow. The pattern in our app template uses a property test: random sequences of moves from a logged seed, with invariants checked after each step (a final state has no way out, every move the machine allows succeeds once, and so on). If a seed fails, it becomes a regression case. Against a real PostgreSQL, a second test proves the race: two concurrent moves, one wins and the other gets a conflict.
Where are we with it?
Forkly has a machine for every status column; the remaining work is replacing the button conditions and some guards that still compare strings next to it. Mesito has machines for its main workflows (showings, leads, offers, invoices, signatures and others). Kommo, our salon hiring app, has them for jobs, applications, interviews, offers and hires. The template we start new apps from has the pattern built in, so new apps start this way.
Is it worth it for a small app?
The table is a few lines. It replaces rules that would otherwise be spread across the API, the UI and the jobs, and it answers “can X go from A to B?” in one place. We’d write it from the first status column.