Control.Monad.Postgres
- Package
- purescript-postgresql
- Repository
- cakekindel/purescript-postgresql
Re-exports from Control.Monad.Postgres.Base
#PostgresT Source
type PostgresT :: (Type -> Type) -> Type -> Typetype PostgresT = RE Pool
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
Instances
(MonadAff m, MonadSession (SessionT m), MonadCursor (CursorT t (SessionT m)) t) => MonadPostgres (PostgresT m) (SessionT m) (CursorT ct (SessionT m)) ct
#MonadPostgres Source
class MonadPostgres :: (Type -> Type) -> (Type -> Type) -> (Type -> Type) -> Type -> Constraintclass (Monad m, MonadSession session, MonadCursor cursor ct) <= MonadPostgres m session cursor ct | m -> ct cursor session where
Typeclass generalizing PostgresT. Allows for dependency-injecting different
implementations of the idea of a postgres connection.
session- Session monad (forPostgresTthis isSessionT)cursor- Cursor session monad (forPostgresTthis isCursorT)ct- Open type parameter for cursor type. Don't pin this to a concrete type.
Members
session :: session ~> mtransaction :: session ~> mRun a session in
m, wrapped in a transaction.If any errors are raised, the transaction is rolled back and the error rethrown.
cursorWith :: forall q. AsQuery q => (Array Raw -> RepT ct) -> String -> q -> cursor ~> mcursor, but using a custom deserialize function for the data yielded by the cursor
Instances
(MonadAff m, MonadSession (SessionT m), MonadCursor (CursorT t (SessionT m)) t) => MonadPostgres (PostgresT m) (SessionT m) (CursorT ct (SessionT m)) ct
#runPostgres Source
runPostgres :: forall m a missing trash r f. MonadBracket Error f m => MonadAff m => Union r missing (Config trash) => Record r -> PostgresT m a -> Except m aCreate a new connection pool from the provided config and execute
the postgres monad, invoking Effect.Aff.Postgres.Pool.end afterwards.
#cursor Source
cursor :: forall @cursort t session cursor q a. MonadPostgres t session cursor cursort => AsQuery q => FromRow cursort => String -> q -> cursor a -> t aCreate 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.Cursor
#CursorT Source
newtype CursorT :: forall k. Type -> (k -> Type) -> k -> Typenewtype CursorT t m a
Constructors
Instances
Newtype (CursorT t m a) _(Functor m) => Functor (CursorT t m)(Apply m) => Apply (CursorT t m)(Applicative m) => Applicative (CursorT t m)(Plus m) => Plus (CursorT t m)(Alt m) => Alt (CursorT t m)(Alternative m) => Alternative (CursorT t m)(Bind m) => Bind (CursorT t m)(Monad m) => Monad (CursorT t m)(MonadEffect m) => MonadEffect (CursorT t m)(MonadAff m) => MonadAff (CursorT t m)(MonadRec m) => MonadRec (CursorT t m)(MonadUnliftEffect m) => MonadUnliftEffect (CursorT t m)(MonadUnliftAff m) => MonadUnliftAff (CursorT t m)MonadTrans (CursorT t)(MonadThrow e m) => MonadThrow e (CursorT t m)(MonadError e m) => MonadError e (CursorT t m)(MonadFork f m) => MonadFork f (CursorT t m)(Monad m, MonadKill e f m) => MonadKill e f (CursorT t m)(Monad m, MonadBracket e f (ReaderT String m), MonadBracket e f m) => MonadBracket e f (CursorT t m)(Monad m) => MonadAsk (Tuple String (Array Raw -> RepT t)) (CursorT t m)(Monad m) => MonadReader (Tuple String (Array Raw -> RepT t)) (CursorT t m)(Apply m, Apply p, Parallel p m) => Parallel (CursorT t p) (CursorT t m)(MonadSession m) => MonadCursor (CursorT t m) t(MonadSession m) => MonadSession (CursorT t m)
#MonadCursor Source
class MonadCursor :: (Type -> Type) -> Type -> Constraintclass (MonadSession m) <= 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
move :: Move -> m IntChange the cursor's position without fetching any data, returning the number of rows skipped.
Instances
(MonadSession m) => MonadCursor (CursorT t m) t
#fetchOne Source
fetchOne :: forall m t. MonadCursor m t => m (Maybe t)Fetch the next row from the cursor
Re-exports from Control.Monad.Postgres.Session
#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 rExecutes a query and unmarshals the result into
rexec :: forall q. AsQuery q => q -> m IntExecutes a query and returns the number of rows affected
exec_ :: forall q. AsQuery q => q -> m UnitExecutes a query and discards the result
streamIn :: String -> m (Writable ())Execute a query with a
Writablestream toSTDINUse with
COPY .. FROMlike so:w <- streamIn "COPY foo FROM STDIN WITH (FORMAT CSV, HEADER true)" liftEffect $ Stream.writeString "bar\n\"my bar column\"" UTF8 wstreamOut :: String -> m (Readable ())Execute a query with a
Readablestream fromSTDOUTUse with
COPY .. TOlike so:r <- streamIn "COPY foo TO STDIN WITH (FORMAT CSV, HEADER true)" liftEffect $ Stream.readString r -- "bar\n\"my bar column\""
Instances
(MonadStartSession s, MonadAff m) => MonadSession (RE s m)
#MonadStartSession Source
class MonadStartSession a whereMembers
startSession :: a -> Except Aff ClientendSession :: a -> Client -> Except Effect Unit
Instances
#handleStream Source
handleStream :: forall e m r. MonadEffect m => MonadError e m => Effect Unit -> m (Stream r) -> m (Stream r)- Modules
- Control.
Monad. Postgres - Control.
Monad. Postgres. Base - Control.
Monad. Postgres. Cursor - Control.
Monad. Postgres. Session - Data.
Postgres - Data.
Postgres. Custom - Data.
Postgres. Custom. Enum - Data.
Postgres. Interval - Data.
Postgres. Query - Data.
Postgres. Query. Builder - Data.
Postgres. Range - Data.
Postgres. Raw - Data.
Postgres. Result - Data.
Postgres. Unresult - Effect.
Aff. Postgres. Client - Effect.
Aff. Postgres. Pool - Effect.
Postgres. Client - Effect.
Postgres. Error - Effect.
Postgres. Error. Common - Effect.
Postgres. Error. Except - Effect.
Postgres. Error. RE - Effect.
Postgres. Pool - Node.
FS. PinnedVersion - Pipes.
Postgres
Run a session in
m.