pg-pool-adapter
v1.0.1
Published
Use knex pool with libraries or frameworks that expect a pg.Pool interface.
Downloads
129
Readme
pg-pool-adapter
pg-pool-adapter
is an adapter for integrating Knex with libraries or frameworks that expect a pg.Pool interface.
For instance, you can integrate LangGraph's PostgresSaver with an existing codebase that uses Knex.
Using this adapter, you can avoid having two separate pools in the same application, allowing it to manage connections more efficiently.
Summary
- Allows Knex's tarn-based pool to behave like a
pg.Pool
instance. - Adheres strictly to promises, avoiding callback-style APIs.
- Provides errors for unsupported operations.
Installation
Install the required packages:
npm install knex pg pg-pool-adapter
Add pg-pool-adapter
to your project and use it like this:
import { PgPoolAdapter } from 'pg-pool-adapter'
import knex from 'knex'
const knexInstance = knex({
client: 'pg',
connection: process.env.DATABASE_URL,
pool: { min: 2, max: 10 },
})
// Create an adapter instance
const pgPool = PgPoolAdapter.fromKnex(knexInstance)
// Use pgPool as you would normally use pg.Pool
pgPool.query('SELECT * FROM users').then((result) => console.log(result.rows))
Supported Features
- Execute queries via
pool.query()
- Acquire clients via
pool.connect()
- Release acquired clients via
client.release()
Unsupported Features
- The
query
method does not support callback-style arguments. Use promises instead. - Releasing a client with the
destroy
flag will throw an error. - The adapter does not support ending the pool directly. You must end the original Knex pool if necessary.
- Event listeners are currently not supported.