servant-db-postgresql: Derive a postgres client to database API specified by servant-db

[ bsd3, database, library ] [ Propose Tags ]

[Skip to Readme]
Versions [RSS] [faq],,,
Change log
Dependencies base (>=4.7 && <5), bytestring (==0.10.*), containers (==0.5.*), monad-logger (==0.3.*), postgresql-query (>=3.0 && <3.2), postgresql-simple (==0.5.*), servant (==0.9.*), servant-db (==0.2.*), servant-db-postgresql, text (==1.2.*), time, transformers-base (==0.4.*) [details]
License BSD-3-Clause
Copyright 2016 (c) Anton Gushcha
Author Anton Gushcha
Category Database
Home page
Source repo head: git clone
Uploaded by NCrashed at 2017-03-15T11:34:54Z
Distributions NixOS:
Executables servant-db-postgresql-example01
Downloads 2338 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2017-03-15 [all 1 reports]



Manual Flags

Automatic Flags

Enables compilation of examples


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Maintainer's Corner

For package maintainers and hackage trustees


Readme for servant-db-postgresql-

[back to package description]


Automatic derive of typed DB API based on postgresql-query package.

How to use:

  • Define your instances of MonadLogger and HasPostgres for your app monad:
newtype PostgresM a = PostgresM { runPostgresM :: PgMonadT (LoggingT IO) a }
  deriving (Functor, HasPostgres, MonadLogger, Monad, Applicative, MonadBase IO)
  • Define type level API:
type UserId = Int

data RegisterUser = RegisterUser {
  userRegName     :: String
, userRegPassword :: String
, userRegRegTime  :: Day
} deriving (Eq)

deriveToRow ''RegisterUser

data User = User {
  userId       :: UserId
, userName     :: String
, userPassword :: String
, userRegTime  :: Day
} deriving (Eq)

deriveFromRow ''User
deriveToRow ''User

type UserAPI =
       ArgNamed "u" (Composite RegisterUser)
    :> Procedure "postUser" (Only Int)
  :<|> ArgPos Int
    :> Procedure "getUser" (Maybe User)
  :<|> ArgPos Int
    :> Procedure "deleteUser" ()
  :<|> Procedure "getUsers" [User]
  • Derive client functions from the API:
postUser :: Composite RegisterUser -> PostgresM (Only Int)
getUser :: Int -> PostgresM (Maybe User)
deleteUser :: Int -> PostgresM ()
getUsers :: PostgresM [User]
(      postUser
  :<|> getUser
  :<|> deleteUser
  :<|> getUsers) = deriveDB (Proxy :: Proxy UserAPI) (Proxy :: Proxy PostgresM)
  • Use them. The full example you can view at example01/Main.hs module. And to compile it run the:
stack build --flag servant-db-postgresql:examples


  • Call functions in schema with :> operator:
type API = "test" :> ArgPos Int :> Procedure "square" (Only Int)

That will call function test.square(int).

  • Composite types are defined with Composite a wrapper:
type API = ArgNamed "u" (Composite UserCreate) :> Procedure "postUser" (Only Int)
  • Support for arrays:
type API = ArgPos (PGArray Int) :> Procedure "mleast" (Maybe (Only Int))
  • Support for variadic arguments:
type API = ArgPos (Variadic Int) :> Procedure "mleast" (Maybe (Only Int))
  • Support for default arguments with newypte Default a = Default (Maybe a):
type API = ArgPos (Default Int) :> Procedure "foo" (Only Int)