我有这个片段描述Notification
和Notified
个实体:
Notification
type NotiType
release ReleaseId
date UTCTime
Notified
aboutWhat NotificationId
unread Bool
user UserId
现在我要写这个:
-- | Mark specified notification as already seen by specific user. Note that
-- we use 'ReleaseId' to select notification, so this may result in several
-- notifications marked as “read” if they happen to be about the same
-- release. This is generally what you want.
markAsRead
:: ReleaseId -- ^ Release in question
-> UserId -- ^ User who has seen mentioned release
-> SqlPersistM ()
markAsRead release user = do
ns <- selectKeysList [ NotificationRelease ==. release ] []
updateWhere [ NotifiedAboutWhat <-. ns
, NotifiedUnread ==. True
, NotifiedUser ==. user ]
[ NotifiedUnread =. False ]
这样可行,但是将通知列表提取为 list ,然后使用它来选择另一个表中的内容......这样做并不完全正确。显然我需要在这里加入,然后我将能够有效地更新所有内容。
如何在纯persistent
中执行此操作?是否有可能在这种情况下与persistent
保持这种任务是一个好主意?我应该使用esqueleto
吗?看起来我需要学习不同的DSL才能使用它,所以我不确定是否要切换。
如何使用markAsRead
正确编写persistent
(如果可能)?
答案 0 :(得分:1)
是Esqueleto,如果你想加入。如果您的数据库和数据建模支持,那么持久性可以很好地与嵌入数据配合使用。
答案 1 :(得分:1)
正如格雷格所说,Esqueleto是要走的路。您可以尝试阅读its main module documentation。
目前Esqueleto不支持UPDATE
上的联接。但是,您可以使用子查询获得相同的效果。
未经测试的代码可以帮助您入门:
-- | Mark specified notification as already seen by specific user. Note that
-- we use 'ReleaseId' to select notification, so this may result in several
-- notifications marked as “read” if they happen to be about the same
-- release. This is generally what you want.
markAsRead
:: ReleaseId -- ^ Release in question
-> UserId -- ^ User who has seen mentioned release
-> SqlPersistM ()
markAsRead release user =
update $ \n -> do
set n [ NotifiedUnread =. val False ]
where_ $
n ^. NotifiedUnread ==. val True &&.
n ^. NotifiedUser ==. val user &&.
n ^. NotifiedAboutWhat `in_`
(subList_select $
from $ \t -> do
where_ $ t ^. NotificationRelease ==. val release
return $ t ^. NotificationId)