README: Add withTransaction to the docs.

This commit is contained in:
Tomasz Rybarczyk 2018-10-21 11:50:20 +02:00
parent f7f2e3c960
commit 1d75dd5644

View File

@ -18,7 +18,7 @@ module Test.Example where
import Prelude
import Database.PostgreSQL (defaultPoolConfiguration, command, execute, newPool, query, Query(Query), withConnection)
import Database.PostgreSQL (defaultPoolConfiguration, command, execute, newPool, query, Query(Query), withConnection, withTransaction)
import Database.PostgreSQL.Row (Row0(Row0), Row3(Row3))
import Data.Decimal as Decimal
import Data.Maybe (Maybe(..))
@ -63,7 +63,16 @@ The last `Row0` value indicates that this `Query` doesn't take any additional pa
""") Row0
```
We can insert some data calling `execute` function with `INSERT` statement.
There is `withTransaction` helper provided. You can wrap the whole
interaction with database in it. It will rollback if any exception
is thrown during execution of given `Aff` block. It commits in the other case.
```purescript
withTransaction conn $ do
```
Now we can insert some data calling `execute` function with `INSERT` statement.
Please notice that we are passing a tuple of arguments to this query
using dedicated constructor. In this case `Row3`.
This library provides types from `Row0` to `Row19` and they are wrappers which
@ -87,7 +96,6 @@ verbose but is not restricted to limited and constant number of arguments.
INSERT INTO fruits (name, delicious, price)
VALUES ($1, $2, $3)
""") ("lemon" /\ false /\ Decimal.fromString "3.30")
```
Of course `Row*` types and nested tuples can be also used when we are fetching
@ -106,14 +114,13 @@ data from db.
There is also a `command` function at our disposal.
Some postgres SQL expressions return a "command tag" which carries
a value of rows which were affected by a given query.
a value with a number of rows which were affected by a given query.
For example we can have: `DELETE rows`, `UPDATE rows`, `INSERT oid rows` etc.
This function should return `rows` value associated with given response.
```purescript
deleted <- command conn (Query """DELETE FROM fruits """) Row0
liftEffect <<< assert $ deleted == 2
```
## Testing