Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Yesod's DB actions in the REPL?

It is easy to run DB actions in the normal Handler workflow, since the runDB function can be used to transform the SqlPersistM actions into Handler ones.

But there is no such way to convert SqlPersistM directly into IO using the default app settings. Looking at the Foundation.hs as its defined in the application scaffold, there is the following instance

instance YesodPersist App where
    type YesodPersistBackend App = SqlBackend
    runDB action = do
        master <- getYesod
        runSqlPool action $ appConnPool master
instance YesodPersistRunner App where
    getDBRunner = defaultGetDBRunner appConnPool

which basically uses runSqlPool with the app's config, but I don't see an easy way how to leverage this to access the config form within the REPL.

TL;DR: What I'm looking for is simply being able to do something like runDB $ selectList [...] [...] from within cabal repl in my Yesod app, without having to duplicate the setup that Yesod scaffolding does out of the box.

like image 650
Jakub Arnold Avatar asked Feb 12 '15 21:02

Jakub Arnold


People also ask

How do I use database with my REPL?

When you click on the Database icon in the sidebar, you'll see some instructions. If your repl is in a language that has an official Database client, you can quickly import it and start using Database by clicking on the "Insert" buttons. If your language does not have a client, we provide some curl examples.

Where can I find the key-value database in my REPL?

When viewing your repl, you'll find the Database icon toward the bottom of the sidebar – it's the second last icon. That’s Replit’s key-value database, built right into your repl!

Is replit a NoSQL database?

Technically Repl.it DB is NoSQL. no, it's a key value database through and through. Why am I still asking questions about this? How does the code know that the user is the same as name?

What happened to REPL run?

We decided to sunset repl.run, which was a way to publish terminal apps as websites. repl.run urls will now redirect to the source repls where users can click "run" and use the repl. More information about what led to deprecation can be found here.


1 Answers

If you're using the Yesod scaffolding, the handler and db functions are provided to let you run handler actions and database queries, respectively, from the repl:

$ cabal repl

db $ selectList [UserName ==. "foo"] []

Edit: I've also updated Yesod's wiki page on GHCi with this information. It includes more examples and covers some advanced usage, like using the debugger.

like image 140
MaxGabriel Avatar answered Sep 28 '22 00:09

MaxGabriel