I have a question that is not specific to Elm, but is general to client / server systems and to which I hope there might be an elegant solution in Elm, or at least some agreed best practice amongst full stack developers.
Back in the day (the '90s) when last I coded, C clients connected to relational database servers. In addition to tripping into SEGV faults merely, it sometimes seems, by breathing hard, we faced the problem of how best to handle record keys across the server / client boundary. I had rather hoped that thirty year’s experience might have landed a well accepted solution, but Googling around and searching Stack Overflow (and elsewhere) it seems not.
The issue was/ is whether to use natural or surrogate keys in the data architecture. Using surrogate keys (whether in the form of row ids, sequences or GUIDs) definitely simplifies the relational database schema, but at the cost of making applications more complex. Should the data server create keys and dole them out to applications on demand? Or should all applications connecting to a data server be obliged to use a common algorithm to create keys that are guaranteed not to clash?
If the server is responsible for key creation, when should the application request them? When the user starts to create a new record? Or when he or she tries to save it? If the latter, how should master-detail be handled? You need the key for the master record in order to create the detail.
The strategy we ended up using was for the database to employ a two part key. The server gave the application a session id when it connected. This was simply a 32-bit integer generated using a database sequence and was the “top” part of the database key. Externally it was represented as a hex string. The “bottom” part of the key was a second, 32-bit integer allocated by the application, again represented externally as a hex string. The application could combine the top and bottom parts to create a new key. In our particular application users would often import large CSV files that created a many new records, only to delete a goodly portion of them; avoiding costly round trips to obtain keys for records that might never be saved to the database seemed a good thing.
Key allocation/ management was, however, a complete pain in the neck. It required an inordinate amount of book keeping to “swizzle” keys to pointers (and back) and great care to ensure that master records were always queried before related detail (many-to-many relations were a particular nightmare, requiring “placeholders” to be allocated). The whole edifice was brittle and hellish to debug.
So, my plea. How’s this issue addressed in modern web apps? And does the immutability of Elm data and its strict type system open a better solution? Has anyone written a package that deals with it?