Search code examples
haskellmongodbhaskell-snap-framework

Access MongoDB from Snap


I'm trying to access mongo using the mongodb haskell drivers (the snap driver appears to be broken for snap > 0.5) in splice.

This is as far as I've got so far:

testSplice :: Splice AppHandler
testSplice = do
  record <- liftIO $ do
    pipe <- runIOE $ connect (host "127.0.0.1") 
    results <- access pipe master "db" (find $ select [] "coll")
    close pipe
    rest result

  return $ [TextNode $ T.pack $ show $ records]

I understand that I need to use liftIO there, as the mongo action occurs inside an IO monad, and I want to pull that back out. Where my understanding breaks down is the result of compiling that splice:

Couldn't match expected type `IO a0'
    with actual type `Action m0 [Database.MongoDB.Document]'

I'm sorry to post a "Send me the codes plz" question, but I'm at loss: where am I going wrong, and how do I make this work?


Solution

  • Here is your function annotated with type signatures. I think this makes it pretty clear where the problem lies.

    testSplice :: Splice AppHandler
    testSplice = do
      record <- liftIO $ do
        pipe <- runIOE $ connect (host "127.0.0.1") -- :: IO Pipe
        results <- access pipe master "db" (find $ select [] "coll")
        -- ^ :: IO (Either Failure Cursor)
        close pipe -- :: IO ()
        rest result -- :: Action m [Document]
    
      return $ [TextNode $ T.pack $ show $ records]
    

    Everything inside the "liftIO $ do" block must be an IO action. The last line "rest result" is not. One solution is to prepend that line with 'access pipe master "db"' just like you've done with find. Another solution is to avoid calling "access pipe..." twice and replace the find line with the following:

    result <- access pipe master "db" (find (select [] "coll") >>= rest)
    

    Then replace the "rest result" line with "return result"

    What Daniel says about the find line not needing liftIO is correct, but in this case it doesn't matter because IO has a MonadIO instance. So it's probably just as easy to keep all the liftIO stuff in one block.