Home

Awesome

koa-generic-session-knex

npm version CircleCI

Store Koa sessions in a database using knex.

Usage

This session storage provider works with koa-generic-session (session middleware for Koa) and with koa-session-minimal (session middleware for Koa 2).

It stores session data in a database defined by you, using the Knex query builder.

It has been tested with SQLite and PostgreSQL.

Forked and modified from koa-generic-session-sequelize.

Installation

npm install --save koa-generic-session-knex

Example

Full example in examples/basic_sqlite.js.

const KnexStore = require('koa-generic-session-knex');

// set up Knex in the usual manner
// for a quick example using the sqlite3 module:
const knex = Knex({
  client: 'sqlite3',
  connection: {
    filename: "./mydb.sqlite"
  }
});

app.use(session({
  store: new KnexStore(
    knex,            // pass your knex object as the first arg
    {}                    // pass any config options for knexStore as the second arg (see below)
  )
}));

Options

Unit tests

To run the test suite, clone this repository and run npm install in the checkout directory. Then run npm test. This will exercise the library against SQLite.

To test against MySQL, PostgreSQL, or SQL Server, edit test/config.js. Uncomment sections referencing those servers and enter your credentials. The table _sess_test will be created during testing.