Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 3fdbf14

Browse files
Merge pull request #8 from dicefm/example
Add a basic find example
2 parents 237bad0 + 5997abb commit 3fdbf14

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

examples/Examples/Data/Event.purs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Data.Event where
2+
3+
import Data.Argonaut ((~>), (:=), (.?), jsonEmptyObject, printJson)
4+
import Data.Argonaut.Core (Json(), JString(), toString)
5+
import Data.Argonaut.Encode (EncodeJson, encodeJson)
6+
import Data.Argonaut.Decode (DecodeJson, decodeJson)
7+
import Data.Date
8+
import Data.Either
9+
import Data.Maybe (Maybe(..), maybe)
10+
import Data.Traversable (traverse)
11+
12+
import Debug.Trace
13+
14+
newtype Event = Event
15+
{ name :: Maybe String
16+
, date :: Maybe Date
17+
}
18+
19+
instance decodeJsonEvent :: DecodeJson Event where
20+
decodeJson json = do
21+
obj <- decodeJson json
22+
name <- obj .? "name"
23+
pure $ Event
24+
{ name : name
25+
, date : Nothing
26+
}
27+
28+
instance encodeJsonEvent :: EncodeJson Event where
29+
encodeJson (Event e)
30+
= "name" := e.name
31+
~> jsonEmptyObject
32+
33+
instance showEvent :: Show Event where
34+
show (Event e) = "Event " ++
35+
"{ name: " ++ show e.name ++
36+
"}"
37+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Examples.Database.Mongo.Example where
2+
3+
import Database.Mongo.Mongo
4+
import Database.Mongo.ConnectionInfo
5+
import Database.Mongo.Bson.BsonValue
6+
7+
import Control.Monad.Aff
8+
import Control.Monad.Eff
9+
import Control.Monad.Eff.Class
10+
import Control.Monad.Eff.Exception
11+
12+
import Data.Argonaut (printJson)
13+
import Data.Argonaut.Core (Json(..))
14+
import Data.Argonaut.Decode (DecodeJson, decodeJson)
15+
import Data.Either
16+
import Data.Event
17+
import Data.Maybe
18+
import Data.String.Regex
19+
20+
import Debug.Trace
21+
22+
foreign import traceAny
23+
"""
24+
function traceAny(a){
25+
return function () {
26+
console.log(a);
27+
return {};
28+
};
29+
}
30+
""" :: forall e a. a -> Eff (trace :: Trace | e) Unit
31+
32+
main = launchAff $ do
33+
34+
Right database <- attempt $ connect $ defaultOptions { db = Just "test" }
35+
col <- collection "events" database
36+
cur <- find [
37+
"$or" := [ "name" := (regex "Amazing" noFlags)
38+
, "name" := (regex "Wow!" noFlags)
39+
]
40+
] [] col
41+
res <- collect cur
42+
43+
liftEff $ case decodeEvents res of
44+
Left err -> traceAny err
45+
Right x -> traceAny x
46+
47+
close database
48+
49+
50+
where
51+
decodeEvents :: Json -> (Either String [Event])
52+
decodeEvents = decodeJson

src/Database/Mongo/Mongo.purs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ type AffDatabase e = Aff (db :: DB | e) Database
2626
type AffCollection e = Aff (db :: DB | e) Collection
2727
type AffCursor e = Aff (db :: DB | e) Cursor
2828
type AffResult e = Aff (db :: DB | e) Json
29+
type AffUnit e = Aff (db :: DB | e) Unit
2930

3031
-- | Makes a connection to the database.
3132
connect :: forall e. ConnectionInfo -> AffDatabase e
3233
connect = makeAff' <<< connect'
3334

35+
-- | Close the connection to the database
36+
close :: forall e. Database -> AffUnit e
37+
close = makeAff' <<< close'
38+
39+
3440
-- | Get the collection
3541
collection :: forall e. String -> Database -> AffCollection e
3642
collection a b = makeAff' (collection' a b)
@@ -59,6 +65,13 @@ connect' :: forall e
5965
-> (Eff (db :: DB | e) (Canceler (db :: DB | e)))
6066
connect' info eb cb = runFn4 _connect (dialUri info) ignoreCancel eb cb
6167

68+
close' :: forall e
69+
. Database
70+
-> (Error -> Eff (db :: DB | e) Unit)
71+
-> (Unit -> Eff (db :: DB | e) Unit)
72+
-> (Eff (db :: DB | e) (Canceler (db :: DB | e)))
73+
close' database eb cb = runFn4 _close database ignoreCancel eb cb
74+
6275
collection' :: forall e
6376
. String
6477
-> Database
@@ -120,6 +133,21 @@ foreign import _connect
120133
(Database -> Eff (db :: DB | e) Unit)
121134
(Eff (db :: DB | e) (Canceler (db :: DB | e)))
122135

136+
foreign import _close
137+
"""
138+
function _close(db, canceler, errback, callback) {
139+
db.close(function(err, x) {
140+
(err ? errback(err) : callback(x))();
141+
});
142+
return canceler({});
143+
}
144+
""" :: forall e. Fn4
145+
Database
146+
(Unit -> Canceler (db :: DB | e))
147+
(Error -> Eff (db :: DB | e) Unit)
148+
(Unit -> Eff (db :: DB | e) Unit)
149+
(Eff (db :: DB | e) (Canceler (db :: DB | e)))
150+
123151
foreign import _collection
124152
"""
125153
function _collection(name, db, canceler, errback, callback) {
@@ -212,7 +240,7 @@ foreign import _ignoreCancel
212240
"""
213241
function _ignoreCancel(any, cancelError, errback, callback) {
214242
return function() {
215-
callback(false);
243+
return callback(false)();
216244
};
217245
}
218246
""" :: forall e a. Fn4

0 commit comments

Comments
 (0)