Skip to content

Commit

Permalink
chore: add README and LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
ppati000 committed Sep 21, 2023
1 parent 50ca316 commit 3d53b14
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Patrick Petrovic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# pg-bounceguard

`pg-bounceguard` is an npm module that acts as a wrapper around the `pg` module, allowing users to block or log Postgres
queries that may not work with [PGBouncer](https://github.com/pgbouncer/pgbouncer).

## Background

PGBouncer is a PostgreSQL connection pooler that usually runs with _transactional pooling_.
A client keeps its connection only for the duration of a transaction.
Hence, certain transaction-escaping statements can cause hard-to-debug issues.
For example, `CREATE TEMP TABLE` may unexpectedly leak the temporary table into subsequent transactions.
One of these transactions may then try to recreate the table even though it already exists.

`pg-bounceguard` allows you to wrap `pg` clients and pools to detect problematic queries.
Per default, problematic queries will throw an error.
You can also configure `pg-bounceguard` to log the queries instead.

> 🤓 **Note:** `pg-bounceguard` is a debugging tool. Recommended for use in development environments only.
## Features

- Blocks or logs Postgres queries that may cause issues with PGBouncer.
- Supports customization for handling specific statements using overrides.
- Offers the ability to sample queries and/or use a custom logging function.

## Installation

You can install the `pg-bounceguard` module via npm:

```bash
npm install pg-bounceguard
```

## Usage

Here's an example of how to use `pg-bounceguard` in your Node.js application:

```javascript
import { PGBounceGuard } from 'pg-bounce-guard';
import { Client } from 'pg';

const unwrapped = new Client({
// ... PostgreSQL connection details
});

const client = PGBounceGuard.wrap(unwrapped, {
action: 'error',
// ... other configurations
});

await client.connect();

// Usage example
await client.query('SELECT * FROM your_table'); // OK
await client.query('CREATE TEMP TABLE your_table (id INT)'); // Error!

await client.end();
```

## Configuration

### `PGBounceGuard.wrap`

- `action`: Action to take for queries that may not work with PGBouncer. Options: `'error'`, `'warn'`, or `'ignore'`.
Default: `'error'`.
- `overrides`: Override `action` for specific query types.
- `logFn`: Custom logging function if `'warn'` is used.
- `sampleRate`: Number between 0 and 1 that determines the share of queries that are checked. Defaults to 1 (all queries
are checked).

### Example Configuration

```javascript
// Example of custom configuration
const client = PGBounceGuard.wrap(unwrapped, {
action: 'error',
overrides: {
createTempTable: 'ignore',
set: 'warn',
},
logFn: (err) => {
// Custom logging logic
},
});
```

## Testing

To run integration tests (requires Docker):

```bash
npm run test:docker
```

Alternatively, you can spin up your own Postgres server on port 5432. Then, run `npm run test`.

## Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

## License

This project is licensed under the [MIT License](LICENSE).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "pg-bounceguard",
"private": true,
"version": "1.0.0",
"description": "Debug tool for pg (node-postgres) that blocks/logs queries that are incompatible with PgBouncer's transaction pooling mode.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "https://github.com/ppati000/pg-bounceguard.git",
"engines": {
"node": ">=18.0.0"
},
Expand Down

0 comments on commit 3d53b14

Please sign in to comment.