Yesod - 获取实体价值的功能

时间:2016-03-15 12:10:13

标签: haskell yesod

看看:

myEntity :: Entity Post
myEntity = ...

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

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

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

1 个答案:

答案 0 :(得分:5)

您有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)