How to perform database queries in GHCi in Yesod Application

♀尐吖头ヾ 提交于 2019-11-30 14:12:32

If you just want to do Persistent queries in ghci you can do this without creating a Yesod application. Unfortunately doing this is differs quite a bit depending on the specific back end you want to use.

For SQLite:

> import Database.Persist.Sqlite
> import Model
> pool <- createSqlitePool "yesod-test.sqlite3" 2
> runSqlite "yesod-test.sqlite3" (runMigration migrateAll)
> userId <- runSqlite "yesod-test.sqlite3" (insert (User "foo@bar.com" Nothing))

For Postgresql:

-- In Shell: $ createdb yesod-test
> import Database.PostgreSQL.Simple
> con <- connectPostgreSQL  "dbname=yesod-test"
> import Database.Persist.Postgresql
> pcon <- openSimpleConn con
> import Model
> runSqlPersistM (runMigration migrateAll) pcon
> userId <- runSqlPersistM (insert (User "foo@bar.com" Nothing)) pcon
> Just user <- runSqlPersistM (get userId) pcon
> userIdent user

The scaffolding provides (at least with yesod-bin 1.4.5) a function db in Application.hs which you can use:

$ cabal repl
...
*Application> db $ insert $ User "foo@bar.com" Nothing
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!