Skip to content

Unclear how to use simple newtype wrappers in beam table columns #451

Open
@codygman

Description

@codygman

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:

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

I tailored that from the example on values support.

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

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 ('[email protected]', ...)
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions