Skip to content

Commit def2ce3

Browse files
committed
updates for 0.12
removes dependency on foreign-generics and allows the user to supply their own Foreign -> Either b a function, e.g. Simple-JSON for record alias decoding.
1 parent 49eea03 commit def2ce3

File tree

4 files changed

+119
-152
lines changed

4 files changed

+119
-152
lines changed

bower.json

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@
2626
"output"
2727
],
2828
"dependencies": {
29-
"purescript-arrays": "^4.2.1",
30-
"purescript-either": "^3.1.0",
31-
"purescript-foreign": "^4.0.1",
32-
"purescript-foldable-traversable": "^3.6.1",
33-
"purescript-transformers": "^3.4.0",
34-
"purescript-aff": "^4.0.0",
35-
"purescript-integers": "^3.1.0",
36-
"purescript-datetime": "^3.4.0",
37-
"purescript-unsafe-coerce": "^3.0.0",
38-
"purescript-nullable": "^3.0.0",
39-
"purescript-prelude": "^3.1.0",
40-
"purescript-foreign-generic": "^5.0.0"
29+
"purescript-arrays": "^5.0.0",
30+
"purescript-either": "^4.0.0",
31+
"purescript-foreign": "^5.0.0",
32+
"purescript-foldable-traversable": "^4.0.0",
33+
"purescript-transformers": "^4.1.0",
34+
"purescript-aff": "^5.0.0",
35+
"purescript-integers": "^4.0.0",
36+
"purescript-datetime": "^4.0.0",
37+
"purescript-unsafe-coerce": "^4.0.0",
38+
"purescript-nullable": "^4.0.0",
39+
"purescript-prelude": "^4.0.0"
4140
},
4241
"devDependencies": {
43-
"purescript-spec": "^2.0.0",
44-
"purescript-generics": "^4.0.0",
45-
"purescript-js-date": "^5.1.0"
42+
"purescript-spec": "^3.0.0",
43+
"purescript-js-date": "^6.0.0",
44+
"purescript-simple-json": "^4.0.0"
4645
}
4746
}

src/Database/Postgres.purs

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Database.Postgres
22
( Query(..)
33
, Client
44
, Pool
5-
, DB
65
, ConnectionInfo
76
, ClientConfig
87
, PoolConfig
@@ -23,17 +22,15 @@ module Database.Postgres
2322

2423
import Prelude
2524

26-
import Control.Monad.Aff (Aff, bracket)
27-
import Control.Monad.Aff.Compat (EffFnAff, fromEffFnAff)
28-
import Control.Monad.Eff (kind Effect, Eff)
29-
import Control.Monad.Eff.Class (liftEff)
30-
import Control.Monad.Eff.Exception (error)
25+
import Effect.Aff (Aff, bracket)
26+
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
27+
import Effect (Effect)
28+
import Effect.Class (liftEffect)
29+
import Effect.Exception (error)
3130
import Control.Monad.Error.Class (throwError)
32-
import Control.Monad.Except (runExcept)
3331
import Data.Array ((!!))
3432
import Data.Either (Either, either)
35-
import Data.Foreign (Foreign, MultipleErrors)
36-
import Data.Foreign.Class (class Decode, decode)
33+
import Foreign (Foreign)
3734
import Data.Maybe (Maybe(Just, Nothing), maybe)
3835
import Data.Traversable (sequence)
3936
import Database.Postgres.SqlValue (SqlValue)
@@ -45,8 +42,6 @@ foreign import data Pool :: Type
4542

4643
foreign import data Client :: Type
4744

48-
foreign import data DB :: Effect
49-
5045
foreign import data ConnectionInfo :: Type
5146

5247
type ConnectionString = String
@@ -90,93 +85,94 @@ connectionInfoFromConfig c p = unsafeCoerce
9085
}
9186

9287
-- | Makes a connection to the database via a Client.
93-
connect :: forall eff. Pool -> Aff (db :: DB | eff) Client
94-
connect = fromEffFnAff <<< connect'
88+
connect :: Pool -> Aff Client
89+
connect = fromEffectFnAff <<< connect'
9590

9691
-- | Runs a query and returns nothing.
97-
execute :: forall eff a. Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) Unit
98-
execute (Query sql) params client = void $ fromEffFnAff $ runQuery sql params client
92+
execute :: forall a. Query a -> Array SqlValue -> Client -> Aff Unit
93+
execute (Query sql) params client = void $ fromEffectFnAff $ runQuery sql params client
9994

