| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Database.Persist.Monad
Description
Defines the SqlQueryT monad transformer, which has a MonadSqlQuery instance
to execute persistent database operations. Also provides easy transaction
management with withTransaction, which supports retrying with exponential
backoff and restricts IO actions to only allow IO actions explicitly marked
as rerunnable.
Usage:
myFunction :: (MonadSqlQuery m, MonadIO m) => m ()
myFunction = do
insert_ $ Person { name = "Alice", age = Just 25 }
insert_ $ Person { name = "Bob", age = Nothing }
-- some other business logic
personList <- selectList [] []
liftIO $ print (personList :: [Person])
-- everything in here will run in a transaction
withTransaction $ do
selectFirst [PersonAge >. 30] [] >>= \case
Nothing -> insert_ $ Person { name = "Claire", age = Just 50 }
Just (Entity key person) -> replace key person{ age = Just (age person - 10) }
-- liftIO doesn't work in here, since transactions can be retried.
-- Use rerunnableIO to run IO actions, after verifying that the IO action
-- can be rerun if the transaction needs to be retried.
rerunnableIO $ putStrLn "Transaction is finished!"
-- some more business logic
return ()
Synopsis
- class (Monad m, MonadSqlQuery (TransactionM m)) => MonadSqlQuery m
- withTransaction :: MonadSqlQuery m => TransactionM m a -> m a
- newtype SqlQueryT m a = SqlQueryT {
- unSqlQueryT :: ReaderT SqlQueryEnv m a
- mapSqlQueryT :: (m a -> n b) -> SqlQueryT m a -> SqlQueryT n b
- runSqlQueryT :: Pool SqlBackend -> SqlQueryT m a -> m a
- runSqlQueryTWith :: SqlQueryEnv -> SqlQueryT m a -> m a
- data SqlQueryEnv = SqlQueryEnv {
- backendPool :: Pool SqlBackend
- retryIf :: SomeException -> Bool
- retryLimit :: Int
- retryCallback :: SomeException -> IO ()
- mkSqlQueryEnv :: Pool SqlBackend -> (SqlQueryEnv -> SqlQueryEnv) -> SqlQueryEnv
- getSqlBackendPool :: Monad m => SqlQueryT m (Pool SqlBackend)
- data SqlTransaction m a
- data TransactionError = RetryLimitExceeded
- catchSqlTransaction :: (MonadUnliftIO m, Exception e) => SqlTransaction m a -> (e -> SqlTransaction m a) -> SqlTransaction m a
- module Database.Persist.Monad.Shim
Type class for executing database queries
class (Monad m, MonadSqlQuery (TransactionM m)) => MonadSqlQuery m Source #
The type-class for monads that can run persistent database queries.
Minimal complete definition
Instances
withTransaction :: MonadSqlQuery m => TransactionM m a -> m a Source #
Run all queries in the given action using the same database connection.
SqlQueryT monad transformer
newtype SqlQueryT m a Source #
The monad transformer that implements MonadSqlQuery.
Constructors
| SqlQueryT | |
Fields
| |
Instances
mapSqlQueryT :: (m a -> n b) -> SqlQueryT m a -> SqlQueryT n b Source #
runSqlQueryT :: Pool SqlBackend -> SqlQueryT m a -> m a Source #
Run the SqlQueryT monad transformer with the given backend.
runSqlQueryTWith :: SqlQueryEnv -> SqlQueryT m a -> m a Source #
Run the SqlQueryT monad transformer with the explicitly provided
environment.
data SqlQueryEnv Source #
Environment to configure running SqlQueryT.
For simple usage, you can just use runSqlQueryT, but for more advanced
usage, including the ability to retry transactions, use mkSqlQueryEnv with
runSqlQueryTWith.
Constructors
| SqlQueryEnv | |
Fields
| |
mkSqlQueryEnv :: Pool SqlBackend -> (SqlQueryEnv -> SqlQueryEnv) -> SqlQueryEnv Source #
Build a SqlQueryEnv from the default.
Usage:
let env = mkSqlQueryEnv pool $ \env -> env { retryIf = 10 }
in runSqlQueryTWith env m
SqlQueryT environment
getSqlBackendPool :: Monad m => SqlQueryT m (Pool SqlBackend) Source #
Transactions
data SqlTransaction m a Source #
The monad that tracks transaction state.
Conceptually equivalent to SqlPersistT, but restricts
IO operations, for two reasons:
- Forking a thread that uses the same
SqlBackendas the current thread causes Bad Things to happen. - Transactions may need to be retried, in which case IO operations in a transaction are required to be rerunnable.
You shouldn't need to explicitly use this type; your functions should only
declare the MonadSqlQuery constraint.
Instances
data TransactionError Source #
Errors that can occur when running a SQL transaction.
Constructors
| RetryLimitExceeded | The retry limit was reached when retrying a transaction. |
Instances
| Exception TransactionError Source # | |
Defined in Database.Persist.Monad Methods toException :: TransactionError -> SomeException # | |
| Show TransactionError Source # | |
Defined in Database.Persist.Monad Methods showsPrec :: Int -> TransactionError -> ShowS # show :: TransactionError -> String # showList :: [TransactionError] -> ShowS # | |
| Eq TransactionError Source # | |
Defined in Database.Persist.Monad Methods (==) :: TransactionError -> TransactionError -> Bool # (/=) :: TransactionError -> TransactionError -> Bool # | |
catchSqlTransaction :: (MonadUnliftIO m, Exception e) => SqlTransaction m a -> (e -> SqlTransaction m a) -> SqlTransaction m a Source #
Like normal catch, except ignores errors specified by ignoreCatch.
Lifted functions
module Database.Persist.Monad.Shim