@axion-io/pg-adapter
v0.0.1
Published
Bridges node-postgres (pg) Pool/PoolClient to @axion-io/core SqlExecutor and TransactionRunner.
Readme
@axion-io/pg-adapter
Bridges node-postgres (pg) to the SqlExecutor and TransactionRunner ports in @axion-io/core.
Installation
npm install @axion-io/core @axion-io/pg-adapter pg
npm install --save-dev @types/pgUsage
Non-transactional queries
import pg from 'pg';
import { PgSqlAdapter } from '@axion-io/pg-adapter';
import { PostgresOperationManager } from '@axion-io/postgres';
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const client = await pool.connect();
const executor = new PgSqlAdapter(client);
const operationManager = new PostgresOperationManager(executor, txRunner);Transactional writes
import { PgTransactionRunner } from '@axion-io/pg-adapter';
const txRunner = new PgTransactionRunner(pool);
await txRunner.run(async (executor) => {
await outboxWriter.writeDomainEvent(event);
await timesheetRepo.save(timesheet);
});PgTransactionRunner acquires a connection from the pool, issues BEGIN/COMMIT/ROLLBACK, and releases the connection on completion or error.
Structural interfaces
The adapter is built against two minimal structural interfaces so it works with any pg-compatible client:
PgQueryable— satisfied bypg.PoolClientandpg.ClientPgPool— satisfied bypg.Pool
No nominal coupling to the pg package types is required in application code.
