import Database.PostgreSQL.Simple.Internal (Field)
import Gargantext.Prelude
import Opaleye (Query, Unpackspec, showSqlForPostgres, FromFields, Select, runQuery)
-import Servant (ServantErr)
import System.IO (FilePath)
import Text.Read (read)
import qualified Data.ByteString as DB
instance HasConnection Connection where
connection = identity
-type CmdM env err m =
+type CmdM' env err m =
( MonadReader env m
- , HasConnection env
, MonadError err m
, MonadIO m
)
+type CmdM env err m =
+ ( CmdM' env err m
+ , HasConnection env
+ )
+
+type Cmd' env err a = forall m. CmdM' env err m => m a
+
type Cmd err a = forall m env. CmdM env err m => m a
-- TODO: ideally there should be very few calls to this functions.
conn <- view connection
liftIO $ k conn
-runCmd :: Connection -> Cmd err a -> IO (Either err a)
-runCmd conn m = runExceptT $ runReaderT m conn
-
--- Use only for dev
-runCmdDev :: Show err => Cmd err a -> IO a
-runCmdDev f = do
- conn <- connectGargandb "gargantext.ini"
- either (fail . show) pure =<< runCmd conn f
-
-runCmdDevWith :: FilePath -> Cmd ServantErr a -> IO a
-runCmdDevWith fp f = do
- conn <- connectGargandb fp
- either (fail . show) pure =<< runCmd conn f
-
-runCmdDevWith' :: Cmd ServantErr a -> IO a
-runCmdDevWith' f = runCmdDevWith "gargantext.ini" f
-
-
-
--- Use only for dev
-runCmdDevNoErr :: Cmd () a -> IO a
-runCmdDevNoErr = runCmdDev
+runCmd :: HasConnection env => env
+ -> Cmd' env err a
+ -> IO (Either err a)
+runCmd env m = runExceptT $ runReaderT m env
runOpaQuery :: Default FromFields fields haskells => Select fields -> Cmd err [haskells]
runOpaQuery q = mkCmd $ \c -> runQuery c q