From 7fcebcbb0a587f3aa4f3b1e45f718567c386e605 Mon Sep 17 00:00:00 2001 From: Sibi Prabakaran Date: Mon, 7 Nov 2016 21:13:56 +0530 Subject: [PATCH] Add type signatures and extension in code Fixes #71 --- Database/MongoDB.hs | 90 +++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/Database/MongoDB.hs b/Database/MongoDB.hs index e784d87..badb862 100644 --- a/Database/MongoDB.hs +++ b/Database/MongoDB.hs @@ -1,42 +1,54 @@ -{- | -Client interface to MongoDB database management system. - -Simple example below. Use with language extensions /OverloadedStrings/ & /ExtendedDefaultRules/. - - -> import Database.MongoDB -> import Control.Monad.Trans (liftIO) -> -> main = do -> pipe <- connect (host "127.0.0.1") -> e <- access pipe master "baseball" run -> close pipe -> print e -> -> run = do -> clearTeams -> insertTeams -> allTeams >>= printDocs "All Teams" -> nationalLeagueTeams >>= printDocs "National League Teams" -> newYorkTeams >>= printDocs "New York Teams" -> -> clearTeams = delete (select [] "team") -> -> insertTeams = insertMany "team" [ -> ["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"], -> ["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"], -> ["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"], -> ["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ] -> -> allTeams = rest =<< find (select [] "team") {sort = ["home.city" =: 1]} -> -> nationalLeagueTeams = rest =<< find (select ["league" =: "National"] "team") -> -> newYorkTeams = rest =<< find (select ["home.state" =: "NY"] "team") {project = ["name" =: 1, "league" =: 1]} -> -> printDocs title docs = liftIO $ putStrLn title >> mapM_ (print . exclude ["_id"]) docs -> --} +-- | +-- Client interface to MongoDB database management system. +-- +-- Simple example below. +-- +-- @ +-- {\-\# LANGUAGE OverloadedStrings \#\-} +-- {\-\# LANGUAGE ExtendedDefaultRules \#\-} +-- +-- import Database.MongoDB +-- import Control.Monad.Trans (liftIO) +-- +-- main :: IO () +-- main = do +-- pipe <- connect (host \"127.0.0.1\") +-- e <- access pipe master \"baseball\" run +-- close pipe +-- print e +-- +-- run :: Action IO () +-- run = do +-- clearTeams +-- insertTeams +-- allTeams >>= printDocs \"All Teams\" +-- nationalLeagueTeams >>= printDocs \"National League Teams\" +-- newYorkTeams >>= printDocs \"New York Teams\" +-- +-- clearTeams :: Action IO () +-- clearTeams = delete (select [] \"team\") +-- +-- insertTeams :: Action IO [Value] +-- insertTeams = insertMany \"team\" [ +-- [\"name\" =: \"Yankees\", \"home\" =: [\"city\" =: \"New York\", \"state\" =: \"NY\"], \"league\" =: \"American\"], +-- [\"name\" =: \"Mets\", \"home\" =: [\"city\" =: \"New York\", \"state\" =: \"NY\"], \"league\" =: \"National\"], +-- [\"name\" =: \"Phillies\", \"home\" =: [\"city\" =: \"Philadelphia\", \"state\" =: \"PA\"], \"league\" =: \"National\"], +-- [\"name\" =: \"Red Sox\", \"home\" =: [\"city\" =: \"Boston\", \"state\" =: \"MA\"], \"league\" =: \"American\"] ] +-- +-- allTeams :: Action IO [Document] +-- allTeams = rest =<< find (select [] \"team\") {sort = [\"home.city\" =: 1]} +-- +-- nationalLeagueTeams :: Action IO [Document] +-- nationalLeagueTeams = rest =<< find (select [\"league\" =: \"National\"] \"team\") +-- +-- newYorkTeams :: Action IO [Document] +-- newYorkTeams = rest =<< find (select [\"home.state\" =: \"NY\"] \"team\") {project = [\"name\" =: 1, \"league\" =: 1]} +-- +-- printDocs :: String -> [Document] -> Action IO () +-- printDocs title docs = liftIO $ putStrLn title >> mapM_ (print . exclude [\"_id\"]) docs +-- +-- @ +-- module Database.MongoDB ( module Data.Bson,