用于Yesod / Persistent的MongoDB示例 [英] MongoDB Example for Yesod / Persistent
问题描述
-
在sqlite的示例中:
share [mkPersist sqlSettings,mkMigratemigrateAll] [persist |
Yesod书中说Mongo代码将使用
mongoSettings
代替。但是我无法在任何模块中找到它,并且代码不能编译。所以相反,我不得不使用它来代替mongoSettings
:
MkPersistSettings {mpsBackend = ConT''Action}
我必须导入Language.Haskell.TH.Syntax才能编译它,我假设应该对用户隐藏,所以我当然不会这样做。
另外,我发现在Mongo的持久测试中没有共享和迁移部分。我实际上不确定为什么,我猜这是因为MongoDB是无模式的,不需要迁移?
-
SqlPersist
我认为
MongoPersist
会与SqlPersist相对应,几乎是 - 我在Persistent的test目录下的init.hs中找到了MongoPersist
的一个实例。但它被评论了,所以我有一种感觉它已经过时了?无论如何,据我所知,它还没有定义。所以我不知道如何转换下面的行(Yesod书的P.115,或接近 http:// www.yesodweb.com/book/persistent )为MongoDB工作:instance YesodPersist PersistTest其中
类型YesodPersistBackend PersistTest = SqlPersist
runDB action = do
PersistTest池< - getYesod
runSqlPool动作池
-
使用withMongoDBConn
因此,对于sqlite,代码是(来自上面网页的第一个示例):
$ b $main :: IO()
main = withSqliteConn:memory:$ runSqlConn $ do
runMigration migrateAll
johnId< - 插入$ PersonJohn Doe$只需35
...等等
但是
main :: IO()
main = withMongoDBConn:memory:$ runMongoDBConn $ do
runMigration migrateAll
johnId< - inse rt $ PersonJohn Doe$只需35
...等等
不起作用。首先,由于某种原因, runMigration
不在范围内。好的,也许我不需要迁移MongoDB,所以我删除了该行。然后,编译器会抱怨:
无法与预期类型匹配
AccessMode'与实际类型 m0 b0'
等等。在这个阶段,我对monads的粗略认识还不足以证明这一点。
预期类型: m0 t0 - > (t 0 - > m 0 b 0) - > AccessMode
实际类型:m0 t0 - > (t 0 - > m 0 b 0) - > m0 b0
总而言之,我在转换集成以及从Sqlite到MongoDB的书中的Yesod示例。有人能给我提供一个Yesod / Persistent with MongoDB的具体例子吗?非常感谢。
我今天在Github Yesod Cookbook上添加了一个页面,它将MongoDB与Persistent结合使用。但是,它并不使用和MongoDBConn
,它也不能避免TH。另外,我解释了为什么我使用单独的YAML配置文件。链接: http://bit.ly/VLvmoK
Haskell and Yesod newbie here. I've been trying to follow the Integration with Yesod example from the Persistent chapter in the Yesod book (http://www.yesodweb.com/book/persistent). The Sqlite compiles and runs fine it seems. However, I'm trying to use MongDB and am having a hard time getting things to work. Specifically:
In the example for sqlite:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist|
The Yesod book says "Mongo code will use
mongoSettings
instead." But I can't find it in any of the modules, and the code doesn't compile. So instead, I had to use this instead ofmongoSettings
:MkPersistSettings { mpsBackend = ConT ''Action }
I had to import Language.Haskell.TH.Syntax to get it to compile, which I'm assuming should be hidden from the user so I'm certainly not doing it right.
Also, I found that in the Persistent tests for Mongo do without the "share" and "migrate" part. I'm actually not sure why, I'm guessing it's because MongoDB is Schema-less and doesn't need migration?
SqlPersist
I thought
MongoPersist
would be the counterpart to SqlPersist, and I guess it almost is--I found one instance ofMongoPersist
in the init.hs in the test directory of Persistent. But it's commented out, so I have a feeling it's obsolete? Otherwise it's not defined anyway as far as I can tell. So I don't know how to convert the following line (P.115 of the Yesod book, or near the end of http://www.yesodweb.com/book/persistent) to work for MongoDB:instance YesodPersist PersistTest where type YesodPersistBackend PersistTest = SqlPersist runDB action = do PersistTest pool <- getYesod runSqlPool action pool
Using withMongoDBConn
So for sqlite, the code is (1st example from the webpage above):
main :: IO () main = withSqliteConn ":memory:" $ runSqlConn $ do runMigration migrateAll johnId <- insert $ Person "John Doe" $ Just 35 ... and so on
But
main :: IO() main = withMongoDBConn ":memory:" $ runMongoDBConn $ do runMigration migrateAll johnId <- insert $ Person "John Doe" $ Just 35 ... and so on
doesn't work. First off, runMigration
for some reason isn't in scope. Okay, maybe I don't need migration for MongoDB, so I remove that line. Then, the compiler complains:
Couldn't match expected type
AccessMode' with actual type m0 b0'
Expected type: m0 t0 -> (t0 -> m0 b0) -> AccessMode
Actual type: m0 t0 -> (t0 -> m0 b0) -> m0 b0
and so on. And by this stage my very cursory knowledge of monads just isn't enough to figure this one out.
All in all, I am having a really tough time converting the Integration with Yesod example from the book from Sqlite to MongoDB. Could someone please provide me with a concrete example of Yesod/Persistent with MongoDB? Much thanks in advance.
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
这篇关于用于Yesod / Persistent的MongoDB示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!