Registry / database / koa-mysql

koa-mysql

JSON →
library1.0.3jsnpmunverified

Koa 1.x wrapper for the mysql npm package (felixge/node-mysql). Current stable version is 1.0.3, last published in 2016. Designed for Koa 1 with generator-based middleware, using co under the hood to yield queries. Not compatible with Koa 2 async/await or modern ESM patterns. Alternatives include mysql2 with promise support or direct use of mysql driver.

npm install koa-mysql
INSTALL
IMPORT
SIG · KOA-MYSQL
K
koa-mysql
databasejavascriptv1.0.3
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
var mysql = require('koa-mysql')
import mysql from 'koa-mysql'
CommonJS only; no ESM support. Package does not export a default export for ES modules.
createPool
var db = mysql.createPool({...})
var db = new mysql.Pool({...})
createPool is a static method on the default export, not a constructor.
query yields rows
var rows = yield db.query('...', params)
var rows = await db.query('...', params)
Uses Koa 1 generators with co; async/await is not supported.

Shows Koa 1 generator middleware, creating a MySQL pool, yielding a query, and returning the result.

var koa = require('koa'); var mysql = require('koa-mysql'); var db = mysql.createPool({ user: 'root', password: '', host: 'localhost', database: 'test' }); var app = koa(); app.use(function* () { try { var rows = yield db.query('SELECT ? + ? AS sum', [1, 2]); this.body = { sum: rows[0].sum }; } catch (err) { this.status = 500; this.body = { error: err.message }; } }); app.listen(3000);
Debug
Known issues
deprecatedkoa-mysql is unmaintained and only supports Koa 1 with generators. It uses the mysql package which has been superseded by mysql2.
fix
Migrate to mysql2 with promise support and Koa 2 async middleware.
affects: <=1.0.3
Errors
Common errors & fixes
SyntaxError: Unexpected token import
Trying to use ES import syntax with a CommonJS-only package.
fix
Use require('koa-mysql') instead of import.
TypeError: db.query is not a function
Using async/await instead of yield with generator function.
fix
Use function* and yield, or switch to Koa 2 and mysql2.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
mysqlrequiredpeer dependency for MySQL driver
Agent activity
5 hits · last 30 days
node
4
Resources
koa-mysql — npm install koa-mysql · libregistry