-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters