Registry / database / knex-serverless-mysql

knex-serverless-mysql

JSON →
library1.1.0jsnpmunverified

Knex.js dialect adapter for serverless-mysql, enabling persistent database connections across AWS Lambda execution contexts. Version 1.1.0 is the latest stable release, with no frequent updates since 2021. It works as a custom Knex client dialect, wrapping the serverless-mysql connection pool, which maintains MySQL connections across cold starts. Unlike native Knex MySQL drivers, this reduces database load in serverless environments by reusing connections. Minimal overhead, but requires manual setup of the serverless-mysql instance and injection as the dialect along with the mysql option.

npm install knex-serverless-mysql
INSTALL
IMPORT
SIG · KNEX-SERVERLESS-MY
K
knex-serverless-mysql
databasejavascriptv1.1.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
import knexServerlessMysql from 'knex-serverless-mysql'
const knexServerlessMysql = require('knex-serverless-mysql')
ESM import works; the package has no types – use @types/serverless-mysql for typings
Knex
import Knex from 'knex'
const Knex = require('knex')
In ESM, Knex has a default export; CJS users use require('knex')
serverlessMysql
import serverlessMysql from 'serverless-mysql'
const serverlessMysql = require('serverless-mysql')
serverless-mysql is CJS, so require works; ESM imports default

Initializes Knex with the serverless-mysql custom dialect and runs a simple query.

import Knex from 'knex'; import knexServerlessMysql from 'knex-serverless-mysql'; import serverlessMysql from 'serverless-mysql'; const mysql = serverlessMysql({ config: { host: process.env.DB_HOST ?? '', database: process.env.DB_DATABASE ?? '', user: process.env.DB_USERNAME ?? '', password: process.env.DB_PASSWORD ?? '', }, }); const knex = Knex({ client: knexServerlessMysql, mysql, }); export async function query() { return knex('users').where('id', 1); }
Debug
Known issues
breakingThe 'mysql' option in Knex config must be a serverless-mysql instance, not a standard mysql connection
fix
Ensure you pass the serverless-mysql object as the 'mysql' property; standard mysql2 won't work
affects: >=1.0.0
gotchaThe module does not ship TypeScript definitions; you must provide your own or ignore types
fix
Add a declaration file or use `@ts-ignore` when importing the dialect
affects: >=1.0.0
deprecatedserverless-mysql itself is no longer actively maintained as of 2022
fix
Consider migrating to mysql2 with a connection pool manager like `lambda-mysql-connector`
affects: >=0.0.0
gotchaThe dialect requires calling `knex.destroy()` only when the Lambda cold start is about to end; otherwise connections may leak
fix
Use `knex.destroy()` in a handler callback or after the Lambda has warmed up, not per invocation
affects: >=1.0.0
gotchaMigrations and seeds may not work with this dialect; they expect a standard Knex client
fix
Use standard Knex with mysql2 for migrations, and only use this dialect for runtime queries
affects: >=1.0.0
Errors
Common errors & fixes
Error: Knex: Unknown dialect 'undefined'
The 'client' option is not set to the knexServerlessMysql function; likely imported incorrectly
fix
Use `client: knexServerlessMysql` instead of `client: 'serverless-mysql'`
TypeError: Cannot read property 'connection' of undefined
The 'mysql' option is missing from Knex config or passed incorrectly
fix
Include `mysql: yourServerlessMysqlInstance` in the Knex configuration object
Error: knex: Required configuration option 'client' is missing
Using a CommonJS require of the dialect without a default export wrapper
fix
Use `const knexServerlessMysql = require('knex-serverless-mysql').default` or import the default export
Warning: Connection timeout after 10000ms
serverless-mysql connection is not properly configured or DB unreachable inside Lambda VPC
fix
Check environment variables, VPC configuration, and ensure DB allows Lambda IP ranges
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
knexrequiredPeer dependency for the Knex query builder; version >=0.11.4
serverless-mysqlrequiredPeer dependency; provides the actual MySQL connection management for Lambda
Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
knex-serverless-mysql — npm install knex-serverless-mysql · libregistry