Skip to content

GuoZhang/koa-session-sequelize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-session-sequelize

sequelizejs based koa session middleware

sequelize storage layer for koa-session-store or koa-generic-session.

Installation

npm install koa-session-sequelize

Usage

This store requires either koa-session-store or koa-generic-session.

var session = require('koa-session-store'); // or you can use 'koa-generic-session'
var Sequelize = require('sequelize');


var koa = require('koa');

// instantiation Sequelize see [Sequelize API Reference](https://github.com/sequelize/sequelize/wiki/API-Reference) for more detail
var sequelize = new Sequelize('test', 'username', '***', {  // instantiation
  dialect: 'sqlite',
  storage: ':memory:'
});

var sequelizeStore = require('koa-session-sequelize');

var app = koa();

app.keys = ['some secret key'];  // needed for cookie-signing

app.use(session({
  store: sequelizeStore.create(sequelize, {
    table: 'sessions',   // (optional) table to store sessions (default to 'sessions')
    model: 'Session',    // (optional) model to represent session (default to 'Session')
    dataField: { type: Sequelize.STRING, allowNull: true }  // (optinal) dataField definition (defaults to: { type: Sequelize.STRING, allowNull: true })
                                                            // type of this field should be those that can hold string data
  })
}));

app.use(function *() {
  var n = this.session.views || 0;
  this.session.views = ++n;
  this.body = n + ' views';
});

app.listen(3000, function () {
  console.log('listening on port 3000');
});

You can specify model name, collection name, and expiration time (in seconds):

Other Relevant Modules

Tests

From root directory:

npm install && npm test

Tests will be run with in-memory sqlite database.

License

The MIT License (MIT)

Copyright (c) 2014 Brian Zhang < gzhang.82@gmail.com >

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

sequelizejs based koa session middleware

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published