Skip to content

Commit c71db7e

Browse files
authored
Merge pull request epost#21 from justinwoo/0.12
updates for 0.12, remove foreign-generics
2 parents 49eea03 + 5e3ae0e commit c71db7e

File tree

4 files changed

+115
-155
lines changed

4 files changed

+115
-155
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: 54 additions & 69 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,20 +22,17 @@ 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)
3125
import Control.Monad.Error.Class (throwError)
32-
import Control.Monad.Except (runExcept)
3326
import Data.Array ((!!))
3427
import Data.Either (Either, either)
35-
import Data.Foreign (Foreign, MultipleErrors)
36-
import Data.Foreign.Class (class Decode, decode)
3728
import Data.Maybe (Maybe(Just, Nothing), maybe)
3829
import Data.Traversable (sequence)
3930
import Database.Postgres.SqlValue (SqlValue)
31+
import Effect (Effect)
32+
import Effect.Aff (Aff, Error, bracket)
33+
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
34+
import Effect.Class (liftEffect)
35+
import Foreign (Foreign)
4036
import Unsafe.Coerce (unsafeCoerce)
4137

4238
newtype Query a = Query String
@@ -45,8 +41,6 @@ foreign import data Pool :: Type
4541

4642
foreign import data Client :: Type
4743

48-
foreign import data DB :: Effect
49-
5044
foreign import data ConnectionInfo :: Type
5145

5246
type ConnectionString = String
@@ -90,93 +84,84 @@ connectionInfoFromConfig c p = unsafeCoerce
9084
}
9185

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

9690
-- | 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
91+
execute :: forall a. Query a -> Array SqlValue -> Client -> Aff Unit
92+
execute (Query sql) params client = void $ fromEffectFnAff $ runQuery sql params client
9993

10094
-- | 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
95+
execute_ :: forall a. Query a -> Client -> Aff Unit
96+
execute_ (Query sql) client = void $ fromEffectFnAff $ runQuery_ sql client
10397

10498
-- | 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))
99+
query :: forall a
100+
. (Foreign -> Either Error a) -> Query a -> Array SqlValue -> Client -> Aff (Array a)
101+
query decode (Query sql) params client = do
102+
rows <- fromEffectFnAff $ runQuery sql params client
103+
either throwError pure (sequence $ decode <$> rows)
111104

112105
-- | 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))
106+
query_ :: forall a
107+
. (Foreign -> Either Error a) -> Query a -> Client -> Aff (Array a)
108+
query_ decode (Query sql) client = do
109+
rows <- fromEffectFnAff $ runQuery_ sql client
110+
either throwError pure (sequence $ decode <$> rows)
119111

120112
-- | 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)
113+
queryOne :: forall a
114+
. (Foreign -> Either Error a) -> Query a -> Array SqlValue -> Client -> Aff (Maybe a)
115+
queryOne decode (Query sql) params client = do
116+
rows <- fromEffectFnAff $ runQuery sql params client
117+
maybe (pure Nothing) (either throwError (pure <<< Just)) (decodeFirst decode rows)
127118

128119
-- | 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)
120+
queryOne_ :: forall a
121+
. (Foreign -> Either Error a) -> Query a -> Client -> Aff (Maybe a)
122+
queryOne_ decode (Query sql) client = do
123+
rows <- fromEffectFnAff $ runQuery_ sql client
124+
maybe (pure Nothing) (either throwError (pure <<< Just)) (decodeFirst decode rows)
135125

136126
-- | 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))
127+
queryValue :: forall a
128+
. (Foreign -> Either Error a) -> Query a -> Array SqlValue -> Client -> Aff (Maybe a)
129+
queryValue decode (Query sql) params client = do
130+
val <- fromEffectFnAff $ runQueryValue sql params client
131+
pure $ either (const Nothing) Just (decode val)
143132

144133
-- | 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)
134+
queryValue_ :: forall a
135+
. (Foreign -> Either Error a) -> Query a -> Client -> Aff (Maybe a)
136+
queryValue_ decode (Query sql) client = do
137+
val <- fromEffectFnAff $ runQueryValue_ sql client
138+
either throwError (pure <<< Just) $ (decode val)
151139

152140
-- | Connects to the database, calls the provided function with the client
153141
-- | and returns the results.
154-
withClient :: forall eff a
155-
. Pool -> (Client -> Aff (db :: DB | eff) a) -> Aff (db :: DB | eff) a
142+
withClient :: forall a
143+
. Pool -> (Client -> Aff a) -> Aff a
156144
withClient pool p =
157145
bracket
158146
(connect pool)
159-
(liftEff <<< release)
147+
(liftEffect <<< release)
160148
p
161149

162-
decodeFirst :: forall a. Decode a => Array Foreign -> Maybe (Either MultipleErrors a)
163-
decodeFirst rows = runExcept <<< decode <$> (rows !! 0)
164-
165-
liftError :: forall e a. MultipleErrors -> Aff e a
166-
liftError errs = throwError $ error (show errs)
150+
decodeFirst :: forall a. (Foreign -> Either Error a) -> Array Foreign -> Maybe (Either Error a)
151+
decodeFirst decode rows = decode <$> (rows !! 0)
167152

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

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

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

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

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

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

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

182-
foreign import end :: forall eff. Pool -> Eff (db :: DB | eff) Unit
167+
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)