basic-test.hs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. {-# LANGUAGE ExistentialQuantification #-}
  2. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  3. {-# LANGUAGE MultiParamTypeClasses #-}
  4. {-# LANGUAGE QuasiQuotes #-}
  5. {-# LANGUAGE TemplateHaskell #-}
  6. {-# LANGUAGE TypeFamilies #-}
  7. {-# LANGUAGE TypeSynonymInstances #-}
  8. {-# LANGUAGE UndecidableInstances #-}
  9. module Main where
  10. import Control.Monad.IO.Class (liftIO)
  11. import Data.Text (Text, pack, unpack)
  12. import qualified Database.Redis as R
  13. import Language.Haskell.TH.Syntax
  14. import Database.Persist
  15. import Database.Persist.Redis
  16. import Database.Persist.TH
  17. let redisSettings = mkPersistSettings (ConT ''RedisBackend)
  18. in share [mkPersist redisSettings] [persistLowerCase|
  19. Person
  20. name String
  21. age Int
  22. deriving Show
  23. |]
  24. d :: R.ConnectInfo
  25. d = R.defaultConnectInfo
  26. host :: Text
  27. host = pack $ R.connectHost d
  28. redisConf :: RedisConf
  29. redisConf = RedisConf host (R.connectPort d) Nothing 10
  30. mkKey :: (Monad m, PersistEntity val) => Text -> m (Key val)
  31. mkKey s = case keyFromValues [PersistText s] of
  32. Right z -> return z
  33. Left a -> fail (unpack a)
  34. main :: IO ()
  35. main =
  36. withRedisConn redisConf $ runRedisPool $ do
  37. _ <- liftIO $ print "Inserting..."
  38. s <- insert $ Person "Test" 12
  39. _ <- liftIO $ print ("Received the key" ++ show s)
  40. key <- mkKey (pack "person_test")
  41. insertKey key $ Person "Test2" 45
  42. repsert s (Person "Test3" 55)
  43. g <- get key :: RedisT IO (Maybe Person)
  44. liftIO $ print g
  45. delete s
  46. return ()