Skip to content

Instantly share code, notes, and snippets.

@5outh
Created February 13, 2017 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/b9d80d7fc174ae5aa87818dfd5158362 to your computer and use it in GitHub Desktop.
Save 5outh/b9d80d7fc174ae5aa87818dfd5158362 to your computer and use it in GitHub Desktop.
persistent-mysql example
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Types
import Database.Persist.MySQL
import Control.Monad.Logger
import Control.Monad.IO.Class
printIO :: (MonadIO m, Show a) => a -> m ()
printIO = liftIO . print
createGame :: MonadIO m => BoardGame -> SqlPersistT m (Entity BoardGame)
createGame = insertEntity
readGame :: MonadIO m => Int -> SqlPersistT m (Maybe BoardGame)
readGame = get . toSqlKey . fromIntegral
connectInfo :: ConnectInfo
connectInfo = defaultConnectInfo
{ connectDatabase = "board_games"
}
main :: IO ()
main =
runStdoutLoggingT
. withMySQLPool connectInfo 3
. runSqlPool
$ do
result <- createGame $ BoardGame
"Cosmic Encounter"
"Bill Eberle"
(Just 2008)
liftIO $ print result
-- This is the easiest way to re-read a record:
get (entityKey result) >>= printIO
-- Getting by id
readGame 1 >>= printIO
-- Get by name too:
getBy (UniqueName "Cosmic Encounter") >>= printIO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment