Skip to content

Commit b142ec5

Browse files
authored
Merge pull request #15 from justinwoo/update
update to PS 0.11 and other stuff
2 parents d61baaa + 41dfa2d commit b142ec5

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed

bower.json

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@
2626
"output"
2727
],
2828
"dependencies": {
29-
"purescript-arrays": "^3.0.0",
30-
"purescript-either": "^2.0.0",
31-
"purescript-foreign": "^3.0.0",
32-
"purescript-foldable-traversable": "^2.0.0",
33-
"purescript-transformers": "^2.0.0",
34-
"purescript-aff": "^2.0.0",
35-
"purescript-integers": "^2.0.0",
36-
"purescript-datetime": "^2.0.0",
37-
"purescript-unsafe-coerce": "^2.0.0",
38-
"purescript-nullable": "^2.0.0"
29+
"purescript-arrays": "^4.0.0",
30+
"purescript-either": "^3.0.0",
31+
"purescript-foreign": "^4.0.0",
32+
"purescript-foldable-traversable": "^3.0.0",
33+
"purescript-transformers": "^3.0.0",
34+
"purescript-aff": "^3.0.0",
35+
"purescript-integers": "^3.0.0",
36+
"purescript-datetime": "^3.0.0",
37+
"purescript-unsafe-coerce": "^3.0.0",
38+
"purescript-nullable": "^3.0.0",
39+
"purescript-prelude": "^3.0.0",
40+
"purescript-foreign-generic": "^4.0.0"
3941
},
4042
"devDependencies": {
41-
"purescript-spec": "^0.10.0",
42-
"purescript-generics": "^3.0.0",
43-
"purescript-js-date": "^3.0.0"
43+
"purescript-spec": "^1.0.0",
44+
"purescript-generics": "^4.0.0",
45+
"purescript-js-date": "^4.0.0"
4446
}
4547
}

src/Database/Postgres.purs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ module Database.Postgres
1717
) where
1818

1919
import Prelude
20-
import Control.Monad.Eff (Eff)
20+
import Control.Monad.Eff (kind Effect, Eff)
2121
import Data.Either (Either, either)
2222
import Data.Function.Uncurried (Fn2(), runFn2)
2323
import Data.Array ((!!))
2424
import Data.Foreign (Foreign, MultipleErrors)
25-
import Data.Foreign.Class (class IsForeign, read)
25+
import Data.Foreign.Class (class Decode, decode)
2626
import Data.Maybe (Maybe(Just, Nothing), maybe)
2727
import Control.Monad.Except (runExcept)
2828
import Control.Monad.Aff (Aff, finally)
@@ -35,9 +35,9 @@ import Database.Postgres.SqlValue (SqlValue)
3535

3636
newtype 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

4242
type ConnectionString = String
4343

@@ -72,45 +72,45 @@ execute_ (Query sql) client = void $ runQuery_ sql client
7272

