Yesod - 获取实体值的函数

Yesod - function for getting the value of an Entity

看看:

myEntity :: Entity Post
myEntity = ...

myValue :: Entity Post -> Post
myValue = <someFunction>

myKey :: Entity Post -> PostId
myKey = <someOtherFunction>

yesod 中是否存在 someFunctionsomeOtherFunction 还是我需要创建它们?

你有Entity key val,然后val

> :t entityVal
entityVal :: Entity record -> record
> :i entityVal
type role Entity nominal
data Entity record where
  Entity :: PersistEntity record =>
            (Key record) -> record -> Entity record
        -- Defined in ‘persistent-2.2.1:Database.Persist.Class.PersistEntity’

key

> :t entityKey
entityKey :: Entity record -> Key record
> :i entityKey
type role Entity nominal
data Entity record where
  Entity :: PersistEntity record =>
            (Key record) -> record -> Entity record
        -- Defined in ‘persistent-2.2.1:Database.Persist.Class.PersistEntity’

或者两者都可以使用模式匹配,例如

> let getR (Entity k v) = (k , v)
> :t getR
getR :: Entity t -> (Key t, t)