bitemyapp / esqueleto

New home of Esqueleto, please file issues so we can get things caught up!
BSD 3-Clause "New" or "Revised" License
376 stars 108 forks source link

Add `selectFirst` Esqueleto variant #257

Closed ibarrae closed 3 years ago

ibarrae commented 3 years ago

esqueleto re-exports the function selectFirst from persistent at the moment, I was wondering if a new function with similar functionality could be introduced. I've seen scenarios in which we only want 1 result or nothing from different queries. Probably doing something similar to the following:

fmap listToMaybe <$>
  E.select $
    E.from $ \table ->
      E.where_ $ ...
      E.limit 1
     return table

Something I have in mind (that I'm not even sure it compiles) would be something like the following:

selectFirst :: (SqlSelect a r, MonadIO m) => SqlQuery a -> SqlReadT m (Maybe r)
selectFirst query = do
    limit 1
    select query

I wonder if that's possible in esqueleto, probably would require some lifting though :disappointed_relieved:. I hope all of these makes sense and thanks for the great library!

parsonsmatt commented 3 years ago

Yeah! The code would look like: selectFirst query = fmap listToMaybe $ select $ limit 1 >> query or something similar, but that is totally doable.

Maybe worthwhile to do in the 3.5 switchover.

ibarrae commented 3 years ago

Great, thanks for the quick reply @parsonsmatt, I'll wait for 3.5 to be merged and probably will work on those changes :tada: