Last active
December 18, 2015 11:49
-
-
Save xkikeg/5778204 to your computer and use it in GitHub Desktop.
Yesod Book :: Persistent :: Synopsis
This file contains hidden or 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 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