circuithub / rel8

Hey! Hey! Can u rel8?
https://rel8.readthedocs.io
Other
150 stars 38 forks source link

Use `ilike` with newtype around `Text` #266

Open worm2fed opened 11 months ago

worm2fed commented 11 months ago

I found that I cannot easy use ilike` with my newtype.

I have this:

-- | Represents 'Individual's name.
newtype IndividualName = IndividualName Text
  deriving stock (Generic)
  deriving newtype
    ( Show
    , Eq
    , Ord
    , DBType
    , DBEq
    , DBOrd
    , ToJSON
    , FromHttpApiData
    , ToHttpApiData
    , ToSchema
    , ToText
    )

findIndividuals :: Maybe Text -> Word -> Word -> Tx [IndividualP]
findIndividuals name limit offset =
  Sql.transaction () . fmap pure $
    toDomain <<$>> Sql.select do
      let order = isName >$< Sql.desc
      Sql.limit limit . Sql.offset offset . Sql.orderBy order $ do
        individuals@IndividualSchema{..} <- Sql.each individualSchema
        whenJust name $ \n -> Sql.where_ $ n `Sql.iLike` Sql.unsafeCastExpr isName
        pure individuals

The only way to do this I found is to use unsafeCastExpr which seems safe, because under hood it's Text anyway... Another option could be to to do nSql.iLikefmap toText isName, but there is no Functor for Expr.

Wdyt?