I was writing a basic CRUD app in Haskell, using libraries Servant and Opaleye.
Servant to setup the API endpoints and Opaleye to store the data in DB.
Let's say there's an endpoint GET /users
which returns the list of all users from DB and another endpoint POST /user
which creates a new user and saves it in DB.
The program beings by initiating a connection to the DB and then it passes around this connection as a parameter to these API endpoint functions (setup using Servant) as a parameter.
Somebody recommended me that a better way is to use the Reader Monad and store the connection in the environment.
I was able to do it but what I don't get is why is Reader Monad a preferred way of sharing environment rather than directly passing arguments.
P.S. - Being a beginner in Haskell, I can use Monads, follow the tutorials and make my program run but I don't really know the beautiful hidden mathematics behind them. Which is why, I want to avoid using monads (until the time I completely comprehend the idea behind monads).
Here's my code, btw.