Registry / database / co-mysql

co-mysql

JSON →
library1.0.0jsnpmunverified

A MySQL wrapper for co or Koa that adapts the mysql library to work with generators and promises. Version 1.0.0 is the final release; the package is in maintenance mode with no active development. It wraps a mysql connection pool (or connection) to return promise-based query results, enabling use with co or async/await. Compared to directly using thunkify or promisify on mysql, co-mysql provides a simpler wrapper, but the author now recommends using thunkify or promisify directly. It has a small user base and is only relevant for legacy projects using co or Koa v1 generators.

npm install co-mysql
INSTALL
IMPORT
SIG · CO-MYSQL
C
co-mysql
databasejavascriptv1.0.0
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.

default (wrapper function)
const wrapper = require('co-mysql');
const wrapper = require('co-mysql').default;
co-mysql uses CommonJS module.exports. Default import via require is correct, not ES6 default import.
p.query()
const p = wrapper(pool); p.query('SELECT 1').then(...);
const p = wrapper(pool); p.query('SELECT 1', callback);
The returned wrapper object replaces the callback-based query with a promise-based one. Do not pass a callback.
Using with co
co(function*() { var rows = yield p.query('SELECT 1'); })();
co(function*() { var rows = yield pool.query('SELECT 1'); })();
The wrapper must be used; otherwise the raw pool.query does not return a thunk suitable for co.

Wraps a mysql pool and executes a query using co generator, demonstrating promise-based query result.

const mysql = require('mysql'); const wrapper = require('co-mysql'); const co = require('co'); const pool = mysql.createPool({ host: 'localhost', user: 'root', password: process.env.MYSQL_PASSWORD ?? '', database: 'test' }); const p = wrapper(pool); co(function* () { try { const rows = yield p.query('SELECT 1 + 1 AS result'); console.log(rows); // [{ result: 2 }] } catch (err) { console.error(err); } })();
Debug
Known issues
deprecatedco-mysql is deprecated in favor of thunkify or promisify to convert mysql to promise or thunk.
fix
Replace with thunkify(mysql.pool) or promisify(mysql.pool) from appropriate libraries.
affects: >=1.0.0
breakingco-mysql only works with Node.js > 0.11.13 due to generator support. Not compatible with modern Node versions without transpilation.
fix
Use async/await with a promisified mysql library instead of generators.
affects: >=1.0.0
gotchaThe wrapper only works with mysql pools or connections created via mysql.createPool or mysql.createConnection. It does not accept a raw mysql instance.
fix
Always create a pool or connection first, then pass it to the wrapper.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: wrapper is not a function
Using ES6 import syntax `import wrapper from 'co-mysql'` which returns an object with default property.
fix
Use CommonJS require: `const wrapper = require('co-mysql');`
Warning: co-mysql is deprecated. Please use thunkify or promisify directly.
Package explicitly deprecates itself in documentation.
fix
Switch to using thunkify or promisify from libraries like `thunkify` or `util.promisify` on the mysql pool.
TypeError: pool.query is not a function
Incorrect usage: trying to yield on pool.query directly without wrapping.
fix
Wrap the pool with `const p = wrapper(pool);` and then yield `p.query(...)`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
mysqlrequiredPeer dependency required to create a connection pool. Must be installed separately.
Agent activity
19 hits · last 30 days
node
18
OpenAI (training)
1
Resources
co-mysql — npm install co-mysql · libregistry