Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch entity by `Int` when a `Key` is expected in the Haskell persistent library?

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

like image 967
Daiver Avatar asked Jan 21 '15 13:01

Daiver


1 Answers

You can use toSqlKey for that.

like image 77
Michael Snoyman Avatar answered Nov 16 '22 02:11

Michael Snoyman