FYI - possible alternate approach
I thought you might be interested to know that APIv4 now supports calculated fields with a callback function that returns a SQL snippet. The fields can be added via specProvider which sets a sqlRenderer
property of the field. Here's an example.
The difference between this extension and the APIv4 approach is that this extension calculates and stores the values, whereas the APIv4 fields are rendered on-demand. Also, to-date APIv4 fields are only used by SearchKit, and wouldn't show up on the contact summary (unless you created a block using SearchKit).