Beam custom types using Data.Tagged

Using Text all throughout your Beam schema is not particularly type-safe. One solution is to define newtype wrappers; but it is not1 super-clear as to how to define Beam specific instances for these wrappers. There is however an alternative using Data.Tagged in conjunction with type-level string literals.


Say you want to add a column for user name. In place of using Text, you can define a type specifically for name and use that:

import Data.Tagged

type UserName = Tagged "userName" Text

As of PR #4572, beam has all the instances for Tagged (tag :: k) a. This means you can use these types in your Beam schema without having to write any database specific instances for them.