{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE ScopedTypeVariables        #-}

module Persistence.SQLite (connection) where

import Persistence.DBConfig

import Control.Monad.IO.Class
import Control.Monad.Trans.Control
import Control.Monad.Logger
import Control.Monad.IO.Unlift
import Data.Maybe
import Data.Pool (Pool)
import Data.Text (pack)
import Database.Persist.Sqlite


connection :: ( MonadIO m
              , MonadBaseControl IO m
              , MonadLogger m
              , MonadLoggerIO m
              , MonadUnliftIO m
              )
           => DBConfig -> Int -> (Pool SqlBackend -> m a) -> m a
connection :: DBConfig -> Int -> (Pool SqlBackend -> m a) -> m a
connection dbConfig :: DBConfig
dbConfig defaultPoolSize :: Int
defaultPoolSize =
  Text -> Int -> (Pool SqlBackend -> m a) -> m a
forall (m :: * -> *) a.
(MonadUnliftIO m, MonadLogger m) =>
Text -> Int -> (Pool SqlBackend -> m a) -> m a
withSqlitePool (String -> Text
pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ DBConfig -> String
database DBConfig
dbConfig) (Int -> (Pool SqlBackend -> m a) -> m a)
-> Int -> (Pool SqlBackend -> m a) -> m a
forall a b. (a -> b) -> a -> b
$
    Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
defaultPoolSize (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ DBConfig -> Maybe Int
pool DBConfig
dbConfig