_______ _______ _ _ _______ _______ _________
( ____ \( ___ )( ( /|( ( /|( ____ \( ____ \\__ __/
| ( \/| ( ) || \ ( || \ ( || ( \/| ( \/ ) (
| | | | | || \ | || \ | || (__ | | | |
| | | | | || (\ \) || (\ \) || __) | | | |
| | | | | || | \ || | \ || ( | | | |
| (____/\| (___) || ) \ || ) \ || (____/\| (____/\ | |
(_______/(_______)|/ )_)|/ )_)(_______/(_______/ )_(
_______ _______ _______ _ _________ _______ _ _________
( ____ )( ____ \( ____ \( \ \__ __/( ____ \( ( /|\__ __/
| ( )|| ( \/| ( \/| ( ) ( | ( \/| \ ( | ) (
_____ | (____)|| | | | | | | | | (__ | \ | | | |
(_____)| _____)| | ____ | | | | | | | __) | (\ \) | | |
| ( | | \_ )| | | | | | | ( | | \ | | |
| ) | (___) || (____/\| (____/\___) (___| (____/\| ) \ | | |
|/ (_______)(_______/(_______/\_______/(_______/|/ )_) )_(
This module is aimed at taking the pain away from managing your Postgres connections from within a connect/express
app. Too many times has res.redirect()
been used and left the DB connection hanging.
npm install connect-pgclient
connect-pgclient
helps you manage (and free) your Postgres database connections so you don't have to. It
automatically gets a client from node-pg
at the start of the request and calls done()
at the end of the
request to automatically return the client back to pg's pool. This way you'll never lose any clients by accidentally
not calling done
.
Example
var pgclient = ; var dbMiddleware = ; app;
What does this package solve?
If you are trying to do your Pg clients manually, then there are various cases which you might forget about where you
should call done()
. Here is an example when you have a client but call res.redirect()
and forget to release
it again:
app;
Using connect-pgclient
you'll be able to do this:
app;
Usage
Once you have setup and called your connect-pgclient
middleware, you Postgres client is available on the
req
object as follows:
// the node-pg clientreqdbclient // the done function which node-pg needs to return the client to the poolreqdbdone // boolean to show us whether we are in the middle of a transactionreqdbtransaction
In general, you should only ever use the req.db.client
property of req.db
.
Options
- config - the database connection params as defined in node-postgres
- transaction - (default: false) States whether to BEGIN and COMMIT a transaction for you.
- log - (default: no-op) a function to call with log messages to help with debugging (usually dev only)
Examples
Connect to DB for Every Request
You may or may not want to do this, but it shows a good example for starters:
var pgclient = ; app;
If you would like connect-pgclient
to BEGIN and COMMIT a transaction for you, then just pass the
transaction
param as true
into the options.
var pgclient = ; app;
Connect to the DB within Specific Routes
The database clients are released back to node-pg
even if the request ends in res.send()
,
res.redirect()
, res.json()
, res.render()
or even in error next(err)
.
For example:
// Postgres middleware which gets a Pg client and releases it after// the request has been fulfilled.var connectToDb = ; // Postgres middleware which gets a Pg client, starts a transaction// and commits and releases it after the request has been fulfilled.var connectToDbWithTransaction = ; // a route which always succeeds - Pg client is released okapp; // a route which redirects 50% of the time - Pg client is released okapp; // a route which dies 50% of the time - Pg client is released okapp;
How it Works
Using brianc's excellent pg library, we connect to the
database and store both the client
and the done
function onto the req
so that we can use the client
in our routes, but also automatically call done
when the request has finished.
connect-pgclient
works much like connect's session
middleware in that it wraps res.end()
so that we
can get control both before and after the request has been fulfilled, which allows us to give the client back to pg's
pool automatically no matter what happened during the request.
Caveat
When you use connect-pgclient
to give you a client and automatically start a transaction, if the request ends up
in error, the transaction still has COMMIT
performed. In this error case, I think ROLLBACK
should be called
instead but I'm not yet sure how to detect if the request is in the error state.
(Note: remember that this is after the request has been fulfilled, which is after any error middleware has been run.)
Author
Written by Andrew Chilton - Blog - Twitter.
License
(Ends)