pga
A convenience wrapper around the pg module's Pool object.
Installation
npm install --save pga
Usage
const pga = ; var db = ;
API
#close
Closes the database connection. An alias for Pool.end
.
db;
#parallel
Performs a series of parameterized queries in parallel over multiple connections from the underlying pool.
Query execution order is arbitrary, but the results are returned in the expected order.
If a single query results in an error, parallel
will immediately execute the specified callback function with both the error and potentially a partial array of results from other successful queries. When returning a Promise, one error among any of the queries will result in the Promise being rejected.
parallel
is provided as a means of efficiently performing multiple SELECT
queries at once rather than one after the other in sequence. Do not use parallel
to execute grouped SQL queries that may potentially alter the database- use transact
instead!
// Parallelized queries with a callback function.db; // Parallelized queries that return a Promise object.db;
pga
also accepts comma-separated queries and optional callback functions for parallel queries if passing in an array is too unwieldy:
db;
#query
Performs a single parameterized query. An alias for Pool.query
.
// A regular query with a callback function.db; // A regular query with a parameter and callback function.db; // A regular query that returns a Promise object.db; // A regular query with a parameter that returns a Promise object.db;
#transact
Performs a database transaction on an array of parameterized queries.
// A transaction with a callback function.db; // A transaction that returns a Promise object.db;
pga
also accepts comma-separated queries and optional callback functions for transactions if passing in an array is too unwieldy:
db;
Extras
- pga-sql for cleaner parameterized queries
- pga-pubsub for PostgreSQL LISTEN/NOTIFY