Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created May 10, 2021 15:33
Show Gist options
  • Save iporsut/9d94ed88d2076d0824da9d82365beb84 to your computer and use it in GitHub Desktop.
Save iporsut/9d94ed88d2076d0824da9d82365beb84 to your computer and use it in GitHub Desktop.
Haskell persistent-mysql example
module Main where
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sql
import Control.Monad.IO.Class
import Control.Monad.Reader
import Database.Persist.MySQL
import Control.Monad.Logger
share [mkPersist sqlSettings] [persistLowerCase|
Person
name String
age Int Maybe
deriving Show
|]
listPerson :: MonadIO m => SqlPersistT m [Entity Person]
listPerson = selectList [] []
conn :: ConnectInfo
conn = defaultConnectInfo {
connectPassword = "password",
connectHost = "0.0.0.0",
connectDatabase = "learning_sql"
}
main :: IO ()
main = runStderrLoggingT $ withMySQLPool conn 1 $ runSqlPool $ do
me <- (entityVal.head) <$> listPerson
p $ "Name: " <> (personName me)
case personAge me of
Just age -> p $ "Age: " <> (show age)
_ -> return ()
where
p = liftIO . putStrLn
name: haskell-mysql
version: 0.1.0.0
github: "githubuser/haskell-mysql"
license: BSD3
author: "Author name here"
maintainer: "example@example.com"
copyright: "2021 Author name here"
extra-source-files:
- README.md
- ChangeLog.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/haskell-mysql#readme>
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
executables:
haskell-mysql-exe:
main: Main.hs
source-dirs: app
default-extensions:
- OverloadedStrings
- QuasiQuotes
- TemplateHaskell
- TypeFamilies
- GADTs
- DerivingStrategies
- GeneralizedNewtypeDeriving
- StandaloneDeriving
- UndecidableInstances
- DataKinds
- FlexibleInstances
- MultiParamTypeClasses
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- haskell-mysql
- persistent
- persistent-template
- persistent-mysql
- mysql
- bytestring
- containers
- mtl
- monad-logger
tests:
haskell-mysql-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- haskell-mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment