| Safe Haskell | None |
|---|---|
| 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
- data SqlQueryT 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
- mkSqlQueryEnv :: Pool SqlBackend -> (SqlQueryEnv -> SqlQueryEnv) -> SqlQueryEnv
- data SqlTransaction m a
- rerunnableLift :: MonadUnliftIO m => m a -> SqlTransaction m a
- data TransactionError = RetryLimitExceeded
- 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
The monad transformer that implements MonadSqlQuery.
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
Transactions
data SqlTransaction m a Source #
The monad that tracks transaction state.
Conceptually equivalent to SqlPersistT, but restricts
IO operations, for two reasons:
1. Forking a thread that uses the same SqlBackend as the current thread
causes Bad Things to happen.
2. 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
rerunnableLift :: MonadUnliftIO m => m a -> SqlTransaction m a Source #
SqlTransaction does not have an instance for MonadTrans to prevent
accidental lifting of unsafe monadic actions. Use this function to explicitly
mark a monadic action as rerunnable.
data TransactionError Source #
Errors that can occur within a SQL transaction.
Constructors
| RetryLimitExceeded | The retry limit was reached when retrying a transaction. |
Instances
| Eq TransactionError Source # | |
Defined in Database.Persist.Monad Methods (==) :: TransactionError -> TransactionError -> Bool # (/=) :: TransactionError -> TransactionError -> Bool # | |
| Show TransactionError Source # | |
Defined in Database.Persist.Monad Methods showsPrec :: Int -> TransactionError -> ShowS # show :: TransactionError -> String # showList :: [TransactionError] -> ShowS # | |
| Exception TransactionError Source # | |
Defined in Database.Persist.Monad Methods toException :: TransactionError -> SomeException # | |
Lifted functions
module Database.Persist.Monad.Shim