MongoDB Example for Yesod / Persistent

孤街醉人 提交于 2019-12-04 02:10:24

I added a page in the Github Yesod Cookbook today that uses MongoDB in conjunction with Persistent. It does not, however, use withMongoDBConn, neither does it avoid TH. Also, I explain there why I use a separate YAML config file. The link: http://bit.ly/VLvmoK

I know this is an answer to an old question, but here is a Yesod independent way to get Persistent working with MongoDB. This might be of use to others new to Persistent.

{-# LANGUAGE TemplateHaskell #-}

import Database.Persist 
import Database.Persist.TH
import Database.Persist.MongoDB
import Network (PortID (PortNumber))

let mongoSettings = (mkPersistSettings (ConT ''MongoBackend)) {mpsGeneric = False}
    in share [mkPersist mongoSettings] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]
runDBActions actions = 
    withMongoDBConn "myDatabaseName" "localhost" (PortNumber 27017) Nothing 2000 $ \pool ->    
        runMongoDBPool master actions pool

actions = do
    mkey <- insert $ Person "John Doe" $ Just 35
    ...

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