| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hyperion.Database.HasDB
Contents
Synopsis
- data DatabaseConfig = DatabaseConfig {
- dbPool :: Pool Connection
- dbProgramId :: ProgramId
- dbRetries :: Int
- class HasDB env where
- dbConfigLens :: Lens' env DatabaseConfig
- type Pool = Pool Connection
- newDefaultPool :: FilePath -> IO (Pool Connection)
- withConnection :: forall m env a. (MonadIO m, MonadReader env m, HasDB env) => (Connection -> IO a) -> m a
- withConnectionRetry :: forall m env a. (MonadIO m, MonadReader env m, HasDB env, MonadCatch m) => (Connection -> IO a) -> m a
General comments
Hyperion.Database.HasDB provides typeclass HasDB which describes environments that contain a
DatabaseConfig, which is extracted by Lens' dbConfigLens.
This is used in the following way: if we have a monad m that
- is an instance of
MonadIO, i.e. embedsIOactions, - an instance of
MonadReader, i.e. it carries an environment, - this environment is an instance of
HasDB,
then we can create m-actions using withConnection, i.e.
doStuffWithConnection :: Sql.Connection -> IO a ... do -- here we are in m monad ... result <- withConnection doStuffWithConnection ...
withConnection uses Data.Pool. See withResource for details.
Documentation
data DatabaseConfig Source #
Database information datatype
Constructors
| DatabaseConfig | |
Fields
| |
Instances
| HasDB DatabaseConfig Source # | |
Defined in Hyperion.Database.HasDB Methods dbConfigLens :: Lens' DatabaseConfig DatabaseConfig Source # | |
class HasDB env where Source #
HasDB typeclass
Methods
dbConfigLens :: Lens' env DatabaseConfig Source #
Instances
| HasDB DatabaseConfig Source # | |
Defined in Hyperion.Database.HasDB Methods dbConfigLens :: Lens' DatabaseConfig DatabaseConfig Source # | |
| HasDB ClusterEnv Source # |
|
Defined in Hyperion.Cluster Methods dbConfigLens :: Lens' ClusterEnv DatabaseConfig Source # | |
| HasDB JobEnv Source # | |
Defined in Hyperion.Job Methods dbConfigLens :: Lens' JobEnv DatabaseConfig Source # | |
newDefaultPool :: FilePath -> IO (Pool Connection) Source #
Produces a default pool with connections to the SQLite DB in the given file
withConnection :: forall m env a. (MonadIO m, MonadReader env m, HasDB env) => (Connection -> IO a) -> m a Source #
Extracts the connection pool from the environment of our monad, gets a connection and runs the supplied function with it
withConnectionRetry :: forall m env a. (MonadIO m, MonadReader env m, HasDB env, MonadCatch m) => (Connection -> IO a) -> m a Source #
Tries withConnection until succeeds. Failure means that SQLError is
thrown during execution of the function. Otherwise execution is deemed successful.
The number of attempts is determined by DatabaseConfig in the environment.
If last attempt is a failure, the last exception propagates
outside of withConnectionRetry. Uses retryRepeated internally.