简体   繁体   English

在Esqueleto中更新具有特定ID的行

[英]Update a row with specific ID in Esqueleto

I can change a field of a row with entryId in Esqueleto like this: 我可以使用entryId中的entryId更改行的字段,如下所示:

  update $ \entry -> do
    set entry [ EntryFoo =. val bar ]
    where_ (entry ^. EntryId ==. val entryId)

However, writing it all the time gets annoying. 但是,一直写它会很烦人。 I'd like to be able to write something like this: 我希望能够编写如下内容:

  updateById entryId $ \entry ->
    set entry [ EntryFoo =. val bar ]

I tried to write this helper by myself, but found that I don't know how to write ^. EntryId 我试图自己写这个助手,但是发现我不知道怎么写^. EntryId ^. EntryId in a generic way (ie a way that would work for any entry types). ^. EntryId以通用方式(即适用于任何条目类型的方式)。 Is it possible? 可能吗? Or am I missing something and updateById already exists in Esqueleto? 还是我错过了一些东西,而updateById已经存在updateById?

I am definitely no expert on esqueleto, but I would guess: 我绝对不是esqueleto的专家,但我会猜测:

updateById entryId upd = update $ \entry -> do
                           upd entry
                           where_ (entry ^. EntryId ==. val entryId)

should solve the problem. 应该解决问题。

For any Entity , ^. EntityId 对于任何Entity^. EntityId ^. EntityId can be written as ^. persistIdField ^. EntityId可以写为^. persistIdField ^. persistIdField (the persistIdField field is a method of the PersistEntity class). ^. persistIdFieldpersistIdField字段是PersistEntity类的方法)。 Thus, your function can be written like this: 因此,您的函数可以这样编写:

updateById
  :: (E.PersistEntityBackend val ~ E.SqlBackend,
      MonadIO m, E.PersistEntity val)
  => Key val
  -> (E.SqlExpr (E.Entity val) -> E.SqlQuery a)
  -> E.SqlWriteT m ()
updateById entryId upd = E.update $ \entry -> do
  upd entry
  E.where_ (entry E.^. E.persistIdField ==. E.val entryId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM