rightfold / purescript-postgresql-client

https://pursuit.purescript.org/packages/purescript-postgresql-client
BSD 3-Clause "New" or "Revised" License
35 stars 20 forks source link

Does newPool need to be Aff? #31

Closed rndnoise closed 5 years ago

rndnoise commented 5 years ago

This may be more trouble than it's worth, but I wish newPool returned simply Effect Pool rather than Aff Pool (or that there were two functions). This seems necessary in some cases, for instance to initialize the Pool a single time before passing it to router with HTTPure:

import HTTPure as H
import Database.PostgreSQL as P

main :: Effect Unit
main = do
  pool <- P.newPool P.defaultPoolConfiguration -- type error: Aff Pool vs Effect Pool
  H.serve 8080 (router pool) (\_ -> pure unit)

router :: P.Pool -> H.Request -> Aff H.Response
router pool { body, method } = do
  P.withConnection pool \c -> do
    -- ...
    H.ok "..."

The workaround I'm using is to pass PoolConfiguration to router and calling newPool inside, but I'm not sure if that does what I want. It seems this would result in a separate pool for each request, rather than a single one shared among requests. Looking at the pg-pool library, it seems that pg.Pool(...) calls a constructor and doesn't return a promise.

If you agree and prefer to have someone submit a PR, I would be happy to submit one. Though I think most of the work is deciding whether to change newPool's return type, or to create a new function.

paluh commented 5 years ago

Hi @rndnoise,

Good catch! I see that we can have newPool just in Effect by droping liftEffect from it. I'm not sure why we are wrapping it in Aff ;-) I can incorporate this change myself because we are expecting a new release this week but if you want to play with the lib you are more than welcome to provide a PR :-)

rndnoise commented 5 years ago

Great! I'll look forward to the release :-)

paluh commented 5 years ago

This is done on master so I'm adding this to v3.0.0 milestone and closing this issue :-)