# pg-txclient

> Transactional pg-Client and connection helper

Latest version **0.2.0** (published 2022-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install pg-txclient
pnpm add pg-txclient
yarn add pg-txclient
bun add pg-txclient
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-01-28 |
| First published | 2020-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Caro Guerrero |
| Maintainers | elhoyos, caroguerrero, fasozu |

## Links

- npm: https://www.npmjs.com/package/pg-txclient
- Repository: https://github.com/alltherooms/pg-txclient
- Homepage: https://github.com/alltherooms/pg-txclient#readme
- Issues: https://github.com/alltherooms/pg-txclient/issues
- npm.io page: https://npm.io/package/pg-txclient

## Dependencies (1)

- [pg](https://npm.io/package/pg.md) ^8.7.1

## Recent versions

- 0.2.0 (latest) — 2022-01-28
- 0.1.0 — 2020-01-23
- 0.0.9 — 2020-01-20
- 0.0.8 — 2020-01-20
- 0.0.7 — 2020-01-20
- 0.0.6 — 2020-01-20
- 0.0.5 — 2020-01-17
- 0.0.4 — 2020-01-17
- 0.0.3 — 2020-01-16
- 0.0.2 — 2020-01-16
- 0.0.1 — 2020-01-16

## README

# pg-txclient

A pg-`Client` on steroids. Transactions and connection helper.

## Installation

```
npm install pg-txclient
```

## Usage examples

```ts
import { NamedPoolConfig, connect } from 'pg-txclient';

const config: NamedPoolConfig = {
  name: 'data-postgres',
  host,
  ...
};

const dataConn = await connect(config);
try {
  const user = await dataConn.query(`SELECT name FROM users LIMIT 1`);
  // do something with the user
} finally {
  // always release the connection to the pool
  dataConn.release();
}
```

All queries that run under `dataConn` will be wrapped in a single transaction. Queries are commited automatically.

## No Autocommit

You can connect to manage the commit and rollback yourself. Here's an example:

```ts
const dataConn = await connect(config, false);
try {
  const user = await dataConn.query(`SELECT 1 FROM users WHERE name = 'Sonya' LIMIT 1`);
  if (isUser(user)) {
    await dataConn.query(`INSERT INTO users VALUES ('Sonya')`);
    await dataConn.commit();
  }
} finally {
  // always rollback mid-air operations
  await dataConn.rollback();
  dataConn.release();
}
```

## Validate your configurations

This is useful to check your connection configurations before connecting.

```ts
import { NamedPoolConfig, connect } from 'pg-txclient';

const validations = ({ host }) => {
  if (process.env.NODE_ENV !=== 'production' && isProductionHost(host)) {
    throw new Error('Watch out! Your trying to connect to a production host');
  }
}

const config: NamedPoolConfig = {
  name: 'data-postgres',
  host,
  ...
  validations,
};

const dataConn = await connect(config); // this will throw before connecting!
```

---
_Source: https://npm.io/package/pg-txclient · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
