mongo-express-session
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

mongo-express-session

npm version built with typescript JavaScript Style Guide CircleCI

A session store for express-session using mongoDB.

  • Does not have the mongodb client as a dependency. Use your own mongodb client, meaning you can configure your client to however you need it to work with this store.
  • Can auto-expire using mongo indexes or with an interval.
  • Fully unit tested. PRs welcomed.

Install

$ npm i mongo-express-session --save

Usage

import { MongoClient } from 'mongodb'
import * as express from 'express'
import * as session from 'express-session'
import { mongoStoreFactory } from 'mongo-express-session'

const MongoStore = mongoStoreFactory(session)
const app = express()

// Create and connect your mongo client
const client = await MongoClient.connect(...)

app.use(session({
    store: new MongoStore({
      client,
      dbName: 'my-database',
      collection: 'sessions',
      ttlMs: 10000,
    }),
    //... don't forget other expres-session options you might need
}))

MongoStore options

  /**
   * The mongo client
   */
  client: MongoClient

  /**
   * Name of the database to use
   */
  dbName: string

  /**
   * Name of the collection to use for session data
   */
  collection: string

  /**
   * Strategy ('native' or 'interval') to use to clean up expired entries.
   * Default is 'native'. Native will create an index with a TTL to expire objects.
   */
  cleanupStrategy?: MongoSessionCleanupStrategy

  /**
   * Session TTL in milliseconds
   */
  ttlMs: number

  /**
   * Session id prefix. Default is no prefix
   */
  prefix?: string

  /**
   * Only applies if cleanup strategy is 'interval'. Triggers a timer in milliseconds to run a
   * cleanup on expired session rows. Default is 5 minutes.
   */
  cleanupInterval?: number
enum MongoSessionCleanupStrategy {
  /**
   * Use the mongo index TTL to clean out expired entries. Once this is set, you will have to
   * manually remove the 'updated_at_ttl_idx' index from your collection if you want to use the
   * interval strategy.
   */
  native = 'native',
  /**
   * Use a timer to periodically clean out expired entries.
   */
  interval = 'interval',
}

Debugging

This module uses debug under the name mongo-express-session. When starting up your app, do the following:

$ DEBUG=mongo-express-session node app.js

Package Sidebar

Install

npm i mongo-express-session

Weekly Downloads

900

Version

1.0.1

License

MIT

Unpacked Size

27 kB

Total Files

15

Last publish

Collaborators

  • theo.gravity