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

upgrade for purescript 0.10 #25

Merged
merged 2 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ main = unsafePartial $ void $ launchAff $ do

See the module documentation for a [full list of these helpers](docs/Database.Mongo.Mongo.md#find)

See the test cases for [more examples](test/Database/Mongo/)

### Module documentation

- [Database.Mongo.Mongo](docs/Database.Mongo.Mongo.md)
Expand Down
21 changes: 13 additions & 8 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"tests"
],
"dependencies": {
"purescript-aff": "^1.0.0",
"purescript-argonaut": "^1.0.0",
"purescript-datetime": "^1.0.0",
"purescript-foldable-traversable": "^1.0.0",
"purescript-foreign": "^1.0.0",
"purescript-string-parsers": "^1.0.1",
"purescript-uri": "^1.0.0"
"purescript-aff": "^2.0.2",
"purescript-argonaut": "^2.0.0",
"purescript-datetime": "^2.0.0",
"purescript-foldable-traversable": "^2.0.0",
"purescript-foreign": "^3.0.1",
"purescript-string-parsers": "^2.0.0",
"purescript-uri": "^2.0.0",
"purescript-transformers": "^2.1.0"
},
"homepage": "https://github.com/dicefm/purescript-node-mongodb",
"authors": [
Expand All @@ -34,5 +35,9 @@
"mongodb",
"mongo"
],
"license": "MIT"
"license": "MIT",
"devDependencies": {
"purescript-debug": "^2.0.0",
"purescript-assert": "^2.0.0"
}
}
35 changes: 0 additions & 35 deletions examples/Examples/Data/Event.purs

This file was deleted.

44 changes: 0 additions & 44 deletions examples/Examples/Database/Mongo/Find.purs

This file was deleted.

49 changes: 0 additions & 49 deletions examples/Examples/Database/Mongo/Insert.purs

This file was deleted.

49 changes: 0 additions & 49 deletions examples/Examples/Database/Mongo/Update.purs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Database/Mongo/Bson/BsonValue.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module Database.Mongo.Bson.BsonValue
) where

import Data.Argonaut.Core (Json())
import Data.String.Regex
import Data.Tuple
import Data.String.Regex (Regex)
import Data.Tuple (Tuple(..))

type Field = Tuple String BsonValue

Expand Down
8 changes: 4 additions & 4 deletions src/Database/Mongo/Mongo.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ module Database.Mongo.Mongo
, updateMany, updateMany'
) where

import Prelude
import Prelude (class Show, Unit, show, ($), (<<<))
import Control.Monad.Aff (Aff(), makeAff', Canceler(), nonCanceler)
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (Error(), error)

import Data.Argonaut.Core (Json())
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
import Data.Either
import Data.Either (Either(..))
import Data.Function.Uncurried (Fn3(), runFn3, Fn4(), runFn4, Fn5(), runFn5, Fn6(), runFn6, Fn7(), runFn7, Fn8(), runFn8)

import Database.Mongo.Options (InsertOptions(), insertOptions, UpdateOptions(), updateOptions)
import Database.Mongo.Results (WriteResult())
import Database.Mongo.Bson.BsonValue (Document(), printBson)
import Data.URI
import Data.URI (printURIRef, runParseURIRef)

import Text.Parsing.StringParser
import Text.Parsing.StringParser (ParseError)

-- | The effect type for DB request made with Mongo
foreign import data DB :: !
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Mongo/Options.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ module Database.Mongo.Options
, defaultUpdateOptions, updateOptions
) where

import Prelude
import Prelude (pure, bind, ($))
import Data.Argonaut ((~>), (:=), (.?), jsonEmptyObject)
import Data.Argonaut.Core (Json())
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
import Data.Either
import Data.Maybe
import Data.Maybe (Maybe(..))

-- | The type of WriteConcern
type WriteConcern = Number
Expand Down
25 changes: 17 additions & 8 deletions src/Database/Mongo/Results.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module Database.Mongo.Results
( WriteResult()
) where

import Prelude
import Data.Argonaut ((.?), jsonEmptyObject)
import Prelude (pure, bind, ($))
import Data.Argonaut ((.?), (:=), (~>), jsonEmptyObject)
import Data.Argonaut.Encode (class EncodeJson)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
import Data.Either
import Data.Maybe
import Data.Either (Either(..))
import Data.Maybe (Maybe(..), fromMaybe)

newtype WriteResult = WriteResult
{ success :: Boolean
Expand All @@ -33,14 +33,23 @@ instance decodeJsonWriteResult :: DecodeJson WriteResult where
}

instance encodeJsonWriteResult :: EncodeJson WriteResult where
encodeJson (WriteResult w) = jsonEmptyObject
encodeJson (WriteResult w)
= "ok" := boolToJsNumber w.success
~> "n" := w.total
~> "nInserted" := fromMaybe 0.0 w.inserted
~> "nModified" := fromMaybe 0.0 w.modified
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Awesome, I noticed this was broken as well.

Copy link
Contributor Author

@zhangchiqing zhangchiqing Jan 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

BTW, I have a question: I used 0.0 here instead of 0 because WriteResult takes Maybe Number, and 0 is a Int not a Number. But I don't understand why I can't create a Number by (0 :: Number), it doesn't typecheck. That's why I used 0.0. The same trick I used here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's purescript works.

~> jsonEmptyObject

boolToJsNumber :: Boolean -> Int
boolToJsNumber false = 0
boolToJsNumber true = 1

-- node mongodb module sends back `1` to mean `true`, this is why we need types
-- as Javascript is abused!
jsNumberToBool :: Either String Int -> Boolean
jsNumberToBool :: Int -> Boolean
jsNumberToBool e = case e of
Left _ -> false
Right x -> if x == 1 then true else false
1 -> true
_ -> false

extract :: Either String Number -> Maybe Number
extract e = case e of
Expand Down
32 changes: 32 additions & 0 deletions test/Data/Event.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Test.Data.Event where

import Prelude
import Data.Argonaut (jsonEmptyObject, (~>), (:=), (.?))
import Data.Argonaut.Encode.Class (class EncodeJson)
import Data.Argonaut.Decode (decodeJson)
import Data.Argonaut.Decode.Class (class DecodeJson)
import Data.Maybe (Maybe)

newtype Event = Event
{ name :: Maybe String
}

instance decodeJsonEvent :: DecodeJson Event where
decodeJson json = do
obj <- decodeJson json
name <- obj .? "name"
pure $ Event
{ name : name
}

instance encodeJsonEvent :: EncodeJson Event where
encodeJson (Event e)
= "name" := e.name
~> jsonEmptyObject

instance showEvent :: Show Event where
show (Event e) = "Event " <>
"{ name: " <> show e.name <>
"}"


25 changes: 25 additions & 0 deletions test/Database/Mongo/Find.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Test.Database.Mongo.Find where

import Prelude (Unit, bind, (>), ($))
import Data.Array (length)
import Data.Maybe (Maybe(..))
import Database.Mongo.Bson.BsonValue ((:=))
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (log)
import Test.Data.Event (Event(..))
import Test.Assert (assert)
import Database.Mongo.Mongo (Database, collect, find, collection)
import Test.Type (Test)

evt :: Event
evt = Event
{ name : Just "Wow"
}

main :: Database -> Test Unit
main database = do
liftEff $ log "should find"
col <- collection "events" database
cur <- find [ "name" := "Wow" ] [ "name" := 1.0 ] col
res <- collect cur
liftEff $ assert $ length (res :: Array Event) > 0
Loading