10095
-- | Runs a query and returns nothing
101-
execute_ :: forall eff a. Query a -> Client -> Aff (db :: DB | eff) Unit
102-
execute_ (Query sql) client = void $ fromEffFnAff $ runQuery_ sql client
96+
execute_ :: forall a. Query a -> Client -> Aff Unit
97+
execute_ (Query sql) client = void $ fromEffectFnAff $ runQuery_ sql client
10398

10499
-- | Runs a query and returns all results.
105-
query :: forall eff a
106-
. Decode a
107-
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Array a)
108-
query (Query sql) params client = do
109-
rows <- fromEffFnAff $ runQuery sql params client
110-
either liftError pure (runExcept (sequence $ decode <$> rows))
100+
query :: forall b a
101+
. Show b
102+
=> (Foreign -> Either b a) -> Query a -> Array SqlValue -> Client -> Aff (Array a)
103+
query decode (Query sql) params client = do
104+
rows <- fromEffectFnAff $ runQuery sql params client
105+
either liftError pure (sequence $ decode <$> rows)
111106

112107
-- | Just like `query` but does not make any param replacement
113-
query_ :: forall eff a
114-
. Decode a
115-
=> Query a -> Client -> Aff (db :: DB | eff) (Array a)
116-
query_ (Query sql) client = do
117-
rows <- fromEffFnAff $ runQuery_ sql client
118-
either liftError pure (runExcept (sequence $ decode <$> rows))
108+
query_ :: forall b a
109+
. Show b
110+
=> (Foreign -> Either b a) -> Query a -> Client -> Aff (Array a)
111+
query_ decode (Query sql) client = do
112+
rows <- fromEffectFnAff $ runQuery_ sql client
113+
either liftError pure (sequence $ decode <$> rows)
119114

120115
-- | Runs a query and returns the first row, if any
121-
queryOne :: forall eff a
122-
. Decode a
123-
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
124-
queryOne (Query sql) params client = do
125-
rows <- fromEffFnAff $ runQuery sql params client
126-
maybe (pure Nothing) (either liftError (pure <<< Just)) (decodeFirst rows)
116+
queryOne :: forall b a
117+
. Show b
118+
=> (Foreign -> Either b a) -> Query a -> Array SqlValue -> Client -> Aff (Maybe a)
119+
queryOne decode (Query sql) params client = do
120+
rows <- fromEffectFnAff $ runQuery sql params client
121+
maybe (pure Nothing) (either liftError (pure <<< Just)) (decodeFirst decode rows)
127122

128123
-- | Just like `queryOne` but does not make any param replacement
129-
queryOne_ :: forall eff a
130-
. Decode a
131-
=> Query a -> Client -> Aff (db :: DB | eff) (Maybe a)
132-
queryOne_ (Query sql) client = do
133-
rows <- fromEffFnAff $ runQuery_ sql client
134-
maybe (pure Nothing) (either liftError (pure <<< Just)) (decodeFirst rows)
124+
queryOne_ :: forall b a
125+
. Show b
126+
=> (Foreign -> Either b a) -> Query a -> Client -> Aff (Maybe a)
127+
queryOne_ decode (Query sql) client = do
128+
rows <- fromEffectFnAff $ runQuery_ sql client
129+
maybe (pure Nothing) (either liftError (pure <<< Just)) (decodeFirst decode rows)
135130

136131
-- | Runs a query and returns a single value, if any.
137-
queryValue :: forall eff a
138-
. Decode a
139-
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
140-
queryValue (Query sql) params client = do
141-
val <- fromEffFnAff $ runQueryValue sql params client
142-
pure $ either (const Nothing) Just (runExcept (decode val))
132+
queryValue :: forall b a
133+
. Show b
134+
=> (Foreign -> Either b a) -> Query a -> Array SqlValue -> Client -> Aff (Maybe a)
135+
queryValue decode (Query sql) params client = do
136+
val <- fromEffectFnAff $ runQueryValue sql params client
137+
pure $ either (const Nothing) Just (decode val)
143138

144139
-- | Just like `queryValue` but does not make any param replacement
145-
queryValue_ :: forall eff a
146-
. Decode a
147-
=> Query a -> Client -> Aff (db :: DB | eff) (Maybe a)
148-
queryValue_ (Query sql) client = do
149-
val <- fromEffFnAff $ runQueryValue_ sql client
150-
either liftError (pure <<< Just) $ runExcept (decode val)
140+
queryValue_ :: forall b a
141+
. Show b
142+
=> (Foreign -> Either b a) -> Query a -> Client -> Aff (Maybe a)
143+
queryValue_ decode (Query sql) client = do
144+
val <- fromEffectFnAff $ runQueryValue_ sql client
145+
either liftError (pure <<< Just) $ (decode val)
151146

