Skip to content

Instantly share code, notes, and snippets.

@valderman
Last active August 23, 2018 10:48
Show Gist options
  • Save valderman/3c874a91174c360466f658a78983d8ed to your computer and use it in GitHub Desktop.
Save valderman/3c874a91174c360466f658a78983d8ed to your computer and use it in GitHub Desktop.
MonadSelda + ExceptT
{-# 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