Adding a DB Connection Pool to a Web Application

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding a DB Connection Pool to a Web Application

Steven Leiva
Hi Everyone, 

I am trying to set up a simple web application, but with all the trappings of a production app. (This is strictly for learning purposes). 

At the moment, I am trying to get a database pool up and running that the rest of the application can use. You can see my current progress at this repository. You may want to look at only the code in the back-end folder, as there is a whole front-end application.

So, again, my goal is to get a database pool up and running. At the moment, if I try to build the executable target, I get the following error:


Error
                  
/Users/stevenleiva/Repos/todo/back-end/app/Main.hs:10:13: error:
• Couldn't match type ‘persistent-2.7.0:Database.Persist.Class.PersistStore.BaseBackend
backend0’
with ‘Database.Persist.Sql.Types.Internal.SqlBackend’
arising from a use of ‘createPostgresqlPool’
The type variable ‘backend0’ is ambiguous
• In a stmt of a 'do' block: pool <- createPostgresqlPool connStr 1
In the expression:
do { pool <- createPostgresqlPool connStr 1;
startApp }
In an equation for ‘main’:
main
= do { pool <- createPostgresqlPool connStr 1;
startApp }
Mixmax Not using Mixmax yet?


I tried helping out the compiler by adding pool :: Pool Connection, after importing those type constructors / constants. At that point, I got a different error:


Error 2
                  
/Users/stevenleiva/Repos/todo/back-end/app/Main.hs:11:32: error:
• No instance for (monad-logger-0.3.25.1:Control.Monad.Logger.MonadLogger
IO)
arising from a use of ‘createPostgresqlPool’
• In a stmt of a 'do' block:
pool :: Pool Connection <- createPostgresqlPool connStr 1
In the expression:
do { pool :: Pool Connection <- createPostgresqlPool connStr 1;
startApp }
In an equation for ‘main’:
main
= do { pool :: Pool Connection <- createPostgresqlPool connStr 1;
startApp }
Mixmax Not using Mixmax yet?


I understand that createPostgresqlPool eventually gives me a value of type m (Pool Connection), where there are some constraints on m. I also understand that at the moment I am getting back a value IO (Pool Connection), and that IO does not have an instance of MonadLogger

From here, I am at a bit of a lose on how to proceed. I thought implementing an instance of MonadLogger for IO that would simply typecheck (and not log), but that would lead to an orphaned instance, correct? What's the easiest next step to make forward progress here? 

P.S. I am aware that there are a lot of advanced (for me anyway) Haskell that can be done via monad transformers, natural transformations, etc., in order to wire up a DB with Servant, but I am happy with the "next simplest step" until I learn some of those concepts better. (Then Haskell makes refactoring a breeze!).



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Adding a DB Connection Pool to a Web Application

William Yager

You can mentally replace “m” with “IO”. 

It takes some monadic action that requires logging (like setting up a database pool) and provides a concrete implementation of how logging should be done (like printing messages to stdout), thus discharging the MonadLogger constraint. See “MTL style”. 

I think MonadLogger is as good an introduction to monad transformers as any, since it’s fairly straightforward. Most of the instances are like the reader monad, where they just pass around some function that takes the log details and does something with them. 


On Oct 26, 2017, at 8:43 AM, Steven Leiva <[hidden email]> wrote:

 I thought implementing an instance of MonadLogger for IO that would simply typecheck (and not log), but that would lead to an orphaned instance, correct? What's the easiest next step to make forward progress here? 

P.S. I am aware that there are a lot of advanced (for me anyway) Haskell that can be done via monad transformers, natural transformations, etc., in order to wire up a DB with Servant, but I am happy with the "next simplest step" until I learn some of those concepts better. (Then Haskell makes refactoring a breeze!).



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Adding a DB Connection Pool to a Web Application

Steven Leiva
Hi will - I wanted to thank you for your reply. 

It seems like with runStderrLoggingT, I'd have to start learning about monad transformers. I know that this is something that I have to learn eventually; I'm 100% convinced of that, but for now I am going with an orphaned instance of MonadLogger IO. As soon as I wire up the database, I'll start on monad transformers. I've been told that Stephen Diehl is a good resource for it. 

Thank you very much.



On Thu, Oct 26, 2017 9:33 AM, Will Yager [hidden email] wrote:

You can mentally replace “m” with “IO”. 

It takes some monadic action that requires logging (like setting up a database pool) and provides a concrete implementation of how logging should be done (like printing messages to stdout), thus discharging the MonadLogger constraint. See “MTL style”. 

I think MonadLogger is as good an introduction to monad transformers as any, since it’s fairly straightforward. Most of the instances are like the reader monad, where they just pass around some function that takes the log details and does something with them. 


On Oct 26, 2017, at 8:43 AM, Steven Leiva <[hidden email]> wrote:

 I thought implementing an instance of MonadLogger for IO that would simply typecheck (and not log), but that would lead to an orphaned instance, correct? What's the easiest next step to make forward progress here? 

P.S. I am aware that there are a lot of advanced (for me anyway) Haskell that can be done via monad transformers, natural transformations, etc., in order to wire up a DB with Servant, but I am happy with the "next simplest step" until I learn some of those concepts better. (Then Haskell makes refactoring a breeze!).




Steven Leiva
305.528.6038
[hidden email]
http://www.linkedin.com/in/stevenleiva

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.