Created
February 13, 2017 01:16
-
-
Save 5outh/d2f6a8762701928ceb967d25710c7ed1 to your computer and use it in GitHub Desktop.
persistent-postgresq example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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