DB.hs

Anonymous Coward 2018-03-08 19:33:33.62912 UTC

1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE QuasiQuotes #-}
5{-# LANGUAGE TemplateHaskell #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9
10module LORAScale.DB where
11
12import Control.Monad.IO.Class (liftIO)
13import Control.Monad.Logger
14import Data.Text
15import Data.Time
16--import Database.Esqueleto
17import Database.Persist hiding ((==.))
18import Database.Persist.Sql -- (runMigration)
19import Database.Persist.Sqlite (runSqlite)
20import Database.Persist.Postgresql -- (runSqlite)
21import Database.Persist.TH
22
23share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
24Sample
25 time UTCTime
26 sensor Text
27 rssi Double
28 snr Double
29 temperature Double
30 humidity Double
31 deriving Show
32|]
33
34connStr = "host=localhost dbname=tutorial user=postgres password=blah port=5432"
35
36test :: IO ()
37test = runStderrLoggingT $ withPostgresqlPool connStr 10 $ liftSqlPersistMPool $ do
38 runMigration migrateAll
39 now <- liftIO getCurrentTime
40 _ <- insert (Sample now "tester" 0.0 1.0 2.0 3.0)
41 return ()
42
43addSample :: Double -> Double -> Double -> Double -> IO ()
44addSample rssi snr temp hum = runStderrLoggingT $ withPostgresqlPool connStr 10 $ liftSqlPersistMPool $ do
45 runMigration migrateAll
46 now <- liftIO getCurrentTime
47 _ <- insert (Sample now "tester" rssi snr temp hum)
48 return ()