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

PortabilityNot portable
Safe HaskellNone
LanguageHaskell2010

Servant.DB.PostgreSQL.HasDB

Contents

Description

 

Synopsis

Documentation

deriveDB Source #

Arguments

:: HasDB layout m 
=> Proxy layout

API layout

-> Proxy m

PostgreSQL monad we operate in

-> DB layout m

Derived functions

Derive DB client from API

class HasDB layout m where Source #

Derive DB client from API

Minimal complete definition

deriveDBWithCtx

Associated Types

type DB layout m :: * Source #

Associated type of deriving result

Methods

deriveDBWithCtx :: Proxy layout -> Proxy m -> QueryContext ToField -> DB layout m Source #

Derive DB client from API layout

Instances

(HasDB api1 m, HasDB api2 m) => HasDB ((:<|>) api1 api2) m Source #

Deriving several procedures to query DB API

type API = Procedure "time" Integer
  :<|> ArgNamed "a" Int :> Procedure "square" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

time :: MyMonad (Only Integer)
square :: Int -> MyMonad (Only Int)
(time, square) = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive separate endpoints with the following SQL calls:

>>> SELECT time();
>>> SELECT square("a" => ?);

Associated Types

type DB ((:<|>) api1 api2) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * (api1 :<|> api2) -> Proxy (* -> *) m -> QueryContext ToField -> DB (api1 :<|> api2) m Source #

(KnownSymbol n, FromRow a, MonadPostgres m) => HasDB (Procedure n a) m Source #

Deriving call to DB procedure with single result

type API = Arg "a" Int -> Procedure "squareReturning" (Int, Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

square :: Int -> MyMonad (Int, Int)
square = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM squareReturning() AS t;

The instance expects that return type of SQL stored function is a single row.

Associated Types

type DB (Procedure n a) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * (Procedure n a) -> Proxy (* -> *) m -> QueryContext ToField -> DB (Procedure n a) m Source #

(KnownSymbol n, FromRow a, MonadPostgres m) => HasDB (Procedure n (Maybe a)) m Source #

Deriving call to DB procedure with optional result

data User -- user data
instance FromRow User

type API = ArgPos Int :> Procedure "user" (Maybe User)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

getUsers :: MyMonad (Maybe User)
getUsers = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM user(?) AS t;

Associated Types

type DB (Procedure n (Maybe a)) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * (Procedure n (Maybe a)) -> Proxy (* -> *) m -> QueryContext ToField -> DB (Procedure n (Maybe a)) m Source #

(KnownSymbol n, FromRow a, MonadPostgres m) => HasDB (Procedure n [a]) m Source #

Deriving call to DB procedure with multiple result

data User -- user data
instance FromRow User

type API = Procedure "users" [User]

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

getUsers :: MyMonad [User]
getUsers = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM users() AS t;

And the instance expects that users function return type is `SETOF user`.

Associated Types

type DB (Procedure n [a]) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * (Procedure n [a]) -> Proxy (* -> *) m -> QueryContext ToField -> DB (Procedure n [a]) m Source #

(KnownSymbol n, MonadPostgres m) => HasDB (Procedure n ()) m Source #

Deriving call to DB procedure with no return type

data User -- user data
instance ToRow User

type API = Arg "user" User :> Procedure "registerUser" ()

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

getUsers :: User -> MyMonad ()
getUsers = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT registerUser("user" => ?);

And the instance expects that users function return type is `SETOF user`.

Associated Types

type DB (Procedure n ()) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * (Procedure n ()) -> Proxy (* -> *) m -> QueryContext ToField -> DB (Procedure n ()) m Source #

(ToField a, HasDB api m) => HasDB ((:>) * * (ArgPos a) api) m Source #

Deriving call to DB procedure with positional arguments

type API = Arg Int :> Arg Int :> Procedure "sum" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbSum :: Int -> Int -> MyMonad (Only Int)
dbSum = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM sum(?, ?) AS t;

Associated Types

type DB ((:>) * * (ArgPos a) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgPos a) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgPos a) api) m Source #

