Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Last active December 18, 2015 11:49
Show Gist options
  • Save xkikeg/5778204 to your computer and use it in GitHub Desktop.
Save xkikeg/5778204 to your computer and use it in GitHub Desktop.
Yesod Book :: Persistent :: Synopsis
{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings #-}
{-# LANGUAGE GADTs, FlexibleContexts #-}
import Data.Conduit (runResourceT)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
import Control.Monad.Logger (runStderrLoggingT)
import Control.Monad.IO.Class (liftIO)
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name String
age Int Maybe
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]
main :: IO ()
main = runStderrLoggingT $ runResourceT $ withSqliteConn ":memory:" $ runSqlConn $ do
runMigration migrateAll
johnId <- insert $ Person "John Doe" $ Just 35
janeId <- insert $ Person "Jane Doe" Nothing
insert $ BlogPost "My fr1st p0st" johnId
insert $ BlogPost "One more for good measure" johnId
oneJohnPost <- selectList [BlogPostAuthorId ==. johnId] [LimitTo 1]
liftIO $ print (oneJohnPost :: [Entity BlogPost])
john <- get johnId
liftIO $ print (john :: Maybe Person)
delete janeId
deleteWhere [BlogPostAuthorId ==. johnId]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment