koa-session-mongoose

Mongoose storage layer for Koa session middleware

npm install koa-session-mongoose
2 downloads in the last day
32 downloads in the last week
91 downloads in the last month

koa-session-mongoose

Build Status NPM version

Mongoose storage layer for koa-session-store or koa-sess.

This can be used instead of koa-session-mongo with koa-session-store, for a more direct integration with an existing Mongoose connection.

Installation

npm install koa-session-mongoose

Usage

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

var session = require('koa-session-store'); // or you can use 'koa-sess'
var mongoose = require('mongoose');
var mongooseStore = require('koa-session-mongoose');
var koa = require('koa');

// mongoose connection must exist before creating a store with koa-session-mongoose
mongoose.connect('mongodb://some_host/some_db');

var app = koa();

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

app.use(session({
  store: mongooseStore.create()
}));

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

app.listen(3000);
console.log('listening on port 3000');

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

app.use(session({
  store: mongooseStore.create({
    model: 'KoaSession',
    collection: 'koaSessions',
    expires: 60 * 60 * 24 * 7    // 1 week
  })
}));

Other Relevant Modules

Tests

From root directory:

npm install
npm test

If you require a specific MongoDB URI, specify it as follows before npm test:

export URI="mongodb://[username:password@]host[:port]/[database]"

Otherwise, the following URI will be used:

mongodb://127.0.0.1/koa_mongoose_store_test

License

The MIT License (MIT)

Copyright (c) 2013 Michael J. Bondra < mjbondra@gmail.com >

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

npm loves you