(ToField a, HasDB api m) => HasDB ((:>) * * (ArgPos (Variadic a)) api) m Source #

Deriving call to DB procedure with positional variadic arguments

type API = ArgPos (Variadic Int) :> Procedure "mleast" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbMleast :: Variadic Int -> MyMonad (Only Int)
dbMleast = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM mleast(VARIADIC ?) AS t;

Associated Types

type DB ((:>) * * (ArgPos (Variadic a)) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgPos (Variadic a)) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgPos (Variadic a)) api) m Source #

(ToField a, HasDB api m) => HasDB ((:>) * * (ArgPos (Default a)) api) m Source #

Deriving call to DB procedure with positional default arguments

type API = ArgPos (Default Int) :> Procedure "foo" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbFoo :: Default Int -> MyMonad (Only Int)
dbFoo = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM foo(DEFAULT) AS t;

Associated Types

type DB ((:>) * * (ArgPos (Default a)) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgPos (Default a)) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgPos (Default a)) api) m Source #

(KnownSymbol n, ToField a, HasDB api m) => HasDB ((:>) * * (ArgNamed n (Default a)) api) m Source #

Deriving call to DB procedure with named default arguments

type API = ArgNamed "a" (Defaultable Int) :> Procedure "foo" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbFoo :: Defaultable Int -> MyMonad (Only Int)
dbFoo = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM default(DEFAULT) AS t;

Associated Types

type DB ((:>) * * (ArgNamed n (Default a)) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgNamed n (Default a)) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgNamed n (Default a)) api) m Source #

(KnownSymbol n, ToField a, HasDB api m) => HasDB ((:>) * * (ArgNamed n (Variadic a)) api) m Source #

Deriving call to DB procedure with named variadic arguments

type API = ArgNamed "arr" (Variadic Int) :> Procedure "mleast" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbMleast :: Variadic Int -> MyMonad (Only Int)
dbMleast = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM mleast(VARIADIC "arr" => ?) AS t;

Associated Types

type DB ((:>) * * (ArgNamed n (Variadic a)) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgNamed n (Variadic a)) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgNamed n (Variadic a)) api) m Source #

(KnownSymbol n, ToField a, HasDB api m) => HasDB ((:>) * * (ArgNamed n a) api) m Source #

Deriving call to DB procedure with named arguments

type API = ArgNamed "a" Int :> ArgNamed "b" Int :> Procedure "sum" (Only Int)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

dbSum :: Int -> Int -> MyMonad (Only Int)
dbSum = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

Upper example will derive the following SQL call:

>>> SELECT * FROM sum("a" => ?, "b" => ?) AS t;

Associated Types

type DB ((:>) * * (ArgNamed n a) api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> *) (ArgNamed n a) api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> *) (ArgNamed n a) api) m Source #

(KnownSymbol n, HasDB api m) => HasDB ((:>) * Symbol n api) m Source #

Deriving several procedures to query DB API

type API = "public" :> Procedure "time" (Only Integer)

data MyMonad m a -- Your application monad with connection pool and logger
instance HasPostgres m
instance MonadLogger m

time :: MyMonad (Only Integer)
time = deriveDB (Proxy :: Proxy API) (Proxy :: Proxy MyMonad)

The time function will call DB with the:

>>> SELECT public.time();

Note that there could be only one schema marker. If there are more than one the later (righter) will override previous ones.

Associated Types

type DB ((:>) * Symbol n api) (m :: * -> *) :: * Source #

Methods

deriveDBWithCtx :: Proxy * ((* :> Symbol) n api) -> Proxy (* -> *) m -> QueryContext ToField -> DB ((* :> Symbol) n api) m Source #

Orphan instances

FromRow a => FromRow (Maybe a) Source # 

Methods

fromRow :: RowParser (Maybe a) #