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.
- Allows Knex's tarn-based pool to behave like a
pg.Poolinstance. - Adheres strictly to promises, avoiding callback-style APIs.
- Provides errors for unsupported operations.
Install the required packages:
npm install knex pg pg-pool-adapterAdd 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))- Execute queries via
pool.query() - Acquire clients via
pool.connect() - Release acquired clients via
client.release()
- The
querymethod does not support callback-style arguments. Use promises instead. - Releasing a client with the
destroyflag 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.