postgresql.hs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env stack
  2. -- stack --resolver lts-9.21 script
  3. {-# LANGUAGE EmptyDataDecls #-}
  4. {-# LANGUAGE FlexibleContexts #-}
  5. {-# LANGUAGE GADTs #-}
  6. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  7. {-# LANGUAGE MultiParamTypeClasses #-}
  8. {-# LANGUAGE OverloadedStrings #-}
  9. {-# LANGUAGE QuasiQuotes #-}
  10. {-# LANGUAGE TemplateHaskell #-}
  11. {-# LANGUAGE TypeFamilies #-}
  12. import Control.Monad.Logger (runStdoutLoggingT)
  13. import Control.Monad.IO.Class (liftIO)
  14. import Database.Persist
  15. import Database.Persist.Postgresql
  16. import Database.Persist.TH
  17. import Control.Monad.Reader
  18. share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
  19. Person
  20. name String
  21. age Int Maybe
  22. deriving Show Eq
  23. BlogPost
  24. title String
  25. authorId PersonId
  26. deriving Show Eq
  27. |]
  28. main :: IO ()
  29. main = runStdoutLoggingT $ withPostgresqlPool "dbname=persistent" 1 $ liftSqlPersistMPool $ do
  30. runMigration migrateAll
  31. johnId <- insert $ Person "John Doe" $ Just 35
  32. delete johnId