Instantly share code, notes, and snippets.

Embed
What would you like to do?
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