valderman / selda

A type-safe, high-level SQL library for Haskell
https://selda.link
MIT License
478 stars 58 forks source link

Using Maybe for updating only Just values, if any #179

Open Odeyaloleg opened 2 years ago

Odeyaloleg commented 2 years ago

Hey everyone, I can't find documentation on how to update only Just values having several Maybe's. Let's say user sent a request asking edit some of his profile fields:

editUser :: MonadSelda m => ID User -> Maybe Text -> Maybe Text -> Maybe Text -> m ()
editUser userId mbNewLogin mbNewPassword mbNewStatus = 
  update_ 
    usersTable
    (\user -> user ! #id .== literal userId)
    (error "What do I do here?")

Perhaps we could replace the error line with something like this:

-- ?:= - is a hypothetical operator for working with Maybe's
(\user -> user `with`
  [ #login ?:= literal <$> mbNewLogin,
    #password ?:= literal <$> mbNewPassword,
    #status ?:= literal <$> mbNewStatus ]
)

Do we have such a functionality in selda? I wrote my own solution and can post it via pull request or comment, but I am pretty sure I am missing some obvious built-in way of doing this.