MongoDB storage layer for Koa session middleware
npm install koa-session-mongo 
MongoDB storage layer for Koa session middleware. Based on connect-mongo.
``bash`
npm install koa-session-mongo
`js
var session = require('koa-session-store');
var mongoStore = require('koa-session-mongo');
var koa = require('koa');
var app = koa();
app.keys = ['some secret key']; // needed for cookie-signing
app.use(session({
store: mongoStore.create({
db: 'database_name'
})
}));
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');
`
If you wish to specify host, port, etc:
`js`
mongoStore.create({
host: 'mongo.hostname.com',
port: 48473,
db: 'database_name',
username: 'admin',
password: 'password',
})
Or you can pass in connection parameters as a URL string:
`js`
mongoStore.create({
url: 'mongodb://user:pass@host:port/database_name/collection_name'
})
Or you can use an existing node-mongo-native db object:
`js
var mongo = require('mongodb');
var db = new mongo.Db("database_name", new mongo.Server('host', port, {}), { w: 1 });
mongoStore.create({
db: dbConn,
collection: 'sessions',
username: 'admin',
password: 'password'
})
`
Or you can use an existing Mongoose connection:
`js
var mongoose = require('mongoose');
mongoose.connect(...);
var store = mongoStore.create({
mongoose: mongoose.connection
})
`
The following configuration options are available for the create() call:
* db String or Object - db name or instantiated node-mongo-native db object.String
* collection - collection name. Default is _sessions_.String
* host server hostname. Default is _127.0.0.1_.Number
* port - server port. Default is _27017_.String
* username - username. Default is _null_.String
* password - password. Default is _null_.Boolean
* auto_reconnect - gets passed to the node-mongo-native constructor as the same option. Default is _false_.Boolean
* ssl - use ssl to connect to the server. Default is _false_.Number
* expirationTime - time-to-live (TTL) in seconds for any given session data - MongoDB will auto-delete data which hasn't been updated for this amount of time. Default is 2 weeks.String
* url - connection URL of the form mongodb://user:pass@host:port/database/collection. If provided then this will take precedence over other options except mongoose.Object
mongoose - a Mongoose connection, use* mongoose.connection to get the connection out of an existing Mongoose object. If provided then this will take precedence over other options.
You can close all connections made through the storage layer using:
`js
var Promise = require('bluebird');
var mongoStore = require('koa-session-mongo');
...
Promise.spawn(function() {
mongoStore.closeConnections();
});
``
Copyright (c) 2013 Ramesh Nair
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.