Safe Haskell | None |
---|
Database.Persist.MongoDB
Contents
Description
Use persistent-mongodb the same way you would use other persistent libraries and refer to the general persistent documentation. There are some new MongoDB specific filters under the filters section. These help extend your query into a nested document.
However, at some point you will find the normal Persistent APIs lacking. and want lower level-level MongoDB access. There are functions available to make working with the raw driver easier: they are under the Entity conversion section. You should still use the same connection pool that you are using for Persistent.
MongoDB is a schema-less database. The MongoDB Persistent backend does not help perform migrations. Unlike SQL backends, uniqueness constraints cannot be created for you. You must place a unique index on unique fields.
- collectionName :: PersistEntity record => record -> Text
- entityToDocument :: PersistEntity record => record -> [Field]
- entityToFields :: PersistEntity record => record -> [Field]
- toInsertFields :: forall record. PersistEntity record => record -> [Field]
- docToEntityEither :: forall record. PersistEntity record => Document -> Either Text (Entity record)
- docToEntityThrow :: forall m record. (MonadIO m, PersistEntity record) => Document -> m (Entity record)
- (->.) :: forall val nes nes1. PersistEntity nes1 => EntityField val nes1 -> EntityField nes1 nes -> NestedField val nes
- (~>.) :: forall val nes nes1. PersistEntity nes1 => EntityField val nes1 -> NestedField nes1 nes -> NestedField val nes
- (?->.) :: forall val nes nes1. PersistEntity nes1 => EntityField val (Maybe nes1) -> EntityField nes1 nes -> NestedField val nes
- (?~>.) :: forall val nes nes1. PersistEntity nes1 => EntityField val (Maybe nes1) -> NestedField nes1 nes -> NestedField val nes
- nestEq :: forall v typ. (PersistField typ, PersistEntityBackend v ~ MongoBackend) => NestedField v typ -> typ -> Filter v
- multiEq :: forall v typ. (PersistField typ, PersistEntityBackend v ~ MongoBackend) => EntityField v [typ] -> typ -> Filter v
- withMongoDBConn :: (MonadIO m, Applicative m) => Database -> HostName -> PortID -> Maybe MongoAuth -> NominalDiffTime -> (ConnectionPool -> m b) -> m b
- withMongoDBPool :: (MonadIO m, Applicative m) => Database -> HostName -> PortID -> Maybe MongoAuth -> Int -> Int -> NominalDiffTime -> (ConnectionPool -> m b) -> m b
- createMongoDBPool :: (MonadIO m, Applicative m) => Database -> HostName -> PortID -> Maybe MongoAuth -> Int -> Int -> NominalDiffTime -> m ConnectionPool
- runMongoDBPool :: (MonadIO m, MonadBaseControl IO m) => AccessMode -> Action m a -> ConnectionPool -> m a
- runMongoDBPoolDef :: (MonadIO m, MonadBaseControl IO m) => Action m a -> ConnectionPool -> m a
- type ConnectionPool = Pool Connection
- data Connection
- data MongoConf = MongoConf {}
- data MongoBackend
- data MongoAuth = MongoAuth Username Password
- type PipePool = Pool Pipe
- createMongoDBPipePool :: (MonadIO m, Applicative m) => HostName -> PortID -> Int -> Int -> NominalDiffTime -> m PipePool
- runMongoDBPipePool :: (MonadIO m, MonadBaseControl IO m) => AccessMode -> Database -> Action m a -> PipePool -> m a
- keyToOid :: PersistEntity val => KeyBackend MongoBackend val -> ObjectId
- oidToKey :: PersistEntity val => ObjectId -> KeyBackend MongoBackend val
- type HostName = String
- data PortID
- type Database = Text
- data Action m a
- data AccessMode
- master :: AccessMode
- slaveOk :: AccessMode
- (=:) :: Val v => Label -> v -> Field
- module Database.Persist
Entity conversion
collectionName :: PersistEntity record => record -> TextSource
entityToDocument :: PersistEntity record => record -> [Field]Source
convert a PersistEntity into document fields.
unlike toInsertFields
, nulls are included.
entityToFields :: PersistEntity record => record -> [Field]Source
Deprecated: Please use entityToDocument instead
Deprecated, use the better named entityToDocument
toInsertFields :: forall record. PersistEntity record => record -> [Field]Source
convert a PersistEntity into document fields.
for inserts only: nulls are ignored so they will be unset in the document.
entityToFields
includes nulls
docToEntityEither :: forall record. PersistEntity record => Document -> Either Text (Entity record)Source
docToEntityThrow :: forall m record. (MonadIO m, PersistEntity record) => Document -> m (Entity record)Source
MongoDB specific Filters
You can find example usage for all of Persistent in our test cases: https://github.com/yesodweb/persistent/blob/master/persistent-test/EmbedTest.hs#L144
These filters create a query that reaches deeper into a document with nested fields.
(->.) :: forall val nes nes1. PersistEntity nes1 => EntityField val nes1 -> EntityField nes1 nes -> NestedField val nesSource
Point to a nested field to query. Used for the final level of nesting with nestEq
or other operators.
(~>.) :: forall val nes nes1. PersistEntity nes1 => EntityField val nes1 -> NestedField nes1 nes -> NestedField val nesSource
Point to a nested field to query. This level of nesting is not the final level. Use (->.) to point to the final level is
(?->.) :: forall val nes nes1. PersistEntity nes1 => EntityField val (Maybe nes1) -> EntityField nes1 nes -> NestedField val nesSource
Same as (->.), but Works against a Maybe type
(?~>.) :: forall val nes nes1. PersistEntity nes1 => EntityField val (Maybe nes1) -> NestedField nes1 nes -> NestedField val nesSource
Same as (~>.), but Works against a Maybe type
nestEq :: forall v typ. (PersistField typ, PersistEntityBackend v ~ MongoBackend) => NestedField v typ -> typ -> Filter vSource
The normal Persistent equality test (==.) is not generic enough. Instead use this with the drill-down operaters (->.) or (?->.)
multiEq :: forall v typ. (PersistField typ, PersistEntityBackend v ~ MongoBackend) => EntityField v [typ] -> typ -> Filter vSource
use to see if an embedded list contains an item
using connections
withMongoDBConn :: (MonadIO m, Applicative m) => Database -> HostName -> PortID -> Maybe MongoAuth -> NominalDiffTime -> (ConnectionPool -> m b) -> m bSource
withMongoDBPool :: (MonadIO m, Applicative m) => Database -> HostName -> PortID -> Maybe MongoAuth -> Int -> Int -> NominalDiffTime -> (ConnectionPool -> m b) -> m bSource
Arguments
:: (MonadIO m, Applicative m) | |
=> Database | |
-> HostName | |
-> PortID | |
-> Maybe MongoAuth | |
-> Int | pool size (number of stripes) |
-> Int | stripe size (number of connections per stripe) |
-> NominalDiffTime | time a connection is left idle before closing |
-> m ConnectionPool |
runMongoDBPool :: (MonadIO m, MonadBaseControl IO m) => AccessMode -> Action m a -> ConnectionPool -> m aSource
runMongoDBPoolDef :: (MonadIO m, MonadBaseControl IO m) => Action m a -> ConnectionPool -> m aSource
use default AccessMode
type ConnectionPool = Pool ConnectionSource
data Connection Source
Information required to connect to a mongo database
Constructors
MongoConf | |
Fields
|
Instances
data MongoBackend Source
Instances
PathPiece (KeyBackend MongoBackend entity) |
using raw MongoDB pipes
Arguments
:: (MonadIO m, Applicative m) | |
=> HostName | |
-> PortID | |
-> Int | pool size (number of stripes) |
-> Int | stripe size (number of connections per stripe) |
-> NominalDiffTime | time a connection is left idle before closing |
-> m PipePool |
A pool of plain MongoDB pipes. The database parameter has not yet been applied yet. This is useful for switching between databases (on the same host and port) Unlike the normal pool, no authentication is available
runMongoDBPipePool :: (MonadIO m, MonadBaseControl IO m) => AccessMode -> Database -> Action m a -> PipePool -> m aSource
run a pool created with createMongoDBPipePool
Key conversion helpers
keyToOid :: PersistEntity val => KeyBackend MongoBackend val -> ObjectIdSource
oidToKey :: PersistEntity val => ObjectId -> KeyBackend MongoBackend valSource
network type
Either a host name e.g., "haskell.org"
or a numeric host
address string consisting of a dotted decimal IPv4 address or an
IPv6 address e.g., "192.168.0.1"
.
MongoDB driver types
data Action m a
A monad on top of m (which must be a MonadIO) that may access the database and may fail with a DB Failure
Instances
MonadTrans Action | |
MonadTransControl Action | |
(MonadIO m, MonadBaseControl b m) => MonadBaseControl b (Action m) | |
MonadBase b m => MonadBase b (Action m) | |
Monad m => MonadError Failure (Action m) | |
Monad m => Monad (Action m) | |
Functor m => Functor (Action m) | |
(Monad m, Functor m) => Applicative (Action m) | |
MonadThrow m => MonadThrow (Action m) | |
(MonadBaseControl IO m, Applicative m, Functor m) => MonadDB (Action m) | |
MonadIO m => MonadIO (Action m) | |
(Applicative m, Functor m, MonadIO m, MonadBaseControl IO m) => PersistQuery (Action m) | |
(Applicative m, Functor m, MonadIO m, MonadBaseControl IO m) => PersistUnique (Action m) | |
(Applicative m, Functor m, MonadIO m, MonadBaseControl IO m) => PersistStore (Action m) |
data AccessMode
Type of reads and writes to perform
Constructors
ReadStaleOk | Read-only action, reading stale data from a slave is OK. |
UnconfirmedWrites | Read-write action, slave not OK, every write is fire & forget. |
ConfirmWrites GetLastError | Read-write action, slave not OK, every write is confirmed with getLastError. |
Instances
master :: AccessMode
Same as ConfirmWrites
[]
Same as ReadStaleOk
Database.Persist
module Database.Persist