Registry / database / co-pg
library2.0.2jsnpmunverified

A wrapper for node-postgres (pg) that provides promise-based methods (e.g., connectPromise, queryPromise) compatible with the co generator-based control flow library and async/await. Current version 2.0.2, last updated in 2022. It allows using PostgreSQL connections and queries with yield or await instead of callbacks, without altering the original pg API. Supports pg, pg-native, and pg-pure backends. The library is in maintenance mode; newer projects should prefer native async/await with pg directly.

npm install co-pg
INSTALL
IMPORT
SIG · CO-PG
C
co-pg
databasejavascriptv2.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

co-pg
✓ const pg = require('co-pg')(require('pg'));
✗ const pg = require('co-pg');
CommonJS only. The library must be called as a function with the pg module as argument. Not available as ESM import.
Client
✓ const client = new pg.Client(connectionString); yield client.connectPromise();
✗ const { Client } = require('co-pg')(require('pg'));
Access Client via the wrapped pg object, not destructured from the wrapper directly.
queryPromise
✓ yield client.queryPromise('SELECT NOW()');
✗ yield client.query('SELECT NOW()');
query is the original callback-based method; queryPromise returns a promise for use with co or async/await.

Demonstrates connecting to PostgreSQL using co-pg with co, querying, and closing connection.

const co = require('co'); const pg = require('co-pg')(require('pg')); co(function*() { const client = new pg.Client(process.env.DATABASE_URL ?? 'postgres://postgres:1234@localhost/postgres'); yield client.connectPromise(); const result = yield client.queryPromise('SELECT NOW() as now'); console.log(result.rows[0].now); client.end(); }).catch(err => console.error(err));
Debug
Known issues
deprecatedco-pg is not actively maintained; favors callback-based pg patterns converted to promises. For new projects, use pg's built-in async/await support with pg.Pool.
fix
Replace co-pg with native pg promises: const { Pool } = require('pg'); const pool = new Pool(); await pool.query(...);
affects: >=2.0.0
gotchaThe library requires an older version of co (4.x) and may not work with modern async/await without co.
fix
Upgrade to 2.0.0 or later, or use async/await directly with co-pg (though co itself is outdated).
affects: <2.0.0
gotchaco-pg wraps pg, but does not automatically handle connection pooling; you must explicitly call done() after using a pooled client.
fix
Always call the done function returned from connectPromise after finishing with the client.
affects: >=0.0.0
breakingVersion 2.0.0 changed the API to be a function that takes the pg module, instead of direct require. Old code like require('co-pg').Client will break.
fix
Use const pg = require('co-pg')(require('pg')); then access pg.Client.
affects: >=2.0.0
Errors
Common errors & fixes
Error: Cannot find module 'co-pg'
Package not installed or missing from package.json.
fix
Run 'npm install co-pg'
TypeError: require(...) is not a function
Calling require('co-pg') without passing the pg module; in v2, co-pg exports a function that must be called.
fix
Use const pg = require('co-pg')(require('pg'));
TypeError: client.queryPromise is not a function
Client was not created from the wrapped pg object, or the wrapper was not correctly initialized.
fix
Ensure pg = require('co-pg')(require('pg')) and use new pg.Client(...).
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
pgrequiredPeer dependency: wraps pg API with promise helpers. Must be provided by user.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
packageco-pg ↗
co-pg — npm install co-pg · libregistry