Haskell Persistent: how get entity from db by key if i have key in integer variable?

狂风中的少年 提交于 2019-12-05 17:31:55

问题


I use Persistent orm with scotty web framework.

I want to get value from db by id. These id are coming to me from GET request

There are "get" function that takes "Key Entity" variable and returns "Maybe Entity".

I use following code to get value from db

k <- keyFromValues $ [(PersistInt64 myOwnIntVarFromRequest)]
case k of
    Left _ -> {-some processing-}
    Right x -> do
    t <- liftIO . runDb $ get (x::Key Post) --Post is one of my models
    case t of
        Nothing -> {-processing-}
        Just x -> {-processing-}

These code is extremly ugly. But i don't know how to do it better

So my question is how obtain variable of type "Key Entity" without calling keyFromValues.

PS Sorry for my poor English


回答1:


You can use toSqlKey for that.



来源:https://stackoverflow.com/questions/28068447/haskell-persistent-how-get-entity-from-db-by-key-if-i-have-key-in-integer-varia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!