haskell – 如何在Yesod应用程序中的GHCi中执行数据库查询

前端之家收集整理的这篇文章主要介绍了haskell – 如何在Yesod应用程序中的GHCi中执行数据库查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例如,如何使用Yesod应用程序的模型将新用户插入数据库?或者,还有更好的方法

我正在处理脚手架应用程序.现在我创建了App实例,并且不知道如何使用它来执行请求.

:i Extra
data Extra
  = Extra {extraCopyright :: Data.Text.Internal.Text,extraAnalytics :: Maybe Data.Text.Internal.Text}
        -- Defined in `Settings

let e = Extra "asdf" Nothing
let c = AppConfig {appEnv = Development,appPort = 3000,appRoot = "/",appHost = "localhost",appExtra = e}
f <- makeFoundation c
:t f
f :: App

:i App
data App
  = App {settings :: AppConfig DefaultEnv Extra,getStatic :: Yesod.Static.Static,connPool :: persistent-1.2.3.0:Database.Persist.Class.PersistConfig.PersistConfigPool
                       PersistConf,httpManager :: http-client-0.2.0.1:Network.HTTP.Client.Types.Manager,persistConfig :: PersistConf,appLogger :: Yesod.Core.Types.Logger}
        -- Defined in `Foundation'

接下来是什么?

解决方法

如果您只想在ghci中执行持久性查询,则可以在不创建Yesod应用程序的情况下执行此操作.不幸的是,这取决于你想要使用的特定后端,这有很大的不同.

对于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))

对于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

猜你在找的MsSQL相关文章