From e74a5a55a66cbcd62c0ab3b32356610242f28f7b Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Thu, 8 May 2014 15:51:22 -0700 Subject: [PATCH 1/3] support all available index options except weights (I am lazy) and version (it is unecessary) This is a breaking change --- Database/MongoDB/Admin.hs | 59 ++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/Database/MongoDB/Admin.hs b/Database/MongoDB/Admin.hs index 264e4d1..c0ed749 100644 --- a/Database/MongoDB/Admin.hs +++ b/Database/MongoDB/Admin.hs @@ -1,6 +1,6 @@ -- | Database administrative functions -{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards #-} +{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards, PatternGuards #-} module Database.MongoDB.Admin ( -- * Admin @@ -42,6 +42,7 @@ import Control.Monad.Trans (MonadIO, liftIO) import Control.Monad.Trans.Control (MonadBaseControl) import Data.Bson (Document, Field(..), at, (=:), (=?), exclude, merge) import Data.Text (Text) +import Data.Maybe (mapMaybe, catMaybes) import qualified Data.Text as T @@ -91,25 +92,63 @@ validateCollection coll = runCommand ["validate" =: coll] type IndexName = Text +data IndexOpts = IndexOpts + { unique :: Bool + , dropDups :: Bool + , background :: Bool + , sparse :: Bool + , expireAfterSeconds :: Maybe Int + , defaultLanguage :: Maybe Text + , languageOverride :: Maybe Text + -- too lazy to add this one + -- , weights :: [Weight] + -- nobody needs this + -- , iVersion :: Int = 1 + } deriving (Show, Eq) + +-- | Unlike the shell, this defaults background to True +defaultIndexOpts :: IndexOpts +defaultIndexOpts = IndexOpts + { unique = False + , dropDups = False + , background = True + , sparse = False + , expireAfterSeconds = Nothing + , defaultLanguage = Nothing + , languageOverride = Nothing + } + data Index = Index { iColl :: Collection, iKey :: Order, iName :: IndexName, - iUnique :: Bool, - iDropDups :: Bool + iOpts :: IndexOpts } deriving (Show, Eq) idxDocument :: Index -> Database -> Document -idxDocument Index{..} db = [ - "ns" =: db <.> iColl, - "key" =: iKey, - "name" =: iName, - "unique" =: iUnique, - "dropDups" =: iDropDups ] +idxDocument Index{..} db = + [ "ns" =: db <.> iColl + , "key" =: iKey + , "name" =: iName + ] ++ mapMaybe addBool + [ ("unique", unique) + , ("dropDups", dropDups) + , ("background", background) + , ("sparse", sparse) + ] ++ catMaybes + [ addMay ("expireAfterSeconds", expireAfterSeconds) + , addMay ("defaultLanguage", defaultLanguage) + , addMay ("languageOverride", languageOverride) + ] + where + addBool (label, accessor) | accessor iOpts = Just (label =: True) + | otherwise = Nothing + addMay (label, accessor) | Just result <- accessor iOpts = Just (label =: result) + | otherwise = Nothing index :: Collection -> Order -> Index -- ^ Spec of index of ordered keys on collection. Name is generated from keys. Unique and dropDups are False. -index coll keys = Index coll keys (genName keys) False False +index coll keys = Index coll keys (genName keys) defaultIndexOpts genName :: Order -> IndexName genName keys = T.intercalate "_" (map f keys) where From 21ea349d5ae977ac58c4ba2a2359f53128643bd6 Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Tue, 13 May 2014 16:06:58 -0700 Subject: [PATCH 2/3] version bump for index changes --- mongoDB.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongoDB.cabal b/mongoDB.cabal index 62ca88b..ad085e6 100644 --- a/mongoDB.cabal +++ b/mongoDB.cabal @@ -1,5 +1,5 @@ Name: mongoDB -Version: 1.4.4 +Version: 1.5 Synopsis: Driver (client) for MongoDB, a free, scalable, fast, document DBMS Description: This package lets you connect to MongoDB servers and From ad6534650b709eadb91c9e7aa1da3947e10fd1c3 Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Tue, 13 May 2014 17:36:37 -0700 Subject: [PATCH 3/3] add Index exports Conflicts: Database/MongoDB/Admin.hs --- Database/MongoDB/Admin.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Database/MongoDB/Admin.hs b/Database/MongoDB/Admin.hs index c0ed749..962f07c 100644 --- a/Database/MongoDB/Admin.hs +++ b/Database/MongoDB/Admin.hs @@ -7,8 +7,8 @@ module Database.MongoDB.Admin ( -- ** Collection CollectionOption(..), createCollection, renameCollection, dropCollection, validateCollection, - -- ** Index - Index(..), IndexName, index, ensureIndex, createIndex, dropIndex, + -- ** Index + Index(..), IndexOpts(..), IndexName, index, ensureIndex, createIndex, dropIndex, defaultIndexOpts, getIndexes, dropIndexes, -- ** User allUsers, addUser, removeUser,