Skip to content

A session store implementation for Connect & Express backed by an NeDB datastore (either in-memory or file-persisted)

License

Notifications You must be signed in to change notification settings

JamesMGreene/nedb-session-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nedb-session-store

GitHub Latest Release Build Status Dependency Status Dev Dependency Status

A session store implementation for Express & Connect backed by an NeDB datastore (either in-memory or file-persisted).

Compatibility

Supports integration with:

Getting Started

npm install --save nedb-session-store

Usage

// ...set up a Connect-compatible session/app...

var NedbStore = require('nedb-session-store')( connectCompatibleSession );

var store = new NedbStore({
  filename: 'path_to_nedb_persistence_file.db'
});

For further details on how to integrate this module with various Connect-compatible middleware environments (e.g. Express), see Middleware Integration

Options

defaultExpiry

Optional. [Date] The default expiry period (max age) in milliseconds to use if and ONLY if the session's expiration is not controlled by the session Cookie configuration. Defaults to 2 weeks.

inMemoryOnly

Optional. [Boolean] Only persist the datastore within the available in-process memory. Defaults to false.

filename

Optional. [String] The path to the file where the datastore will be persisted. If not provided, the datastore will automatically be assigned the filename of 'data/sessions.db'.

For more details about the underlying filename option, please read about it in the NeDB documentation.

afterSerialization

Optional. [Function] A hook that you can use to transform data after it was serialized and before it is written to disk. A common example usage for this hook is to encrypt data before writing the database to disk.

ONLY applies when your NeDB datastore is file-persisted!

For more details about the underlying afterSerialization option, please read about it in the NeDB documentation.

beforeDeserialization

Optional. [Function] The inverse of afterSerialization: a hook that you can use to transform data after it was read from disk and before it is deserialized. A common example usage for this hook is to decrypt data after reading the database from disk.

ONLY applies when your NeDB datastore is file-persisted!

For more details about the underlying beforeDeserialization option, please read about it in the NeDB documentation.

corruptAlertThreshold

Optional. [Number] NeDB will refuse to start if more than this percentage of the datafile is corrupt. Valid values must be a number between 0 (0%) and 1 (100%). A value of 0 means you do NOT tolerate any corruption, 1 means you do not care about corruption. NeDB uses a default value of 0.1 (10%).

ONLY applies when your NeDB datastore is file-persisted!

For more details about the underlying corruptAlertThreshold option, please read about it in the NeDB documentation.

autoCompactInterval

Optional. [Number] NeDB's file persistence uses an append-only format for performance reasons, meaning that all updates and deletes actual result in lines being added at the end of the datastore file. To compact the file back into a 1-line-per-document format, you must either restart your application or specify an automatic compaction interval with this option. Valid values must be either null (disabled) or an integer between 5000 (5 seconds) and 86400000 (1 day). Defaults to 1 day.

ONLY applies when your NeDB datastore is file-persisted!

For more details about the underlying automatic compaction functionality, please read about it in the NeDB documentation.

Middleware Integration

Express

To integrate with modern versions of Express (4.x and above):

var sharedSecretKey = 'yoursecret';
var express = require('express');
var session = require('express-session');
var app = express();

var NedbStore = require('nedb-session-store')(session);

app.use(
  session({
    secret: sharedSecretKey,
    resave: false,
    saveUninitialized: false,
    cookie: {
      path: '/',
      httpOnly: true,
      maxAge: 365 * 24 * 60 * 60 * 1000   // e.g. 1 year
    },
    store: new NedbStore({
      filename: 'path_to_nedb_persistence_file.db'
    })
  })
);

To integrate with deprecated versions of Express (3.x and 2.x):

var sharedSecretKey = 'yoursecret';
var express = require('express');
var app = express();

var NedbStore = require('nedb-session-store')(express);

app.use(express.cookieParser(sharedSecretKey));
app.use(
  express.session({
    secret: sharedSecretKey,
    resave: false,
    saveUninitialized: false,
    cookie: {
      path: '/',
      httpOnly: true,
      maxAge: 365 * 24 * 60 * 60 * 1000   // e.g. 1 year
    },
    store: new NedbStore({
      filename: 'path_to_nedb_persistence_file.db'
    })
  })
);

Connect

To integrate with modern versions of Connect (3.x and above):

var sharedSecretKey = 'yoursecret';
var connect = require('connect');
var session = require('express-session');
var app = connect();

var NedbStore = require('nedb-session-store')(session);

app.use(
  session({
    secret: sharedSecretKey,
    resave: false,
    saveUninitialized: false,
    cookie: {
      path: '/',
      httpOnly: true,
      maxAge: 365 * 24 * 60 * 60 * 1000   // e.g. 1 year
    },
    store: new NedbStore({
      filename: 'path_to_nedb_persistence_file.db'
    })
  })
);

To integrate with deprecated versions of Connect (2.x and 1.x (technically >= 1.0.3)):

var sharedSecretKey = 'yoursecret';
var connect = require('connect');
var app = connect();

var NedbStore = require('nedb-session-store')(connect);

app.use(connect.cookieParser(sharedSecretKey));
app.use(
  connect.session({
    secret: sharedSecretKey,
    resave: false
    saveUninitialized: false,
    cookie: {
      path: '/',
      httpOnly: true,
      maxAge: 365 * 24 * 60 * 60 * 1000   // e.g. 1 year
    },
    store: new NedbStore({
      filename: 'path_to_nedb_persistence_file.db'
    })
  })
);

License

Copyright (c) 2015, James M. Greene @ Viavi Solutions, Inc. (MIT License)

This software was developed during the course of my work at Viavi Solutions, Inc. and has been publicly released with their permission.

About

A session store implementation for Connect & Express backed by an NeDB datastore (either in-memory or file-persisted)

Resources

License

Stars

Watchers

Forks

Packages

No packages published