[Haskell-cafe] Using Haskell types in a database

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Tue Feb 10 15:32:21 UTC 2015


On Tue, Feb 10, 2015 at 05:29:55PM +0300, Nikita Karetnikov wrote:
> User
>   name         UserName
>   email        UserEmail v   Maybe
>   last_login   UserLastLogin
> 
> where
> 
> data Verified
> data Unverified
> newtype UserEmail v = UserEmail Text deriving (Show, Read)
> derivePersistField "UserEmail"

I think you want the verification status to exist at the value level as well
as the type level.  What you currently have is essentially the same as
defining your table to be

    [(UserName, Maybe (UserEmail v), UserLastLogin)]

where I guess you're assuming the `v` will be existentially quantified. 
Presumably this isn't actually what you want.  You could however have (the
database version of)

    [(UserName, Maybe UserEmail, UserLastLogin)]

and something like (the database version of)

    UserEmail -> Maybe UserEmailVerified
    UserEmail -> Maybe UserEmailUnverified

which would give you the type safety you're looking for, I think.

Opaleye supports user-created types for this kind of purpose, and I've found
it invaluable.

Tom


More information about the Haskell-Cafe mailing list