vivocha/connect-mongo

Name: connect-mongo

Owner: Vivocha

Description: MongoDB session store for Express and Connect

Forked from: jdesboeufs/connect-mongo

Created: 2016-09-28 09:03:42.0

Updated: 2016-09-28 09:03:43.0

Pushed: 2016-09-28 09:09:15.0

Homepage:

Size: 241

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

connect-mongo

MongoDB session store for Connect and Express

npm version downloads Build Status Coverage Status Dependency Status

Compatibility

For older Node.js versions 0.10, 0.12 and io.js, please read the Node.js compatibility section

Usage
Express or Connect integration

Express 4.x, 5.0 and Connect 3.x:

t session = require('express-session');
t MongoStore = require('connect-mongo')(session);

use(session({
secret: 'foo',
store: new MongoStore(options)

Express 2.x, 3.x and Connect 1.x, 2.x:

t MongoStore = require('connect-mongo')(express);

use(express.session({
secret: 'foo',
store: new MongoStore(options)

For Connect 1.x and 2.x, just replace express by connect.

Connection to MongoDB

In many circumstances, connect-mongo will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.

Alternatively, you can configure connect-mongo to establish a new connection.

Re-use a Mongoose connection
t mongoose = require('mongoose');

asic usage
oose.connect(connectionOptions);

use(session({
store: new MongoStore({ mongooseConnection: mongoose.connection })


dvanced usage
t connection = mongoose.createConnection(connectionOptions);

use(session({
store: new MongoStore({ mongooseConnection: connection })

Re-use a native MongoDB driver connection (or a promise)

In this case, you just have to give your Db instance to connect-mongo. If the connection is not opened, connect-mongo will do it for you.


here are many ways to create dbInstance.
ou should refer to the driver documentation.


use(session({
store: new MongoStore({ db: dbInstance })

Or just give a promise…

use(session({
store: new MongoStore({ dbPromise: dbInstancePromise })

Create a new connection from a MongoDB connection string

MongoDB connection strings are the best way to configure a new connection. For advanced usage, more options can be configured with mongoOptions property.

asic usage
use(session({
store: new MongoStore({ url: 'mongodb://localhost/test-app' })


dvanced usage
use(session({
store: new MongoStore({
    url: 'mongodb://user12345:foobar@localhost/test-app?authSource=admins&w=1',
    mongoOptions: advancedOptions // See below for details
})

Events

A MongoStore instance will emit the following events:

| Event name | Description | Payload | —– | —– | —– | | create | A session has been created | sessionId | | touch | A session has been touched (but not modified) | sessionId | | update | A session has been updated | sessionId | | set | A session has been created OR updated (for compatibility purpose) | sessionId | | destroy | A session has been destroyed | sessionId |

Old Node.js versions compatibility

For versions 0.10, 0.12 and io.js, you must use the ES5 fallback:

MongoStore = require('connect-mongo/es5')(session);
Session expiration

When the session cookie has an expiration date, connect-mongo will use it.

Otherwise, it will create a new one, using ttl option.

use(session({
store: new MongoStore({
  url: 'mongodb://localhost/test-app',
  ttl: 14 * 24 * 60 * 60 // = 14 days. Default
})

Note: Each time an user interacts with the server, its session expiration date is refreshed.

Remove expired sessions

By default, connect-mongo uses MongoDB's TTL collection feature (2.2+) to have mongod automatically remove expired sessions. But you can change this behavior.

Set MongoDB to clean expired sessions (default mode)

connect-mongo will create a TTL index for you at startup. You MUST have MongoDB 2.2+ and administration permissions.

use(session({
store: new MongoStore({
  url: 'mongodb://localhost/test-app',
  autoRemove: 'native' // Default
})

Note: If you use connect-mongo in a very concurrent environment, you should avoid this mode and prefer setting the index yourself, once!

Set the compatibility mode

You have an older MongoDB version (compatible with connect-mongo) or you can't or don't want to create a TTL index.

connect-mongo will take care of removing expired sessions, using defined interval.

use(session({
store: new MongoStore({
  url: 'mongodb://localhost/test-app',
  autoRemove: 'interval',
  autoRemoveInterval: 10 // In minutes. Default
})

Disable expired sessions cleaning

You are in production environnement and/or you manage the TTL index elsewhere.

use(session({
store: new MongoStore({
  url: 'mongodb://localhost/test-app',
  autoRemove: 'disabled'
})

Lazy session update

If you are using express-session >= 1.10.0 and don't want to resave all the session on database every single time that the user refresh the page, you can lazy update the session, by limiting a period of time.

use(express.session({
secret: 'keyboard cat',
saveUninitialized: false, // don't create session until something stored
resave: false, //don't save session if unmodified
store: new mongoStore({
    url: 'mongodb://localhost/test-app',
    touchAfter: 24 * 3600 // time period in seconds
})

by doing this, setting touchAfter: 24 * 3600 you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)

More options
Tests
npm test

The tests use a database called connect-mongo-test.

License

The MIT License


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.