packages feed

yam-datasource (empty) → 0.5.1

raw patch · 5 files changed

+147/−0 lines, 5 filesdep +basedep +conduitdep +persistentsetup-changed

Dependencies added: base, conduit, persistent, resource-pool, resourcet, unliftio-core, yam

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Daniel YU (c) 2019++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Daniel YU nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,4 @@+# yam-datasource++[![Hackage](https://img.shields.io/badge/hackage-v0.5.1-orange.svg)](https://hackage.haskell.org/package/yam-datasource)+
+ Setup.hs view
@@ -0,0 +1,2 @@+import           Distribution.Simple+main = defaultMain
+ src/Yam/DataSource.hs view
@@ -0,0 +1,71 @@+module Yam.DataSource(+  -- * DataSource Types+    DataSourceProvider+  , DataSource+  , DB+  -- * Primary DataSource Functions+  , runTrans+  , primaryDatasourceMiddleware+  -- * Secondary DataSource Functions+  , runTransWith+  , datasourceMiddleware+  -- * Sql Functions+  , query+  , selectValue+  ) where++import           Control.Exception       (bracket)+import           Control.Monad.IO.Unlift+import           Data.Acquire            (withAcquire)+import           Data.Conduit+import qualified Data.Conduit.List       as CL+import           Data.Pool+import           Database.Persist.Sql    hiding (Key)+import           System.IO.Unsafe        (unsafePerformIO)+import           Yam                     hiding (LogFunc)++type DataSource = ConnectionPool++{-# NOINLINE dataSourceKey #-}+dataSourceKey :: Key DataSource+dataSourceKey = unsafePerformIO newKey++type DataSourceProvider = LoggingT IO DataSource+-- SqlPersistT ~ ReaderT SqlBackend+type DB = SqlPersistT++query+  :: (MonadUnliftIO m)+  => Text+  -> [PersistValue]+  -> DB m [[PersistValue]]+query sql params = do+  res <- rawQueryRes sql params+  withAcquire res (\a -> runConduit $ a .| CL.fold (flip (:)) [])++selectValue :: (PersistField a, MonadUnliftIO m) => Text -> DB m [a]+selectValue sql = fmap unSingle <$> rawSql sql []++runTransWith :: (MonadUnliftIO m) => Key DataSource -> DB (AppM m) a -> AppM m a+runTransWith k a = requireAttr k >>= (`runDB` a)++runTrans :: (MonadUnliftIO m) => DB (AppM m) a -> AppM m a+runTrans = runTransWith dataSourceKey++{-# INLINE runDB #-}+runDB :: (MonadLoggerIO m, MonadUnliftIO m) => DataSource -> DB m a -> m a+runDB pool db = do+  logger <- askLoggerIO+  withRunInIO $ \run -> withResource pool $ run . \c -> runSqlConn db c { connLogFunc = logger }++{-# INLINE runInDB #-}+runInDB :: LogFunc -> DataSourceProvider -> (DataSource -> IO a) -> IO a+runInDB logfunc f g = bracket (runLoggingT f logfunc) destroyAllResources g++datasourceMiddleware :: Key DataSource -> DataSourceProvider -> AppMiddleware+datasourceMiddleware k dsp = AppMiddleware $ \env f -> do+  lf <- askLoggerIO+  logInfo "Datasource Initialized..."+  liftIO $ runInDB lf dsp $ \ds -> runLoggingT (f (setAttr k ds env, id)) lf++primaryDatasourceMiddleware = datasourceMiddleware dataSourceKey
+ yam-datasource.cabal view
@@ -0,0 +1,40 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.1.+--+-- see: https://github.com/sol/hpack+--+-- hash: 4f09f53eec372cb7ff1ab8e598ed809e88dabdfb34fb42604e882b4b19ca4f77++name:           yam-datasource+version:        0.5.1+synopsis:       Yam DataSource Middleware+category:       Web+homepage:       https://github.com/leptonyu/yam/yam-datasource#readme+author:         Daniel YU+maintainer:     Daniel YU <leptonyu@gmail.com>+copyright:      (c) Daniel YU+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md++library+  exposed-modules:+      Yam.DataSource+  other-modules:+      Paths_yam_datasource+  hs-source-dirs:+      src+  default-extensions: AutoDeriveTypeable BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DuplicateRecordFields DoAndIfThenElse EmptyDataDecls ExistentialQuantification FlexibleContexts FlexibleInstances FunctionalDependencies GADTs GeneralizedNewtypeDeriving InstanceSigs KindSignatures LambdaCase MonadFailDesugaring MultiParamTypeClasses MultiWayIf NamedFieldPuns OverloadedStrings PartialTypeSignatures PatternGuards PolyKinds RankNTypes RecordWildCards ScopedTypeVariables StandaloneDeriving TupleSections TypeApplications TypeFamilies TypeOperators TypeSynonymInstances ViewPatterns+  ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -fno-warn-orphans -fno-warn-missing-signatures+  build-depends:+      base >=4.7 && <5+    , conduit+    , persistent+    , resource-pool+    , resourcet+    , unliftio-core+    , yam+  default-language: Haskell2010