persistent-mongoDB 1.3.2 → 1.3.3
raw patch · 2 files changed
+38/−25 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Database.Persist.MongoDB: filtersToDoc :: (PersistEntity val, PersistEntityBackend val ~ MongoBackend) => [Filter val] -> Document
+ Database.Persist.MongoDB: toInsertDoc :: PersistEntity record => record -> Document
+ Database.Persist.MongoDB: toUniquesDoc :: PersistEntity record => Unique record -> [Field]
+ Database.Persist.MongoDB: updatesToDoc :: PersistEntity entity => [Update entity] -> Document
- Database.Persist.MongoDB: entityToDocument :: PersistEntity record => record -> [Field]
+ Database.Persist.MongoDB: entityToDocument :: PersistEntity record => record -> Document
Files
- Database/Persist/MongoDB.hs +37/−24
- persistent-mongoDB.cabal +1/−1
Database/Persist/MongoDB.hs view
@@ -26,12 +26,17 @@ ( -- * Entity conversion collectionName- , entityToDocument- , entityToFields- , toInsertFields , docToEntityEither , docToEntityThrow+ , entityToDocument+ , toInsertDoc+ , updatesToDoc+ , filtersToDoc+ , toUniquesDoc + , toInsertFields+ , entityToFields+ -- * MongoDB specific Filters -- $filters , nestEq, multiEq, nestBsonEq, multiBsonEq@@ -313,8 +318,12 @@ => Key record -> EntityDef a -> DB.Selection selectByKey k record = (DB.select (filterByKey k) (unDBName $ entityDB record)) +updatesToDoc :: (PersistEntity entity) => [Update entity] -> DB.Document+updatesToDoc upds = map updateToMongoField upds+ updateFields :: (PersistEntity entity) => [Update entity] -> [DB.Field]-updateFields upds = map updateToMongoField upds +updateFields = updatesToDoc+{-# DEPRECATED updateFields "Please use updatesToDoc instead" #-} updateToMongoField :: (PersistEntity entity) => Update entity -> DB.Field updateToMongoField (Update field v up) =@@ -336,16 +345,20 @@ (Divide, _) -> throw $ PersistMongoDBUnsupported "divide not supported" -uniqSelector :: forall record. (PersistEntity record) => Unique record -> [DB.Field]-uniqSelector uniq = zipWith (DB.:=)+toUniquesDoc :: forall record. (PersistEntity record) => Unique record -> [DB.Field]+toUniquesDoc uniq = zipWith (DB.:=) (map (unDBName . snd) $ persistUniqueToFieldNames uniq) (map DB.val (persistUniqueToValues uniq)) +toInsertFields :: forall record. (PersistEntity record) => record -> [DB.Field]+toInsertFields = toInsertDoc+{-# DEPRECATED toInsertFields "Please use toInsertDoc instead" #-}+ -- | convert a PersistEntity into document fields. -- for inserts only: nulls are ignored so they will be unset in the document.--- 'entityToFields' includes nulls-toInsertFields :: forall record. (PersistEntity record) => record -> [DB.Field]-toInsertFields record = zipFilter (entityFields entity) (toPersistFields record)+-- 'entityToDocument' includes nulls+toInsertDoc :: forall record. (PersistEntity record) => record -> DB.Document+toInsertDoc record = zipFilter (entityFields entity) (toPersistFields record) where zipFilter [] _ = [] zipFilter _ [] = []@@ -358,8 +371,8 @@ collectionName = unDBName . entityDB . entityDef . Just -- | convert a PersistEntity into document fields.--- unlike 'toInsertFields', nulls are included.-entityToDocument :: (PersistEntity record) => record -> [DB.Field]+-- unlike 'toInsertDoc', nulls are included.+entityToDocument :: (PersistEntity record) => record -> DB.Document entityToDocument record = zipIt (entityFields entity) (toPersistFields record) where zipIt [] _ = []@@ -400,12 +413,12 @@ insertMany (r:records) = map (\(DB.ObjId oid) -> oidToKey oid) `fmap` DB.insertMany (collectionName r) (map toInsertFields (r:records)) - insertKey k record = saveWithKey toInsertFields DB.insert_ k record+ insertKey k record = saveWithKey toInsertDoc DB.insert_ k record repsert k record = saveWithKey entityToDocument DB.save k record replace k record = do- DB.replace (selectByKey k t) (toInsertFields record)+ DB.replace (selectByKey k t) (entityToDocument record) return () where t = entityDef $ Just record@@ -436,7 +449,7 @@ instance (Applicative m, Functor m, Trans.MonadIO m, MonadBaseControl IO m) => PersistUnique (DB.Action m) where getBy uniq = do mdoc <- DB.findOne $- DB.select (uniqSelector uniq) (unDBName $ entityDB t)+ DB.select (toUniquesDoc uniq) (unDBName $ entityDB t) case mdoc of Nothing -> return Nothing Just doc -> fmap Just $ fromPersistValuesThrow t doc@@ -446,7 +459,7 @@ deleteBy uniq = DB.delete DB.Select { DB.coll = collectionName $ dummyFromUnique uniq- , DB.selector = uniqSelector uniq+ , DB.selector = toUniquesDoc uniq } _id :: T.Text@@ -462,12 +475,12 @@ update key upds = DB.modify (DB.Select [keyToMongoIdField key] (collectionName $ recordTypeFromKey key))- $ updateFields upds+ $ updatesToDoc upds updateGet key upds = do result <- DB.findAndModify (DB.select [keyToMongoIdField key] (unDBName $ entityDB t)- ) (updateFields upds)+ ) (updatesToDoc upds) case result of Left e -> err e Right doc -> do@@ -482,20 +495,20 @@ updateWhere filts upds = DB.modify DB.Select { DB.coll = collectionName $ dummyFromFilts filts- , DB.selector = filtersToSelector filts- } $ updateFields upds+ , DB.selector = filtersToDoc filts+ } $ updatesToDoc upds deleteWhere filts = do DB.delete DB.Select { DB.coll = collectionName $ dummyFromFilts filts- , DB.selector = filtersToSelector filts+ , DB.selector = filtersToDoc filts } count filts = do i <- DB.count query return $ fromIntegral i where- query = DB.select (filtersToSelector filts) $+ query = DB.select (filtersToDoc filts) $ collectionName $ dummyFromFilts filts selectSource filts opts = do@@ -544,7 +557,7 @@ makeQuery :: (PersistEntity val, PersistEntityBackend val ~ MongoBackend) => [Filter val] -> [SelectOpt val] -> DB.Query makeQuery filts opts =- (DB.select (filtersToSelector filts) (collectionName $ dummyFromFilts filts)) {+ (DB.select (filtersToDoc filts) (collectionName $ dummyFromFilts filts)) { DB.limit = fromIntegral limit , DB.skip = fromIntegral offset , DB.sort = orders@@ -553,8 +566,8 @@ (limit, offset, orders') = limitOffsetOrder opts orders = map orderClause orders' -filtersToSelector :: (PersistEntity val, PersistEntityBackend val ~ MongoBackend) => [Filter val] -> DB.Document-filtersToSelector filts = +filtersToDoc :: (PersistEntity val, PersistEntityBackend val ~ MongoBackend) => [Filter val] -> DB.Document+filtersToDoc filts = #ifdef DEBUG debug $ #endif
persistent-mongoDB.cabal view
@@ -1,5 +1,5 @@ name: persistent-mongoDB-version: 1.3.2+version: 1.3.3 license: MIT license-file: LICENSE author: Greg Weber <greg@gregweber.info>