-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfde63f
commit 19a472b
Showing
9 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{-# LANGUAGE ConstraintKinds #-} | ||
{-# LANGUAGE FunctionalDependencies #-} | ||
|
||
module Freckle.App.Database.MonadSql | ||
( MonadSql | ||
, MonadSqlBackend (..) | ||
, liftSql | ||
) where | ||
|
||
import Freckle.App.Prelude hiding (lift) | ||
|
||
import Control.Monad.Reader (ReaderT (ReaderT), ask) | ||
import Database.Persist.Sql | ||
|
||
-- | A monadic context in which a Persistent backend is available | ||
-- for running database queries | ||
-- | ||
-- The type parameter @backend@ is the Persistent backend. When the | ||
-- backend is 'SqlBackend', you probably want to use the 'MonadSql' | ||
-- alias rather than refer to this class directly. | ||
class MonadSqlBackend backend m | m -> backend where | ||
getSqlBackend :: m backend | ||
|
||
-- | A monadic context in which a SQL backend is available | ||
-- for running database queries | ||
type MonadSql = MonadSqlBackend SqlBackend | ||
|
||
instance Monad m => MonadSqlBackend backend (ReaderT backend m) where | ||
getSqlBackend = ask | ||
|
||
-- | Can be used e.g. to generalized from 'SqlPersistT' to 'MonadSql' | ||
liftSql :: (Monad m, MonadSqlBackend backend m) => ReaderT backend m a -> m a | ||
liftSql (ReaderT f) = getSqlBackend >>= f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{-# LANGUAGE ConstraintKinds #-} | ||
{-# LANGUAGE FunctionalDependencies #-} | ||
|
||
module Freckle.App.Database.MonadSqlTx | ||
( MonadSqlTx (..) | ||
) where | ||
|
||
import Freckle.App.Prelude | ||
|
||
-- | A monadic context in which database transactions can be run | ||
-- | ||
-- Type parameters: | ||
-- | ||
-- * @m@ - Generally something like 'ReaderT' where the reader context | ||
-- includes a database connection pool. | ||
-- * @db@ - Generally something like 'ReaderT' where the context includes a | ||
-- database connection. Will typically be an instance of | ||
-- 'Freckle.App.Database.MonadSql.MonadSql'. | ||
class MonadSqlTx db m | m -> db where | ||
-- | Runs the action in a SQL transaction | ||
runSqlTx :: HasCallStack => db a -> m a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters