postgresql-typed
postgresql-typed copied to clipboard
How to correctly map a DB tuple returned by the query to a Haskell record?
Haskell newbie trying to understand how I can get this to work. I have a record defined like this:
data EmailConfig = EmailConfig {
id :: UUID,
fromName :: ByteString,
fromEmail :: ByteString,
apiKey :: ByteString
}
When I execute the SQL query like following, I wish to map it to a record.
selectEmailQ :: PGPreparedQuery (UUID, ByteString, ByteString, ByteString)
selectEmailQ = [pgSQL|$
SELECT
id, from_name, from_email, api_key
FROM
email_config;
|]
Following the advice from the comments in the StackOverflow, I am making use of uncurry4
function like below to map to a record:
uncurry4 func (a, b, c, d) = func a b c d
getEmailConfig :: PGConnection -> IO [EmailConfig]
getEmailConfig conn = do
result <- pgQuery conn selectEmailQ
pure $ map (uncurry4 EmailConfig) result
So, I wonder if this is a correct/idiomatic way of doing the mapping of result to a Haskell record? Or, is there some other better way of achieving this? I looked at the Database.PostgreSQL.Typed.Relation module but I don't think it is meant for this purpose.