Use of `get` and `toSqlKey` in persistent

孤街醉人 提交于 2019-12-08 11:10:44

问题


I'm trying to use persistent-postgresql with servant.

I have a User model.

I want to have an endpoint that takes an id and returns the user with that id.

According to other answers I can use toSqlKey to turn an Int64 into a Key to feed to get.

My function looks like:

oneUser :: Int64 -> App (Entity User)
oneUser userId = do
  maybeUser <- runDb $ get $ toSqlKey userId
  case maybeUser of
    Nothing ->
      throwError err404
    Just user ->
      return user

When I try to compile I get the error Couldn't match expected type ‘PersistEntityBackend (Entity User)’ with actual type ‘SqlBackend’

Use of selectList works fine.

allUsers :: App [Entity User]
allUsers = runDb $ selectList [] []

Please tell me what I'm doing wrong and where I should look in the future for stuff like this. I couldn't find get on hackage/the right version of the library on stackage etc.

runDb looks like:

runDb :: (MonadReader Config m, MonadIO m) => SqlPersistT IO b -> m b
runDb query = do
  pool <- asks getPool
  liftIO $ runSqlPool query pool

taken from this github project.


回答1:


The difference is that get ... returns a plain User not an Entity User, so this will work:

altSingleUser :: Int64 -> App User
altSingleUser userid = do
    let foo = get (toSqlKey userid) :: SqlPersistT IO (Maybe User)
    maybeUser <- runDb $ foo
    case maybeUser of
         Nothing ->
            throwError err404
         Just person ->
            return person

If you want to return an Entity User, just change the last return statement to:

return $ Entity { entityKey = toSqlKey userid, entityVal = person }


来源:https://stackoverflow.com/questions/39055829/use-of-get-and-tosqlkey-in-persistent

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