@@ -17,12 +17,12 @@ module Database.Postgres
1717 ) where
1818
1919import Prelude
20- import Control.Monad.Eff (Eff )
20+ import Control.Monad.Eff (kind Effect , Eff )
2121import Data.Either (Either , either )
2222import Data.Function.Uncurried (Fn2 (), runFn2 )
2323import Data.Array ((!!))
2424import Data.Foreign (Foreign , MultipleErrors )
25- import Data.Foreign.Class (class IsForeign , read )
25+ import Data.Foreign.Class (class Decode , decode )
2626import Data.Maybe (Maybe (Just, Nothing), maybe )
2727import Control.Monad.Except (runExcept )
2828import Control.Monad.Aff (Aff , finally )
@@ -35,9 +35,9 @@ import Database.Postgres.SqlValue (SqlValue)
3535
3636newtype Query a = Query String
3737
38- foreign import data Client :: *
38+ foreign import data Client :: Type
3939
40- foreign import data DB :: !
40+ foreign import data DB :: Effect
4141
4242type ConnectionString = String
4343
@@ -72,45 +72,45 @@ execute_ (Query sql) client = void $ runQuery_ sql client
7272
7373-- | Runs a query and returns all results.
7474query :: forall eff a
75- . (IsForeign a )
75+ . (Decode a )
7676 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Array a )
7777query (Query sql) params client = do
7878 rows <- runQuery sql params client
79- either liftError pure (runExcept (sequence $ read <$> rows))
79+ either liftError pure (runExcept (sequence $ decode <$> rows))
8080
8181-- | 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 )
8383query_ (Query sql) client = do
8484 rows <- runQuery_ sql client
85- either liftError pure (runExcept (sequence $ read <$> rows))
85+ either liftError pure (runExcept (sequence $ decode <$> rows))
8686
8787-- | Runs a query and returns the first row, if any
8888queryOne :: forall eff a
89- . (IsForeign a )
89+ . (Decode a )
9090 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
9191queryOne (Query sql) params client = do
9292 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)
9494
9595-- | 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 )
9797queryOne_ (Query sql) client = do
9898 rows <- runQuery_ sql client
99- maybe (pure Nothing ) (either liftError (pure <<< Just )) (readFirst rows)
99+ maybe (pure Nothing ) (either liftError (pure <<< Just )) (decodeFirst rows)
100100
101101-- | Runs a query and returns a single value, if any.
102102queryValue :: forall eff a
103- . (IsForeign a )
103+ . (Decode a )
104104 => Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff ) (Maybe a )
105105queryValue (Query sql) params client = do
106106 val <- runQueryValue sql params client
107- pure $ either (const Nothing ) Just (runExcept (read val))
107+ pure $ either (const Nothing ) Just (runExcept (decode val))
108108
109109-- | 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 )
111111queryValue_ (Query sql) client = do
112112 val <- runQueryValue_ sql client
113- either liftError (pure <<< Just ) $ runExcept (read val)
113+ either liftError (pure <<< Just ) $ runExcept (decode val)
114114
115115-- | Connects to the database, calls the provided function with the client
116116-- | and returns the results.
@@ -130,8 +130,8 @@ withClient :: forall eff a
130130 -> Aff (db :: DB | eff ) a
131131withClient info p = runFn2 _withClient (mkConnectionString info) p
132132
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 )
135135
136136liftError :: forall e a . MultipleErrors -> Aff e a
137137liftError errs = throwError $ error (show errs)
0 commit comments