sequelstore-connect v1.1.5
An express-session store for Sequelize.js
Sequelize session store for Connect and Express
Compatibility
- Support Express
4.xand5.0 - Support Node.js
0.10,0.12,4.x,5.xand 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
fallbackMemoryFallback toMemoryStore. Useful if you want to use MemoryStore in some case, like in development environment.transformDefault isJSON.stringify- you can change the function that handles the session data.
Tests
npm install && npm testContributing
After cloning this repo, ensure dependencies are installed by running:
npm installThis 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 buildOnce 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 testWhile actively developing, we recommend running
npm run watchin 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.
- connect-session-sequelize.js
- https://github.com/mweibel/connect-session-sequelize
- by Michael Weibel
- MIT Licensed
- connect-mongo
- https://github.com/kcbanner/connect-mongo/
- by Casey Banner
- by Jerome Desboeufs
- MIT Licensed
- connect-redis
- https://github.com/tj/connect-redis
- by TJ Holowaychuk
- MIT Licensed
License
The MIT License