sequelize-session-store
What is this?
This is a session store for use with Express and Sequelize. It strives to be database agnostic, much like Sequelize. Although there are a plethora of session managers for use with a direct connection to the database, I found the choices lacking when it came to usage with Sequelize.
Installation
npm install sequelize-session-store
- You need an Express application using the express-session middleware
- You will also need an instance of Sequelize
Session Store
The constructor takes an argument object:
-
sequelize
: This is the sequelize instance- Refer to the Sequelize documentation for configuring the instance
-
model
: A Sequelize model- You can define the model yourself with various fields but the following are REQUIRED to be in the table:
sid
,sess
,expire
-sid
: varchar, not null, primary key -sess
: json/varchar/text, not null -expire
: timestamp, not null - Specifying a model allows relational mappings to other databases and inclusion of other fields
- You can define the model yourself with various fields but the following are REQUIRED to be in the table:
If a sequelize instance is not passed in, an error will be thrown. If you do not pass it a model, one will be created and available for you to access using the SequelizeSessionStore instance.
-- default table used
CREATE TABLE express_session (
sid VARCHAR NOT NULL,
sess TEXT NOT NULL,
expire TIMESTAMP(6) NOT NULL,
PRIMARY KEY (sid)
)
Example
const express = require('express');
const session = require('express-session');
const Sequelize = require('sequelize');
const SequelizeSessionStore = require('sequelize-session-store')(session);
let sequelize = new Sequelize(); // refer to sequelize documentation on instantiation
let app = express();
let sessionConfig = {
// express session options here
store: SequelizeSessionStore({
model: aSequelizeModel,
db: sequelize
});
};
app.use(session(sessionConfig));
To Do
Check the to do list