The previous post covered defining a Servant server with automatic code generation for Elm bindings.
Are there libraries that allow for type-safe persistence in Haskell?
Again referring to the state of the Haskell ecosystem, one interesting library is acid-state, which provides ACID guarantees for de/serializing (serializable) Haskell data structures directly, with no external DBMS.
acid-state also uses Template Haskell, and it's relatively straightforward to augment the prior Hello Server data definitions. We define some simple operations we want (query, write), as well as the template invocations:
$(deriveSafeCopy 0 'base ''ServerState)
$(makeAcidic ''ServerState ['writeState, 'queryState])
On the server side, this file has the updated logic to interact with the data store. Since acid-state uses monadic state, we can use ReaderT and again follow the Servant tutorial to thread the data state through the server calls. Although it doesn't seem strictly necessary given the acid-state guarantees, we can use the bracket pattern from Control.Exception to ensure that the data store is closed normally in the event of unexpected server failure.
That's pretty much it! As promised by the stack, generating the Elm code is just a matter of changing some filename configs in the code. In this case, the REST interface didn't change, so changing the backend had no impact on the rest of the client code.
I haven't thought too much yet about the implications of this approach vs using some typed-SQL library like persistent, which I intend the try next.