From dd0011687c25d3197cc4e453d5a244afdfe81fb8 Mon Sep 17 00:00:00 2001 From: Tomasz Rybarczyk Date: Tue, 10 Nov 2020 17:25:17 +0100 Subject: [PATCH] Use config loader in the README example --- .env-example | 9 ++++++++- README.md | 12 +++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.env-example b/.env-example index 5e16c3e..317150e 100644 --- a/.env-example +++ b/.env-example @@ -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 diff --git a/README.md b/README.md index 8930d02..3e39dca 100644 --- a/README.md +++ b/README.md @@ -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.