Skip to content

support all available index options #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 51 additions & 12 deletions Database/MongoDB/Admin.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- | Database administrative functions

{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards #-}
{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards, PatternGuards #-}

module Database.MongoDB.Admin (
-- * 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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mongoDB.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down