7373
-- | Runs a query and returns all results.
7474
query :: forall eff a
75-
. (IsForeign a)
75+
. (Decode a)
7676
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Array a)
7777
query (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)
8383
query_ (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
8888
queryOne :: forall eff a
89-
. (IsForeign a)
89+
. (Decode a)
9090
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
9191
queryOne (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)
9797
queryOne_ (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.
102102
queryValue :: forall eff a
103-
. (IsForeign a)
103+
. (Decode a)
104104
=> Query a -> Array SqlValue -> Client -> Aff (db :: DB | eff) (Maybe a)
105105
queryValue (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)
111111
queryValue_ (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
131131
withClient 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

136136
liftError :: forall e a. MultipleErrors -> Aff e a
137137
liftError errs = throwError $ error (show errs)

src/Database/Postgres/SqlValue.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Data.Time (hour, minute, second)
1414
import Unsafe.Coerce (unsafeCoerce)
1515
import Data.Nullable (toNullable)
1616

17-
foreign import data SqlValue :: *
17+
foreign import data SqlValue :: Type
1818

1919
class IsSqlValue a where
2020
toSql :: a -> SqlValue

test/Main.purs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
module Test.Main where
22

33
import Prelude
4+
45
import Control.Monad.Aff (Aff, apathize, attempt)
6+
import Control.Monad.Aff.AVar (AVAR)
57
import Control.Monad.Eff (Eff)
68
import Control.Monad.Eff.Class (liftEff)
79
import Control.Monad.Eff.Console (CONSOLE)
810
import Control.Monad.Eff.Exception (error)
11+
import Control.Monad.Eff.Timer (TIMER)
912
import Control.Monad.Error.Class (throwError)
1013
import Data.Array (length)
1114
import Data.Date (canonicalDate)
12-
import Data.DateTime (DateTime(..))
1315
import Data.Date.Component (Month(..))
14-
import Data.Enum (toEnum)
15-
import Data.Time (Time(..))
16+
import Data.DateTime (DateTime(..))
1617
import Data.Either (either)
18+
import Data.Enum (toEnum)
1719
import Data.Foreign (Foreign)
20+
import Data.Foreign.Class (class Decode, decode)
21+
import Data.Foreign.Index (readProp)
22+
import Data.Generic (class Generic, gEq)
1823
import Data.JSDate (toDateTime)
1924
import Data.Maybe (Maybe(Nothing, Just), maybe)
20-
import Data.Foreign.Class (class IsForeign, readProp)
21-
import Data.Generic (class Generic, gEq)
22-
import Database.Postgres (DB, Query(Query), queryOne_, execute, execute_, withConnection, query, withClient, end, query_, connect, queryValue_, mkConnectionString)
25+
import Data.Time (Time(..))
26+
import Database.Postgres (DB, Query(Query), connect, end, execute, execute_, mkConnectionString, query, queryOne_, queryValue_, query_, withClient, withConnection)
2327
import Database.Postgres.SqlValue (toSql)
2428
import Database.Postgres.Transaction (withTransaction)
2529
import Node.Process (PROCESS)
26-
27-
import Unsafe.Coerce (unsafeCoerce)
28-
2930
import Test.Spec (describe, it)
30-
import Test.Spec.Runner (run)
3131
import Test.Spec.Assertions (fail, shouldEqual)
3232
import Test.Spec.Reporter.Console (consoleReporter)
33+
import Test.Spec.Runner (run)
34+
import Unsafe.Coerce (unsafeCoerce)
3335

3436
data Artist = Artist
3537
{ name :: String
@@ -45,7 +47,16 @@ connectionInfo =
4547
, password: "test"
4648
}
4749

48-
main :: Eff (process :: PROCESS, console :: CONSOLE , db :: DB) Unit
50+
main :: forall eff.
51+
Eff
52+
( console :: CONSOLE
53+
, timer :: TIMER
54+
, avar :: AVAR
55+
, process :: PROCESS
56+
, db :: DB
57+
| eff
58+
)
59+
Unit
4960
main = run [consoleReporter] do
5061
describe "connection string" do
5162
it "should build one from the connection record" do
@@ -111,7 +122,7 @@ main = run [consoleReporter] do
111122
) dt
112123

113124

114-
describe "transactions" do
125+
describe "transactions" do
115126
it "does not commit after an error inside a transation" do
116127
withConnection connectionInfo $ \c -> do
117128
execute_ (Query "delete from artist") c
@@ -138,8 +149,8 @@ derive instance genericArtist :: Generic Artist
138149
instance eqArtist :: Eq Artist where
139150
eq = gEq
140151

141-
instance artistIsForeign :: IsForeign Artist where
142-
read obj = do
143-
n <- readProp "name" obj
144-
y <- readProp "year" obj
152+
instance artistIsForeign :: Decode Artist where
153+
decode obj = do
154+
n <- decode =<< readProp "name" obj
155+
y <- decode =<< readProp "year" obj
145156
pure $ Artist { name: n, year: y }

0 commit comments

Comments
 (0)