Use config loader in the README example

This commit is contained in:
Tomasz Rybarczyk 2020-11-10 17:25:17 +01:00
parent 4c738fe72f
commit dd0011687c
2 changed files with 15 additions and 6 deletions

View File

@ -1,3 +1,10 @@
PG_DB="purspg"
PG_PORT=6432
PG_PORT=5432
# We want this setup so test runner disconnects quickly
PG_IDLE_TIMEOUT_MILLISECONDS=1000
# You can provide additional options:
# PG_HOST ∷ String
# PG_MAX ∷ Int
# PG_USER ∷ STring
# PG_PASSWORD ∷ String

View File

@ -20,16 +20,17 @@ import Prelude
import Control.Monad.Except.Trans (ExceptT, runExceptT)
import Data.Either (Either(..))
import Database.PostgreSQL (Client, Connection, defaultConfiguration, Pool, Query(Query), PGError)
import Database.PostgreSQL (Connection, Pool, Query(Query), PGError)
import Database.PostgreSQL.PG (command, execute, query, withTransaction) as PG
import Database.PostgreSQL.Pool (new) as Pool
import Database.PostgreSQL.Row (Row0(Row0), Row3(Row3))
import Data.Decimal as Decimal
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Effect.Aff (Aff)
import Effect.Aff.Class (liftAff)
import Effect.Class (liftEffect)
import Test.Assert (assert)
import Test.Config (load) as Config
```
The whole API for interaction with PostgreSQL is performed asynchronously in `Aff`
@ -49,6 +50,8 @@ withTransaction :: forall a. Pool -> (Connection -> AppM a) -> AppM a
withTransaction p = PG.withTransaction runExceptT p
```
Our tests runner reads the configuration for the current process environment or from
the _.env_ file (please check _.env-example_ for details).
We assume here that Postgres is running on a standard local port
with `ident` authentication so configuration can be nearly empty (`defaultConfiguration`).
It requires only database name which we pass to `newPool` function.
@ -59,9 +62,8 @@ is run by our test suite and we want to exit after its execution quickly ;-)
```purescript
run ∷ AppM Unit
run = do
pool <- liftEffect $ Pool.new
((defaultConfiguration "purspg") { idleTimeoutMillis = Just 1000 })
config ← liftAff $ Config.load
pool ← liftEffect $ Pool.new config
```
We can now create our temporary table which we are going to query in this example.