Squirrel Squeezing with Robots!
a good way to handle DB return type proliferation
Gleam and databases are a little awkward together - but you gotta make it work.
pog is an excellent database client, but it’s low-level - you have to write a lot of sql-strings in gleam, and then all those decoders too.
squirrel helps a ton, it’s a very practical and easy way to automate the work of using pog - it reads your SQL queries from a directory of query files and generates functions that use pog and return a specific type for each query. This is a big time-saver - but each query gets a unique return-row type, even if the query returns the same data as another query.
Here are the types I was dealing with - they had identical fields
sql.InsertMonitorRow
sql.GetMonitorForUserRow
sql.GetMonitorsForUserRow // note the plural *sigh*
sql.UpdateMonitorRowIf it’s the same type of squirrel, i’d like it to be the same type Squirrel
// I'd like my application to get this one standard type:
sql.Monitor“semantic satiation” when you look at the word “squirrel” long enough it starts seeming really weird.
Q: Is there a good way to treat types with the same fields as the same type?
A: no
I looked all over and tried some hacks, but ultimately if you declare distinct types they are DISTINCT. You can, however, make adapter functions that take Type1 and give you Type2.
/// adapt the squirrel-supplied type to my preferred one
pub fn monitor_from_update_monitor_row(x: sql.UpdateMonitorRow) -> Monitor {
...
}You can easily have your AI coding buddy write little adapter functions for you. Gemini and the PyCharm LLMs had no problem writing these tedious little functions.
“in sql.gleam find the types that can be adapted to the Result type in adapt.gleam. write the adapter functions for adapt.gleam. follow the example of the ones for Monitor”
— instruction to pycharm
It took a little wrangling the first time to get a nice compact and gleamy example, but then it followed the template just fine.
Wrap-up
Have squirrel auto-generate all of its usual return types, willy-nilly in its
sql.gleamfileWrite the version of the return type that you want to use in your own code in a file like
adapt.gleamwrite little adapter functions that squeeze the various squirrels into your one chosen type. have the AI write these for you.
when you call the squirrel functions just pipeline the results through the adapters (I put all of this in another module so I can reuse all the error-handling and not clutter my other code)





