2 ms·
> How do you store that in the database? 1. A very small dimension table, defining the set of flags in terms of the current state: (user_lifecycle_state, is_a,
by derefr 4y ago
> How do you store that in the database?
1. A very small dimension table, defining the set of flags in terms of the current state: (user_lifecycle_state, is_a, is_b, is_c, ...). Join it to the fact table when you need it.
2. Computed columns in a writable view wrapping the table.
3. Procedures/functions that take the lifecycle state as input, so you can `SELECT u.id, ..., is_active(u.lifecycle_state) AS is_active FROM users`.
> which just hides/black-boxes the ugly code
I don't see what's ugly about it. The point of defining something as a FSM is that that's a formal definition on the design level, separate from any particular implementation; you can write a single stateless module that defines these predicates in terms of the lifecycle-states on any or all layers of the stack they're needed, and they'll all work the same, because they're all working off a formal definition of the meaning of each state.
Compare and contrast: Postgres has an `inet` type for IP addresses. It has a function `family(inet)`, which returns 4 or 6. Your business layer's runtime probably also has a function like that somewhere. They won't necessarily have the same outputs (i.e. your business layer's version might be a pair of predicates `is_ipv4?` and `is_ipv6?`) — but they'll both answer the same way semantically given the same inputs, because they're both working off of a shared formal definition of what an IP address is.
- eurasiantiger 4y agoThanks for the clarification. The example, however, doesn’t fit reality particularly well given how few implementations actually agree on all possible forms of v4 and v6 addresses.