Never bother with `Value`s again?

Open
#440 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
sql
Domain
database

Research direction

This is a design proposal, not a defined task. The entry points are the select and selectOne wrappers over Database.Esqueleto.Experimental and the UnpackValue and CanCoerceFromValue type machinery in the issue body. Start by reading how Value is used in the library's existing select signatures and by checking the aggregate return-type problems the author describes. Done means maintainers have decided whether to integrate this, and on what API, so a newcomer should first ask whether a direction has been chosen.

Written by the indexing model from the issue text.

Description

I was fed up enough with having to pattern match on Values and using unValue when it is only a solo tuple in essence, and can just be coerced away.
So I made a small wrapper around select and selectOne that makes sure to coerce all the Value as to their respective as. That way it's much more ergonomic when doing SELECT table.column FROM and not having to unwrap the Values all the time.

I want to present my implementation here, so that either:

  • It can be critiqued and I can improve it; and/or,
  • It could be integrated in some way so that the API of esqueleto might become more usable.

I haven't found any issues with this implementation, other than a few type errors because of too lax (mostly aggregate) function return types. e.g. countRows's return type is a Num a, so this has to be made concrete either in the query, or at the usage site of the returned (Value) a. Or when making helper functions, some type applications might be necessary.


{-# LANGUAGE TypeFamilies #-}
import Database.Esqueleto.Experimental qualified as E

type SqlSelectUnpack a r b =
  ( SqlSelect a r -- Converts the `SqlExpr (Value a)` to the resulting `[r]` of select
  , CanCoerceFromValue r ~ b -- The check that we're only coercing `Value a` to `a`
  , Coercible r b
  , UnpackValue r b
  )

-- | Constraint to remove 'Value' from 'select' return values.
class UnpackValue a b where
  unpackValue :: a -> b

-- | Any value can be coerced, either to itself, or @Value a -> a@
instance (Coercible a b, CanCoerceFromValue a ~ b) => UnpackValue a b where
  unpackValue = coerce
  {-# INLINE unpackValue #-}

{- |
Checks that we're only trying to coerce a @Value a -> a@.
Any other value will not be touched.
-}
type family CanCoerceFromValue a where
  CanCoerceFromValue [a] = [CanCoerceFromValue a]
  CanCoerceFromValue (Maybe a) = Maybe (CanCoerceFromValue a)
  -- This is the one that actually changes the type by removing the 'Value'
  CanCoerceFromValue (Value a) = CanCoerceFromValue a
  -- ALL THE TUPLE INSTANCES
  CanCoerceFromValue (a, b) = (CanCoerceFromValue a, CanCoerceFromValue b)
  CanCoerceFromValue (a, b, c) =
    (CanCoerceFromValue a, CanCoerceFromValue b, CanCoerceFromValue c)
  CanCoerceFromValue (a, b, c, d) =
    (CanCoerceFromValue a, CanCoerceFromValue b, CanCoerceFromValue c, CanCoerceFromValue d)

  -- ... more tuple instances

  -- The default case
  CanCoerceFromValue a = a

-- | @SELECT@ from the database (cf. 'Database.Esqueleto.select')
select ::
  forall a b r m backend.
  ( MonadIO m
  , SqlBackendCanRead backend
  , SqlSelectUnpack a r b
  ) =>
  SqlQuery a -> ReaderT backend m [b]
select query =
    unpackValue <$> E.select query

-- | @SELECT@ with @LIMIT 1@ from the database (cf. 'Database.Esqueleto.selectOne')
selectOne ::
  forall a b r m backend.
  ( MonadIO m
  , SqlBackendCanRead backend
  , SqlSelectUnpack a r b
  ) =>
  SqlQuery a -> ReaderT backend m (Maybe b)
selectOne query =
    unpackValue <$> E.selectOne query
Dominant language
Haskell
Stars
401
Forks
108
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bitemyapp/esqueleto

All issues in bitemyapp/esqueleto

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.