-
Notifications
You must be signed in to change notification settings - Fork 39
/
primitive.hs
40 lines (34 loc) · 1.34 KB
/
primitive.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Monad.IO.Class (liftIO)
import Database.Groundhog.Core
import Database.Groundhog.Sqlite
import Database.Groundhog.TH
data WeekDay = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
deriving (Eq, Show, Enum)
data Time = Time {hour :: Int, minute :: Int}
deriving (Eq, Show, Read)
data Alarm = Alarm {weekDay :: WeekDay, time :: Time}
deriving (Eq, Show)
mkPersist
defaultCodegenConfig
[groundhog|
- entity: Alarm # Name of the datatype
- primitive: WeekDay # For WeekDay PrimitivePersistField instance will be created
converter: enumConverter # It will be stored as an integer column. Conversion between WeekDay and PersistValue will use Enum instance.
- primitive: Time
converter: showReadConverter # It will be stored as a string column. Conversion between Time and PersistValue will use Show and Read instances.
|]
main = withSqliteConn ":memory:" $
runDbConn $ do
runMigration $ do
migrate (undefined :: Alarm)
let alarm = Alarm Monday (Time 07 00)
insert alarm
alarms <- select $ WeekDayField ==. Monday
liftIO $ print alarms