Last active
August 23, 2018 10:48
-
-
Save valderman/3c874a91174c360466f658a78983d8ed to your computer and use it in GitHub Desktop.
MonadSelda + ExceptT
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
{-# LANGUAGE DeriveGeneric, OverloadedStrings, OverloadedLabels #-} | |
import Database.Selda | |
import Database.Selda.Backend (MonadSelda (..)) | |
import Database.Selda.SQLite | |
import Control.Monad.Except | |
instance (MonadMask m, MonadSelda m) => MonadSelda (ExceptT e m) where | |
seldaConnection = ExceptT (Right <$> seldaConnection) | |
type ExceptSeldaT e m = ExceptT e (SeldaT m) | |
run :: (MonadIO m, MonadMask m) | |
=> FilePath | |
-> ExceptSeldaT e m a | |
-> m (Either e a) | |
run file = withSQLite file . runExceptT | |
data Foo = Foo | |
{ id :: ID Foo | |
, foo :: Int | |
} deriving (Show, Generic) | |
instance SqlRow Foo | |
foos :: Table Foo | |
foos = table "foos" [#id :- autoPrimary] | |
getFoos :: ExceptSeldaT Text IO [Foo] | |
getFoos = do | |
fs <- lift $ query $ select foos | |
when (null fs) $ throwError "There were no foos!" | |
return fs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment