sequelstore-connect

1.1.5 • Public • Published

An express-session store for Sequelize.js

npm Travis Coverage Status

Sequelize session store for Connect and Express

Compatibility

  • Support Express 4.x and 5.0
  • Support Node.js 0.10, 0.12, 4.x, 5.x and all io.js versions
  • Support for Sequelize up to version 3.15

Express or Connect integration

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

const session = require('express-session');
const SequelStore = require('sequelstore-connect')(session);
 
app.use(session({
    secret: 'foo',
    store: new SequelStore(options)
}));

Connection to Sequelize

sequelstore-connect can be used in three different ways, depending on your architecture, you should pick one which fits your requirements and architecture the best.

  • Usage with your existing database by adding a specific model.

  • Usage with your existing database by passing a custom model into it.

  • Or it can create the model for you.

const Sequelize = require('sequelize');
 
// Basic usage
const database = new Sequelize(config);
 
app.use(session({
    store: new SequelizeStore({database})
}));

Re-use your Sequelize instance.

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

/*
** There are many ways to create an instance.
** You should refer to the ORM documentation
** http://docs.sequelizejs.com/en/latest/
*/
 
 
 
const database = require ('./models').sequelize;
 
app.use(session({
    store: new MongoStore({ database })
}));

Using your own session models

Automatically use your db by creating a Model named ConnectSession

/*
 * IF this model exists anywhere in your db then
 * it will be used for session storage. It must be named "ConnectSession"
 */
module.exports = (sequelize, DataTypes) => {
  return sequelize.define('ConnectSession', {
    sid: {
      type: DataTypes.STRING,
      primaryKey: true
    },
    expires: DataTypes.DATE
  });
};

Using a custom session model

Use the sessionModel option for custom integration.

store = new SequelStore({
  database,
  sessionModel: db.models.FooModel
});

Session expiration

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

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

app.use(session({
    store: new MongoStore({
      database: db,
      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

Expired session are removed from the database every 15 minutes by default. You can change it by setting autoRemoveInterval

app.use(session({
    store: new MongoStore({
      database,
      autoRemoveInterval: 15 * 60 * 1000 // = 15 minutes. Default.
    })
}));

Disable expired sessions cleaning

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

app.use(session({
    store: new MongoStore({
      url: 'mongodb://localhost/test-app',
      autoRemoveInterval: 0
    })
}));

More options

  • fallbackMemory Fallback to MemoryStore. Useful if you want to use MemoryStore in some case, like in development environment.
  • transform Default is JSON.stringify - you can change the function that handles the session data.

Tests

npm install && npm test

Contributing

After cloning this repo, ensure dependencies are installed by running:

npm install

This library is written in ES6 and uses Babel for ES5 transpilation and Flow for type safety. Widely consumable JavaScript can be produced by running:

npm run build

Once npm run build has run, you may import or require() directly from node.

After developing, the full test suite can be evaluated by running:

npm test

While actively developing, we recommend running

npm run watch

in a terminal. This will watch the file system run lint, tests, and type checking automatically whenever you save a js file.

To lint the JS files and run type interface checks run npm run lint.

Acknowledgements

The following sources were heavily studied and/or used to product this library.

License

The MIT License

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.50latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.50
1.1.40
1.1.30
1.1.20
1.1.10
1.1.00
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i sequelstore-connect

Weekly Downloads

0

Version

1.1.5

License

MIT

Last publish

Collaborators

  • mattmcfarland