generated from tpl/purs
Add documentation comments
This commit is contained in:
parent
583cea18d5
commit
370f8ca6b7
@ -36,6 +36,7 @@ import Prelude
|
|||||||
|
|
||||||
foreign import data POSTGRESQL :: !
|
foreign import data POSTGRESQL :: !
|
||||||
|
|
||||||
|
-- | PostgreSQL connection pool configuration.
|
||||||
type PoolConfiguration =
|
type PoolConfiguration =
|
||||||
{ user :: String
|
{ user :: String
|
||||||
, password :: String
|
, password :: String
|
||||||
@ -46,23 +47,30 @@ type PoolConfiguration =
|
|||||||
, idleTimeoutMillis :: Int
|
, idleTimeoutMillis :: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- | PostgreSQL connection pool.
|
||||||
foreign import data Pool :: *
|
foreign import data Pool :: *
|
||||||
|
|
||||||
|
-- | PostgreSQL connection.
|
||||||
foreign import data Connection :: *
|
foreign import data Connection :: *
|
||||||
|
|
||||||
|
-- | PostgreSQL query with parameter (`$1`, `$2`, …) and return types.
|
||||||
newtype Query i o = Query String
|
newtype Query i o = Query String
|
||||||
|
|
||||||
derive instance newtypeQuery :: Newtype (Query i o) _
|
derive instance newtypeQuery :: Newtype (Query i o) _
|
||||||
|
|
||||||
|
-- | Convert things to SQL rows.
|
||||||
class ToSQLRow a where
|
class ToSQLRow a where
|
||||||
toSQLRow :: a -> Array Foreign
|
toSQLRow :: a -> Array Foreign
|
||||||
|
|
||||||
|
-- | Convert things from SQL rows.
|
||||||
class FromSQLRow a where
|
class FromSQLRow a where
|
||||||
fromSQLRow :: Array Foreign -> Maybe a
|
fromSQLRow :: Array Foreign -> Maybe a
|
||||||
|
|
||||||
|
-- | Convert things to SQL values.
|
||||||
class ToSQLValue a where
|
class ToSQLValue a where
|
||||||
toSQLValue :: a -> Foreign
|
toSQLValue :: a -> Foreign
|
||||||
|
|
||||||
|
-- | Convert things from SQL values.
|
||||||
class FromSQLValue a where
|
class FromSQLValue a where
|
||||||
fromSQLValue :: Foreign -> Maybe a
|
fromSQLValue :: Foreign -> Maybe a
|
||||||
|
|
||||||
@ -126,17 +134,24 @@ instance fromSQLValueArray :: (FromSQLValue a) => FromSQLValue (Array a) where
|
|||||||
instance fromSQLValueList :: (FromSQLValue a) => FromSQLValue (List a) where
|
instance fromSQLValueList :: (FromSQLValue a) => FromSQLValue (List a) where
|
||||||
fromSQLValue = map List.fromFoldable <<< traverse fromSQLValue <=< fromRight <<< runExcept <<< readArray
|
fromSQLValue = map List.fromFoldable <<< traverse fromSQLValue <=< fromRight <<< runExcept <<< readArray
|
||||||
|
|
||||||
|
-- | Create a new connection pool.
|
||||||
foreign import newPool
|
foreign import newPool
|
||||||
:: ∀ eff
|
:: ∀ eff
|
||||||
. PoolConfiguration
|
. PoolConfiguration
|
||||||
-> Aff (postgreSQL :: POSTGRESQL | eff) Pool
|
-> Aff (postgreSQL :: POSTGRESQL | eff) Pool
|
||||||
|
|
||||||
|
-- | Run an action with a connection. The connection is released to the pool
|
||||||
|
-- | when the action returns.
|
||||||
foreign import withConnection
|
foreign import withConnection
|
||||||
:: ∀ eff a
|
:: ∀ eff a
|
||||||
. Pool
|
. Pool
|
||||||
-> (Connection -> Aff (postgreSQL :: POSTGRESQL | eff) a)
|
-> (Connection -> Aff (postgreSQL :: POSTGRESQL | eff) a)
|
||||||
-> Aff (postgreSQL :: POSTGRESQL | eff) a
|
-> Aff (postgreSQL :: POSTGRESQL | eff) a
|
||||||
|
|
||||||
|
-- | Run an action within a transaction. The transaction is committed if the
|
||||||
|
-- | action returns, and rolled back when the action throws. If you want to
|
||||||
|
-- | change the transaction mode, issue a separate `SET TRANSACTION` statement
|
||||||
|
-- | within the transaction.
|
||||||
withTransaction
|
withTransaction
|
||||||
:: ∀ eff a
|
:: ∀ eff a
|
||||||
. Connection
|
. Connection
|
||||||
@ -148,6 +163,7 @@ withTransaction conn action =
|
|||||||
Right a -> execute conn (Query "COMMIT TRANSACTION") unit $> a
|
Right a -> execute conn (Query "COMMIT TRANSACTION") unit $> a
|
||||||
Left e -> execute conn (Query "ROLLBACK TRANSACTION") unit *> throwError e
|
Left e -> execute conn (Query "ROLLBACK TRANSACTION") unit *> throwError e
|
||||||
|
|
||||||
|
-- | Execute a PostgreSQL query and discard its results.
|
||||||
execute
|
execute
|
||||||
:: ∀ i o eff
|
:: ∀ i o eff
|
||||||
. (ToSQLRow i)
|
. (ToSQLRow i)
|
||||||
@ -158,6 +174,7 @@ execute
|
|||||||
execute conn (Query sql) values =
|
execute conn (Query sql) values =
|
||||||
void $ _query conn sql (toSQLRow values)
|
void $ _query conn sql (toSQLRow values)
|
||||||
|
|
||||||
|
-- | Execute a PostgreSQL query and return its results.
|
||||||
query
|
query
|
||||||
:: ∀ i o eff
|
:: ∀ i o eff
|
||||||
. (ToSQLRow i, FromSQLRow o)
|
. (ToSQLRow i, FromSQLRow o)
|
||||||
|
Loading…
Reference in New Issue
Block a user