@@ -112,31 +112,40 @@ module Database.Persist.MongoDB
112112 , module Database.Persist
113113 ) where
114114
115- import qualified Data.List.NonEmpty as NEL
116115import Control.Exception (throw , throwIO )
117- import Control.Monad (liftM , (>=>) , forM_ , unless )
116+ import Control.Monad (forM_ , liftM , unless , (>=>) )
118117import Control.Monad.IO.Class (liftIO )
119118import qualified Control.Monad.IO.Class as Trans
120119import Control.Monad.IO.Unlift (MonadUnliftIO , withRunInIO )
121120import Control.Monad.Trans.Reader (ask , runReaderT )
121+ import qualified Data.List.NonEmpty as NEL
122122
123123import Data.Acquire (mkAcquire )
124- import Data.Aeson (Value (Number ), (.:) , (.:?) , (.!=) , FromJSON (.. ), ToJSON (.. ), withText , withObject )
124+ import Data.Aeson
125+ ( FromJSON (.. )
126+ , ToJSON (.. )
127+ , Value (Number )
128+ , withObject
129+ , withText
130+ , (.!=)
131+ , (.:)
132+ , (.:?)
133+ )
125134import Data.Aeson.Types (modifyFailure )
126135import Data.Bits (shiftR )
127136import Data.Bson (ObjectId (.. ))
128137import qualified Data.ByteString as BS
129138import Data.Conduit
130- import Data.Maybe (mapMaybe , fromJust )
139+ import Data.Maybe (fromJust , mapMaybe )
131140import Data.Monoid (mappend )
141+ import qualified Data.Pool as Pool
132142import qualified Data.Serialize as Serialize
133143import Data.Text (Text )
134144import qualified Data.Text as T
135145import qualified Data.Text.Encoding as E
136- import qualified Data.Traversable as Traversable
137- import qualified Data.Pool as Pool
138146import Data.Time (NominalDiffTime )
139147import Data.Time.Calendar (Day (.. ))
148+ import qualified Data.Traversable as Traversable
140149#ifdef HIGH_PRECISION_DATE
141150import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds )
142151#endif
@@ -145,8 +154,14 @@ import Network.Socket (HostName)
145154import Numeric (readHex )
146155import System.Environment (lookupEnv )
147156import Unsafe.Coerce (unsafeCoerce )
157+ import Web.HttpApiData
158+ ( FromHttpApiData (.. )
159+ , ToHttpApiData (.. )
160+ , parseUrlPieceMaybe
161+ , parseUrlPieceWithPrefix
162+ , readTextData
163+ )
148164import Web.PathPieces (PathPiece (.. ))
149- import Web.HttpApiData (ToHttpApiData (.. ), FromHttpApiData (.. ), parseUrlPieceMaybe , parseUrlPieceWithPrefix , readTextData )
150165
151166#ifdef DEBUG
152167import FileLocation (debug )
@@ -156,8 +171,8 @@ import qualified Database.MongoDB as DB
156171import Database.MongoDB.Query (Database )
157172
158173import Database.Persist
159- import qualified Database.Persist.Sql as Sql
160174import Database.Persist.EntityDef.Internal (toEmbedEntityDef )
175+ import qualified Database.Persist.Sql as Sql
161176
162177instance HasPersistBackend DB. MongoContext where
163178 type BaseBackend DB. MongoContext = DB. MongoContext
@@ -430,15 +445,18 @@ toInsertDoc record =
430445 DB. :=
431446 embeddedVal pv
432447 )
433- $ filter (\ (_, pv) -> isNull pv)
448+ $ filter (\ (_, pv) -> not $ isNull pv)
434449 $ zip xs ys
435450 where
436451 isNull PersistNull = True
437452 isNull (PersistMap m) = null m
438453 isNull (PersistList l) = null l
439454 isNull _ = False
440455
441- -- make sure to removed nulls from embedded entities also
456+ -- make sure to removed nulls from embedded entities also.
457+ -- note that persistent no longer supports embedded maps
458+ -- with fields. This means any embedded bson object will
459+ -- insert null. But top level will not.
442460 embeddedVal :: PersistValue -> DB. Value
443461 embeddedVal (PersistMap m) =
444462 DB. Doc $ fmap (\ (k, v) -> k DB. := DB. val v) $ m
@@ -989,25 +1007,24 @@ orderPersistValues entDef castDoc =
9891007 -- another application may use fields we don't care about
9901008 -- our own application may set extra fields with the raw driver
9911009 match [] _ values = values
992- match ((fieldName , medef) : columns) fields values =
1010+ match ((fName , medef) : columns) fields values =
9931011 let
9941012 ((_, pv) , unused) =
9951013 matchOne fields []
9961014 in
9971015 match columns unused $
998- values ++ [(fieldName , nestedOrder medef pv)]
1016+ values ++ [(fName , nestedOrder medef pv)]
9991017 where
1000- nestedOrder (Just _) (PersistMap m) =
1001- PersistMap m
1002- nestedOrder (Just em) (PersistList l) =
1003- PersistList $ map (nestedOrder (Just em)) l
1004- nestedOrder Nothing found =
1005- found
1018+ -- support for embedding other persistent objects into a schema for
1019+ -- mongodb cannot be currently supported in persistent.
1020+ -- The order will be undetermined but that's ok because there is no
1021+ -- schema migration for mongodb anyways.
1022+ -- nestedOrder (Just _) (PersistMap m) = PersistMap m
1023+ nestedOrder (Just em) (PersistList l) = PersistList $ map (nestedOrder (Just em)) l
1024+ nestedOrder _ found = found
10061025
10071026 matchOne (field: fs) tried =
1008- if fieldName == fst field
1009- -- snd drops the name now that it has been used to make the match
1010- -- persistent will add the field name later
1027+ if fName == fst field
10111028 then (field, tried ++ fs)
10121029 else matchOne fs (field: tried)
10131030 -- if field is not found, assume it was a Nothing
@@ -1016,7 +1033,7 @@ orderPersistValues entDef castDoc =
10161033 -- instead, we want to store no field at all: that takes less space.
10171034 -- Also, another ORM may be doing the same
10181035 -- Also, this adding a Maybe field means no migration required
1019- matchOne [] tried = ((fieldName , PersistNull ), tried)
1036+ matchOne [] tried = ((fName , PersistNull ), tried)
10201037
10211038assocListFromDoc :: DB. Document -> [(Text , PersistValue )]
10221039assocListFromDoc = Prelude. map (\ f -> ( (DB. label f), cast (DB. value f) ) )
@@ -1057,8 +1074,7 @@ instance DB.Val PersistValue where
10571074 val (PersistRational _) = throw $ PersistMongoDBUnsupported " PersistRational not implemented for the MongoDB backend"
10581075 val (PersistArray a) = DB. val $ PersistList a
10591076 val (PersistDbSpecific _) = throw $ PersistMongoDBUnsupported " PersistDbSpecific not implemented for the MongoDB backend"
1060- val (PersistLiteral _) = throw $ PersistMongoDBUnsupported " PersistLiteral not implemented for the MongoDB backend"
1061- val (PersistLiteralEscaped _) = throw $ PersistMongoDBUnsupported " PersistLiteralEscaped not implemented for the MongoDB backend"
1077+ val (PersistLiteral_ _ _) = throw $ PersistMongoDBUnsupported " PersistLiteral not implemented for the MongoDB backend"
10621078 cast' (DB. Float x) = Just (PersistDouble x)
10631079 cast' (DB. Int32 x) = Just $ PersistInt64 $ fromIntegral x
10641080 cast' (DB. Int64 x) = Just $ PersistInt64 x
0 commit comments