Module

Control.Monad.Postgres

Package
purescript-postgresql
Repository
cakekindel/purescript-postgresql

Re-exports from Control.Monad.Postgres.Base

#PostgresT Source

newtype PostgresT :: forall k. (k -> Type) -> k -> Typenewtype PostgresT m a

Monad handling pool resource acquisition & release

runPostgres
  {connectionString: "postgresql://postgres:postgres@localhost:5432"}
  $ session do
      exec_ "create table foo (bar int);"
      exec_ "insert into foo values (1);"
      res <- query "select * from foo"
      pure $ res == 1

Is equivalent to:

do
  pool <- liftEffect $ Pool.make {connectionString: "postgresql://postgres:postgres@localhost:5432"}
  finally (Pool.end pool) do
    client <- Pool.connect pool
    finally (liftEffect $ Pool.release pool client) do
      Client.exec_ "create table foo (bar int);" client
      Client.exec_ "insert into foo values (1);" client
      res <- Client.query "select * from foo" client
      pure $ res == 1

Constructors

Instances

#withPool Source

withPool :: forall m a. PostgresT m a -> Pool -> m a

Execute a PostgresT using an existing connection pool.

This will not invoke Pool.end after executing.

#transaction Source

transaction :: forall m a. MonadBracket Error Fiber m => MonadAff m => MonadSession (SessionT m) => SessionT m a -> PostgresT m a

Lifts a session to PostgresT, running the session in a transaction.

If the session throws an error, the transaction will be rolled back and the error rethrown.

#session Source

session :: forall e f m a. MonadBracket e f m => MonadAff m => MonadSession (SessionT m) => SessionT m a -> PostgresT m a

Lifts a session to PostgresT, releasing the client to the pool after execution.

#runPostgres Source

runPostgres :: forall m a missing trash r e f. MonadBracket e f m => MonadAff m => Union r missing (Config trash) => Record r -> PostgresT m a -> m a

Create a new connection pool from the provided config and execute the postgres monad, invoking Effect.Aff.Postgres.Pool.end afterwards.

Re-exports from Control.Monad.Postgres.Cursor

#CursorT Source

newtype CursorT :: forall k. Type -> (k -> Type) -> k -> Typenewtype CursorT t m a

Constructors

Instances

#MonadCursor Source

class MonadCursor :: (Type -> Type) -> Type -> Constraintclass (MonadSession m, FromRow t) <= MonadCursor m t  where

A monad representing a handle to a server-side cursor

runPostgres {connectionString: "..."} do
  exec_ "create table foo (id int not null primary key);"
  exec_
    $ intercalate "\n "
      [ "insert into foo (id)"
      , "values"
      , intercalate ", "
          $ map (\n -> "(" <> show n <> ")")
          $ Array.range 1 100
      ]

  cursor @Int "foo_cursor" "select id from foo" do
    a <- fetchOne -- 1
    b <- fetchOne -- 2
    c <- fetchOne -- 3
    d <- fetch 10 -- 4..14
    e <- fetchAll -- 15..100
    pure unit

Members

  • fetch :: Int -> m (Array t)

    Fetch a specified number of rows from the cursor

  • fetchAll :: m (Array t)

    Fetch all remaining rows from the cursor

Instances

#fetchOne Source

fetchOne :: forall m t. MonadCursor m t => m (Maybe t)

Fetch the next row from the cursor

#cursor Source

cursor :: forall m @t a q. AsQuery q => MonadAff m => MonadBracket Error Fiber m => MonadSession (SessionT m) => String -> q -> CursorT t (SessionT m) a -> PostgresT m a

Create a server-side cursor for a query in a transaction, and execute a CursorT with a view to the new cursor.

Re-exports from Control.Monad.Postgres.Session

#SessionT Source

type SessionT :: forall k. (k -> Type) -> k -> Typetype SessionT = ReaderT Client

Instances

#MonadSession Source

class MonadSession :: (Type -> Type) -> Constraintclass (MonadAff m) <= MonadSession m  where

A monad representing a connected session to a database

Members

  • query :: forall q r. AsQuery q => FromRows r => q -> m r

    Executes a query and unmarshals the result into r

  • exec :: forall q. AsQuery q => q -> m Int

    Executes a query and returns the number of rows affected

  • exec_ :: forall q. AsQuery q => q -> m Unit

    Executes a query and discards the result

Instances