haskell-beam / haskell-beam/beam

Unclear how to use simple newtype wrappers in beam table columns

Open
#451 4 comments 0 reactions 0 assignees View on GitHub
docs
Dominant language
Haskell
Stars
635
Forks
193
PR merge metrics
No merged PRs in 30d

Description

Related to #262.

I think it expands on that because I try something beyond what the recommendations there cover and use `just_` with my that custom column type:

```haskell
guard_' (just_ emails ==?. _userEmail users)
````

I tailored that from the example on [values support](https://tathougies.github.io/beam/user-guide/queries/select/#values-support).

I hope you don't mind, most of my commentary is inline comments in the Haskell code.

```haskell
data UserT f
= UserT { _userId :: Columnar f (SqlSerial Int)
, _userEmail :: Columnar f Email
}
type User = UserT Identity

textToEmail :: Text.Text -> Maybe Email
textToEmail = undefined

newtype Email = Email Text.Text

instance Beam.HasSqlEqualityCheck Beam.Postgres Email

instance Beam.FromBackendRow Beam.Postgres Email where
fromBackendRow = do
val <- Beam.fromBackendRow
case textToEmail val of
Right email -> pure email
Left str -> fail $ "invalid email: " <> str

-- that works until I want to do something like
-- NOTE assuming this is like
-- select * from users where user.email in ('email1@email.com', ...)
getUsersByEmail :: [Email] -> IO [User]
getUsersByEmail listOfEmails = runSelectReturningList @Beam.Postgres @Beam.Pg @User . select $ do
users <- all_ (ViewingFact._metricsViewingFact ViewingFact.metricsDb)
emails <- values_ listOfEmails
guard_' (just_ emails ==?. _userEmail users)
pure users
```

If time allows I'll make a repo of this example for others who run into this problem and if necessary for us to debug with.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.