From 91504c4e86cfacf8364ff8f37cba0bebb9f0809a Mon Sep 17 00:00:00 2001 From: Tomasz Rybarczyk Date: Mon, 8 Nov 2021 17:48:42 +0100 Subject: [PATCH] Minor refactoring --- README.md | 2 +- src/Database/PostgreSQL/Aff.purs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ab8fd62..7119ce1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Our tests runner reads the configuration for the current process environment or 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. +It requires only database name which we pass to the `newPool` function. Additionally we pass `idleTimeoutMillis` value because this code is run by our test suite and we want to exit after its execution quickly ;-) diff --git a/src/Database/PostgreSQL/Aff.purs b/src/Database/PostgreSQL/Aff.purs index ee05e10..9cdd2b4 100644 --- a/src/Database/PostgreSQL/Aff.purs +++ b/src/Database/PostgreSQL/Aff.purs @@ -49,13 +49,23 @@ import Unsafe.Coerce (unsafeCoerce) -- | PostgreSQL connection. foreign import data Client :: Type +newtype Connection = Connection (Either Pool Client) +derive instance newtypeConnection :: Newtype Connection _ + +fromPool :: Pool -> Connection +fromPool pool = Connection (Left pool) + +fromClient :: Client -> Connection +fromClient client = Connection (Right client) + + -- | PostgreSQL query with parameter (`$1`, `$2`, …) and return types. newtype Query i o = Query String derive instance newtypeQuery :: Newtype (Query i o) _ --- | Run an action with a connection. The connection is released to the pool +-- | Run an action with a client. The client is released to the pool -- | when the action returns. withClient :: forall a. @@ -152,17 +162,8 @@ withClientTransaction client action = rollback = execute conn (Query "ROLLBACK TRANSACTION") Row0 -newtype Connection = Connection (Either Pool Client) -derive instance newtypeConnection :: Newtype Connection _ - -fromPool :: Pool -> Connection -fromPool pool = Connection (Left pool) - -fromClient :: Client -> Connection -fromClient client = Connection (Right client) - -- | APIs of the `Pool.query` and `Client.query` are the same. --- | We can dse this polyformphis to simplify ffi. +-- | We can use this polyformphis to simplify ffi. foreign import data UntaggedConnection :: Type -- | Execute a PostgreSQL query and discard its results.