Skip to content

Commit

Permalink
more detailed descriptions for the basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
lykahb committed Mar 18, 2016
1 parent c424886 commit 6d2f681
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions examples/basic.hs
Expand Up @@ -18,18 +18,22 @@ mkPersist defaultCodegenConfig [groundhog|
|]

main = withSqliteConn ":memory:" $ runDbConn $ do
-- create tables
runMigration $ do
migrate (undefined :: Customer)
migrate (undefined :: Product)
johnKey <- insert $ Customer "John Doe" "0123456789"
janeKey <- insert $ Customer "Jane Doe" "987654321"
-- get customer by its primary key
get johnKey >>= liftIO . print
-- insert objects with a foreign key
insert $ Product "Apples" 5 johnKey
insert $ Product "Melon" 3 johnKey
janeKey <- insert $ Customer "Jane Doe" "987654321"
insert $ Product "Oranges" 4 janeKey
-- select all customers without condition
allCustomers <- select CondEmpty
liftIO $ putStrLn $ "All customers: " ++ show (allCustomers :: [Customer])
insert $ Product "Oranges" 4 janeKey
-- bonus melon for all large melon orders. The values used in expressions should have known type, so literal 5 is annotated.
-- bonus melon for all large melon orders. The values used in expressions should have defined type, so literal 5 is annotated.
update [QuantityField =. liftExpr QuantityField + 1] (ProductNameField ==. "Melon" &&. QuantityField >. (5 :: Int))
productsForJohn <- select $ (CustomerField ==. johnKey) `orderBy` [Asc ProductNameField]
liftIO $ putStrLn $ "Products for John: " ++ show productsForJohn

0 comments on commit 6d2f681

Please sign in to comment.