In all the examples I have seen the results from esqueleto are projected into a list of tuples or to entities records.
For example:
previousLogItems <- select $ from $ \li -> do
orderBy [desc (li ^. LogItemId)]
limit 10
return (li ^. LogItemId, li ^. LogItemTitle)
Is there any way in esqueleto to project a subset of the columns to custom records (different than the entity) instead of tuples? This being done without an extra projection from tuples to custom records.
As an example, let's say that it would be inefficient to get all the data from the database so we only want to project columns WindowTitle and BeginTime from the database into a custom record with adequate names for those columns.
Update
Example of not working code:
data Custom = Custom
{ title :: Text
, id :: Int
} deriving (Eq, Show, Generic)
daily :: Servant.Handler [Custom]
daily = do
lis <- liftIO $ runDB $
select $ from $ \li -> do
orderBy [desc (li ^. LogItemId)]
limit 25
return (Custom (li ^. LogItemTitle) (li ^. LogItemId))
return lis
Error:
• Couldn't match expected type ‘Text’
with actual type ‘SqlExpr (Database.Esqueleto.Value Text)’
• In the first argument of ‘Custom’, namely ‘(li ^. LogItemTitle)’
In the first argument of ‘return’, namely
‘(Custom (li ^. LogItemTitle) (li ^. LogItemId))’
In a stmt of a 'do' block:
return (Custom (li ^. LogItemTitle) (li ^. LogItemId))
Update
Example of not working code:
daily :: Servant.Handler [Custom]
daily = do
lis <- liftIO $ runDB $
select $ from $ \li -> do
orderBy [desc (li ^. LogItemId)]
limit 25
return (Custom <$> (li ^. LogItemTitle) <*> (li ^. LogItemId))
return lis
Error:
• Couldn't match type ‘Database.Esqueleto.Value Text’ with ‘Text’
Expected type: SqlExpr Text
Actual type: SqlExpr (Database.Esqueleto.Value Text)
• In the second argument of ‘(<$>)’, namely ‘(li ^. LogItemTitle)’
In the first argument of ‘(<*>)’, namely
‘Custom <$> (li ^. LogItemTitle)’
In the first argument of ‘return’, namely
‘(Custom <$> (li ^. LogItemTitle) <*> (li ^. LogItemId))’
• Couldn't match type ‘Database.Esqueleto.Value (Key LogItem)’
with ‘Int’
Expected type: SqlExpr Int
Actual type: SqlExpr (Database.Esqueleto.Value (Key LogItem))
• In the second argument of ‘(<*>)’, namely ‘(li ^. LogItemId)’
In the first argument of ‘return’, namely
‘(Custom <$> (li ^. LogItemTitle) <*> (li ^. LogItemId))’
In a stmt of a 'do' block:
return (Custom <$> (li ^. LogItemTitle) <*> (li ^. LogItemId))