Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly map a DB tuple returned by the query to a Haskell record? #32

Open
mistyharsh opened this issue Jan 13, 2023 · 0 comments

Comments

@mistyharsh
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant