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

Generate `ToMaybe` in `deriveEsqueletoRecord` #370

Open 9999years opened 1 year ago

9999years commented 1 year ago

Before submitting your PR, check that you've:

After submitting your PR:

halogenandtoast commented 1 year ago

I have a version that compiles and I believe generates the desired instance here:

https://github.com/bitemyapp/esqueleto/compare/master...halogenandtoast:esqueleto:goose/dux-1281-tomaybe-instance-for-deriveesqueletorecord

I wasn't sure if the scope was larger than this (are there other instances that are needed?)

I assume what else is left is:

-- * @x@ is transformed into @'SqlExpr' ('Value' ('Maybe' x))@.
-- * If there exists an instance @'SqlSelect' sql x@, then @x@ is transformed into @sql@.
parsonsmatt commented 1 year ago

Proper acceptance critieria is going to include a test where we can define a subquery that returns a SqlMyRecord and you can left join on it, and the result is a SqlMaybeMyRecord, and parsing/query execution works out fine

halogenandtoast commented 1 year ago

@parsonsmatt would this suffice for acceptance:

mySubselectRecordQuery :: SqlQuery (SqlExpr (Maybe (Entity Address)))
mySubselectRecordQuery = do
  _ :& record <-
    from $ table @User
        `leftJoin` myRecordQuery
        `on` (do \(user :& record) -> just (user ^. #id) ==. record.myUser ?. #id)
  pure $ record.myAddress
    itDb "can left join on records" $ do
        setup
        [record] <- fmap entityVal . catMaybes <$> select mySubselectRecordQuery
        liftIO $ record
          `shouldSatisfy`
          (\case Address { addressAddress = "30-50 Feral Hogs Rd" } -> True
                 _ -> False)