Registry / database / maranda-koa2-session-mysql

maranda-koa2-session-mysql

JSON →
library3.1.9jsnpmunverified

A Koa2 session middleware backed by MySQL, designed for TypeScript projects. Version 3.1.9 provides session management with automatic garbage collection, custom expiry, and typed session data via generics. Unlike generic session stores, it integrates directly with Sequelize ORM and supports both auto and manual GC modes. The library is suitable for Koa2 applications requiring persistent sessions with MySQL storage, and is actively maintained with a focus on TypeScript support.

npm install maranda-koa2-session-mysql
INSTALL
IMPORT
SIG · MARANDA-KOA2-SESSI
M
maranda-koa2-session-mysql
databasejavascriptv3.1.9
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 Session, { Sequelize } from 'maranda-koa2-session-mysql'
const Session = require('maranda-koa2-session-mysql')
ESM-only package; use named import for Sequelize class.
Session.Ctx
import Session from 'maranda-koa2-session-mysql'; type Ctx = Session.Ctx<SessionData>
import { Ctx } from 'maranda-koa2-session-mysql'
Ctx is not exported directly; access via Session.Ctx generic.
Session.DataType
import Session from 'maranda-koa2-session-mysql'; interface SessionData extends Session.DataType { ... }
import { DataType } from 'maranda-koa2-session-mysql'
DataType is a namespace; extend it to define custom session data shape.

Setup Koa2 with MySQL session store, define typed session data, and use middware with garbage collection options.

import Koa from 'koa'; import Session, { Sequelize } from 'maranda-koa2-session-mysql'; interface SessionData extends Session.DataType { name: string; id: number; } const app = new Koa<any, Session.Ctx<SessionData>>(); const sequelize = new Sequelize('database', 'username', 'password', { dialect: 'mysql', host: 'localhost', port: 3306, }); app.use(Session.middware(sequelize, { gcOpts: { probDenominator: 100, probMolecular: 1, type: 'auto' }, defaultExpiry: 24 * 60 * 60 * 1000, })); app.use((ctx, next) => { if (ctx.path === '/login') { ctx.session.data.id = 123; ctx.session.expiry = 5 * 24 * 60 * 60 * 1000; ctx.body = 'Logged in'; } else { ctx.body = ctx.session.isNewRecord ? 'No session' : 'Session exists'; } return next(); }); app.listen(3000);
Debug
Known issues
breakingSession.middware is misspelled; correct is Session.middware with double 'd'.
fix
Use Session.middware (not Session.middleware) in your code.
affects: >=3.0.0
deprecatedThe old import pattern 'const Session = require("...")' does not work in ESM-only versions.
fix
Use import syntax: import Session from 'maranda-koa2-session-mysql'.
affects: >=3.0.0
gotchaSession data modifications after await next() will not be saved automatically; you must call ctx.session.save() and set cookies manually.
fix
Set session data before calling next(), or use manual save with ctx.session.save() after next and handle cookies yourself.
affects: >=1.0.0
gotchaThe table name defaults to 'sessions' and must not exist in the database schema before middleware initialization.
fix
Ensure no table named 'sessions' exists in your MySQL database, or specify a custom tableName in options.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'maranda-koa2-session-mysql'
Package not installed or import path incorrect.
fix
Run 'npm install maranda-koa2-session-mysql' and verify import: import Session from 'maranda-koa2-session-mysql'.
TypeError: session.save is not a function
You called ctx.session.save() but the session object does not have a save method; use automatic saves or call ctx.session.destroy() for deletion.
fix
Remove manual save calls; session data is automatically saved after the response unless using manual mode. For manual GC, set gcType: 'manual' and call session.save() after modification.
Upgrade
Version history
3.1.9latest on npm
Audit
Dependencies
sequelizerequiredORM for MySQL interactions; session data is stored using Sequelize models
mysql2optionalMySQL driver used by Sequelize
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
maranda-koa2-session-mysql — npm install maranda-koa2-session-mysql · libregistry