152147
-- | Connects to the database, calls the provided function with the client
153148
-- | and returns the results.
154-
withClient :: forall eff a
155-
. Pool -> (Client -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a
149+
withClient :: forall a
150+
. Pool -> (Client -> Aff a) -> Aff a
156151
withClient pool p =
157152
bracket
158153
(connect pool)
159-
(liftEff <<< release)
154+
(liftEffect <<< release)
160155
p
161156

162-
decodeFirst :: forall a. Decode a => Array Foreign -> Maybe (Either MultipleErrors a)
163-
decodeFirst rows = runExcept <<< decode <$> (rows !! 0)
157+
decodeFirst :: forall b a. Show b
158+
=> (Foreign -> Either b a) -> Array Foreign -> Maybe (Either b a)
159+
decodeFirst decode rows = decode <$> (rows !! 0)
164160

165-
liftError :: forall e a. MultipleErrors -> Aff e a
161+
liftError :: forall a b. Show b => b -> Aff a
166162
liftError errs = throwError $ error (show errs)
167163

168-
foreign import mkPool :: forall eff. ConnectionInfo -> Eff (db :: DB | eff) Pool
164+
foreign import mkPool :: ConnectionInfo -> Effect Pool
169165

170-
foreign import connect' :: forall eff. Pool -> EffFnAff (db :: DB | eff) Client
166+
foreign import connect' :: Pool -> EffectFnAff Client
171167

172-
foreign import runQuery_ :: forall eff. String -> Client -> EffFnAff (db :: DB | eff) (Array Foreign)
168+
foreign import runQuery_ :: String -> Client -> EffectFnAff (Array Foreign)
173169

174-
foreign import runQuery :: forall eff. String -> Array SqlValue -> Client -> EffFnAff (db :: DB | eff) (Array Foreign)
170+
foreign import runQuery :: String -> Array SqlValue -> Client -> EffectFnAff (Array Foreign)
175171

176-
foreign import runQueryValue_ :: forall eff. String -> Client -> EffFnAff (db :: DB | eff) Foreign
172+
foreign import runQueryValue_ :: String -> Client -> EffectFnAff Foreign
177173

178-
foreign import runQueryValue :: forall eff. String -> Array SqlValue -> Client -> EffFnAff (db :: DB | eff) Foreign
174+
foreign import runQueryValue :: String -> Array SqlValue -> Client -> EffectFnAff Foreign
179175

180-
foreign import release :: forall eff. Client -> Eff (db :: DB | eff) Unit
176+
foreign import release :: Client -> Effect Unit
181177

182-
foreign import end :: forall eff. Pool -> Eff (db :: DB | eff) Unit
178+
foreign import end :: Pool -> Effect Unit

src/Database/Postgres/Transaction.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module Database.Postgres.Transaction where
22

33
import Prelude
4-
import Control.Monad.Aff (Aff, attempt)
4+
import Effect.Aff (Aff, attempt)
55
import Control.Monad.Error.Class (throwError)
66
import Data.Either (either)
77

8-
import Database.Postgres (DB, Client, Query(Query), execute_)
8+
import Database.Postgres (Client, Query(Query), execute_)
99

1010
-- | Runs an asynchronous action in a database transaction. The transaction
1111
-- | will be rolled back if the computation fails and committed otherwise.
@@ -19,7 +19,7 @@ import Database.Postgres (DB, Client, Query(Query), execute_)
1919
-- | throwError $ error "Something went wrong"
2020
-- | execute_ (Query "insert into accounts ...") c
2121
-- | ```
22-
withTransaction :: forall eff a. (Client -> Aff (db :: DB | eff) a) -> Client -> Aff (db :: DB | eff) a
22+
withTransaction :: forall a. (Client -> Aff a) -> Client -> Aff a
2323
withTransaction act client = do
2424
begin client
2525
res <- attempt (act client)
@@ -28,11 +28,11 @@ withTransaction act client = do
2828
rollback_ err = rollback client *> throwError err
2929
commit_ v = commit client *> pure v
3030

31-
begin :: forall eff. Client -> Aff (db :: DB | eff) Unit
31+
begin :: Client -> Aff Unit
3232
begin = execute_ (Query "BEGIN TRANSACTION")
3333

34-
commit :: forall eff. Client -> Aff (db :: DB | eff) Unit
34+
commit :: Client -> Aff Unit
3535
commit = execute_ (Query "COMMIT")
3636

37-
rollback :: forall eff. Client -> Aff (db :: DB | eff) Unit
37+
rollback :: Client -> Aff Unit
3838
rollback = execute_ (Query "ROLLBACK")

0 commit comments

Comments
 (0)