Giter Site home page Giter Site logo

koa-session-sequelize's Introduction

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 < [email protected] >

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.

koa-session-sequelize's People

Contributors

guozhang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.