Skip to content

Commit

Permalink
Add postgres example code
Browse files Browse the repository at this point in the history
  • Loading branch information
psibi committed Nov 25, 2016
1 parent 687a1be commit a2eae79
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cookbook.md
Expand Up @@ -37,6 +37,7 @@ For non-Yesod code, see also [Snippets](https://github.com/yesodweb/yesod-cookbo
* [Lens for existing site](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Lens-for-existing-site.md)
* [Connecting to an additional existing database](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Connecting-to-an-additional-existing-database.md)
* [Example MySQL Connection code](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Example-MySQL-Connection-code.md)
* [Example Postgres Connection code](./cookbook/postgres-example-code.md)
* [Activate foreign key checking in Sqlite](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Activate-foreign-key-checking-in-Sqlite.md)
* [Adding Seed data to Scaffolded Site](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Adding-Seed-Data-to-Scaffolded-Site.md)
* [Handling exception in persistence](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Handling-Persistence-Exception.md)
Expand Down
56 changes: 56 additions & 0 deletions cookbook/postgres-example-code.md
@@ -0,0 +1,56 @@
Example Posgres Persistent code:

``` haskell
#!/usr/bin/env stack
{- stack
--resolver lts-7.3
--install-ghc
runghc
--package persistent
--package persistent-postgresql
--package persistent-template
--package network
--package mtl
-}


{-# 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 Control.Monad.Logger (runStderrLoggingT)
import Database.Persist
import Database.Persist.Postgresql
import Database.Persist.TH

share
[mkPersist sqlSettings, mkMigrate "migrateAll"]
[persistLowerCase|
Peerson
name String
age Int
UniqueAge age
deriving Show
|]

connStr = "host=localhost dbname=test user=postgres password=postgres port=5432"

main :: IO ()
main =
runStderrLoggingT $
withPostgresqlPool connStr 10 $
\pool ->
liftIO $
do flip runSqlPersistMPool pool $
do runMigration migrateAll
johnId <- insert $ Peerson "John Doe" 35
liftIO $ print johnId
return ()
```

0 comments on commit a2eae79

Please sign in to comment.