Skip to content

Instantly share code, notes, and snippets.

@5outh
Created February 13, 2017 01:16
Show Gist options
  • Save 5outh/d2f6a8762701928ceb967d25710c7ed1 to your computer and use it in GitHub Desktop.
Save 5outh/d2f6a8762701928ceb967d25710c7ed1 to your computer and use it in GitHub Desktop.
persistent-postgresq example
{-# LANGUAGE OverloadedStrings #-}
import Types
import Database.Persist.Postgresql
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
main :: IO ()
main =
runStdoutLoggingT
. withPostgresqlPool "host=localhost port=5432 connect_timeout=10" 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