Databases
postgresql-simple, mysql-simple, sqlite-simple Raw SQL with typed row parsing
Fast PostgreSQL with explicit encoders/decoders (+ hasql-th for compile-time checked SQL) persistent + esqueleto
Model definitions via TH, migrations, type-safe query DSL beam, opaleye, rel8, selda Type-safe relational query builders Schema at the type level
redis (hedis), mongoDB, cassandra clients
{-# LANGUAGE OverloadedStrings #-}
-- postgresql-simple: straightforward SQL with parameter substitution
import Database.PostgreSQL.Simple
data User = User {userId :: Int, userName :: String} deriving Show
instance FromRow User where
fromRow = User <$> field <*> field
main :: IO ()
main = do
conn <- connectPostgreSQL "host=localhost dbname=app user=app"
_ <- execute_ conn "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT NOT NULL)"
_ <- execute conn "INSERT INTO users (name) VALUES (?)" (Only ("Ana" :: String)) -- parameters prevent SQL injection
users <- query conn "SELECT id, name FROM users WHERE name LIKE ?" (Only ("A%" :: String)) :: IO [User]
mapM_ print users
withTransaction conn $ do
_ <- execute conn "UPDATE users SET name = ? WHERE id = ?" ("Ana B." :: String, 1 :: Int)
pure ()
close conn