Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Persistent

Demo zum Gebrauch von Persist ohne Yesod

Haskell source code icon Persistent.hs — Haskell source code, 1 KB (1429 bytes)

Dateiinhalt

{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}
import           Control.Monad.IO.Class  (liftIO)
import           Database.Persist
import           Database.Persist.Sqlite
import           Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

-- > echo '.dump' | sqlite3 test.sql > test.dump.sql

main :: IO ()
main =
  -- runSqlite ":memory:" $ do
  runSqlite "test.sql" $ do
    runMigration migrateAll

    johnId <- insert $ Person "John Doe" $ Just 35
    janeId <- insert $ Person "Jane Doe" Nothing

    fstPostId <- 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
    post <- get fstPostId

    liftIO $ print (john :: Maybe Person)

    --delete janeId
    --deleteWhere [BlogPostAuthorId ==. johnId]

Artikelaktionen


Funktionsleiste