A middleware that handles DB access. Each request gets its own pool client, so there is isolation between requests and their transactions.
We are also able to handle exceptions gracefully, releasing the client from the pool in an event of one.
Can be used with any resolver function, like http listener or queue worker.
Supports multiple pools, and would create connections to all of them simultaneously.
const pool = new Pool({ connectionString: '...' });
const pg = new PgService(pool);
const withDb = pgMiddleware(pg);
new HttpService({ listener: withDb(async ({ db }) => {
* console.log(await db.query('...'));
}) });
// Multiple
// ----------------
const pg1 = new PgService(new Pool({ connectionString: '...' }));
const pg2 = new PgService(new Pool({ connectionString: '...' }));
const withDbs = pgMiddleware({ db1: pg1, db2: pg2 });
new HttpService({ listener: withDb(async ({ db1, db2 }) => {
console.log(await db1.query('...'));
console.log(await db2.query('...'));
}) });
Generated using TypeDoc
Context with one or more database clients connected.
By default client is
db
.You can define multiple client by using an object with
PgService
properties.