@@ -17,12 +17,12 @@ module Database.Postgres
17
17
) where
18
18
19
19
import Prelude
20
- import Control.Monad.Eff (Eff )
20
+ import Control.Monad.Eff (kind Effect , Eff )
21
21
import Data.Either (Either , either )
22
22
import Data.Function.Uncurried (Fn2 (), runFn2 )
23
23
import Data.Array ((!!))
24
24
import Data.Foreign (Foreign , MultipleErrors )
25
- import Data.Foreign.Class (class IsForeign , read )
25
+ import Data.Foreign.Class (class Decode , decode )
26
26
import Data.Maybe (Maybe (Just, Nothing), maybe )
27
27
import Control.Monad.Except (runExcept )
28
28
import Control.Monad.Aff (Aff , finally )
@@ -35,9 +35,9 @@ import Database.Postgres.SqlValue (SqlValue)
35
35
36
36
newtype Query a = Query String
37
37
38
- foreign import data Client :: *
38
+ foreign import data Client :: Type
39
39
40
- foreign import data DB :: !
40
+ foreign import data DB :: Effect
41
41
42
42
type ConnectionString = String
43
43
@@ -72,45 +72,45 @@ execute_ (Query sql) client = void $ runQuery_ sql client
72
72
73
73
-- | Runs a query and returns all results.
74
74
query :: forall eff a
75
- . (IsForeign a )
75
+ . (Decode a )
76
76
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Array a )
77
77
query (Query sql) params client = do
78
78
rows <- runQuery sql params client
79
- either liftError pure (runExcept (sequence $ read <$> rows))
79
+ either liftError pure (runExcept (sequence $ decode <$> rows))
80
80
81
81
-- | Just like `query` but does not make any param replacement
82
- query_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Array a )
82
+ query_ :: forall eff a . (Decode a ) => Query a -> Client -> Aff (db :: DB | eff ) (Array a )
83
83
query_ (Query sql) client = do
84
84
rows <- runQuery_ sql client
85
- either liftError pure (runExcept (sequence $ read <$> rows))
85
+ either liftError pure (runExcept (sequence $ decode <$> rows))
86
86
87
87
-- | Runs a query and returns the first row, if any
88
88
queryOne :: forall eff a
89
- . (IsForeign a )
89
+ . (Decode a )
90
90
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
91
91
queryOne (Query sql) params client = do
92
92
rows <- runQuery sql params client
93
- maybe (pure Nothing ) (either liftError (pure <<< Just )) (readFirst rows)
93
+ maybe (pure Nothing ) (either liftError (pure <<< Just )) (decodeFirst rows)
94
94
95
95
-- | Just like `queryOne` but does not make any param replacement
96
- queryOne_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
96
+ queryOne_ :: forall eff a . (Decode a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
97
97
queryOne_ (Query sql) client = do
98
98
rows <- runQuery_ sql client
99
- maybe (pure Nothing ) (either liftError (pure <<< Just )) (readFirst rows)
99
+ maybe (pure Nothing ) (either liftError (pure <<< Just )) (decodeFirst rows)
100
100
101
101
-- | Runs a query and returns a single value, if any.
102
102
queryValue :: forall eff a
103
- . (IsForeign a )
103
+ . (Decode a )
104
104
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
105
105
queryValue (Query sql) params client = do
106
106
val <- runQueryValue sql params client
107
- pure $ either (const Nothing ) Just (runExcept (read val))
107
+ pure $ either (const Nothing ) Just (runExcept (decode val))
108
108
109
109
-- | Just like `queryValue` but does not make any param replacement
110
- queryValue_ :: forall eff a . (IsForeign a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
110
+ queryValue_ :: forall eff a . (Decode a ) => Query a -> Client -> Aff (db :: DB | eff ) (Maybe a )
111
111
queryValue_ (Query sql) client = do
112
112
val <- runQueryValue_ sql client
113
- either liftError (pure <<< Just ) $ runExcept (read val)
113
+ either liftError (pure <<< Just ) $ runExcept (decode val)
114
114
115
115
-- | Connects to the database, calls the provided function with the client
116
116
-- | and returns the results.
@@ -130,8 +130,8 @@ withClient :: forall eff a
130
130
-> Aff (db :: DB | eff ) a
131
131
withClient info p = runFn2 _withClient (mkConnectionString info) p
132
132
133
- readFirst :: forall a . IsForeign a => Array Foreign -> Maybe (Either MultipleErrors a )
134
- readFirst rows = runExcept <<< read <$> (rows !! 0 )
133
+ decodeFirst :: forall a . Decode a => Array Foreign -> Maybe (Either MultipleErrors a )
134
+ decodeFirst rows = runExcept <<< decode <$> (rows !! 0 )
135
135
136
136
liftError :: forall e a . MultipleErrors -> Aff e a
137
137
liftError errs = throwError $ error (show errs)
0 commit comments