Sequelize and Express Session Model

I am trying to get express sessions to work with postgres. After several hours of debugging, I fixed all the problems except one. Everything works, but the following:

If I run this request inside pgAdmin, my sessions are working correctly

CREATE TABLE "sessions" ( "sid" varchar NOT NULL COLLATE "default", "sess" json NOT NULL, "expire" timestamp(6) NOT NULL ) WITH (OIDS=FALSE); ALTER TABLE "sessions" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE; 

If I create a model with sequelize, it does not save sessions. I am sure, and it is obvious that I am missing some parts of the model definition and would appreciate any data.

 var Sessions = database.define('sessions', { sid: { type: Sequelize.STRING, primaryKey: true }, expire: { type: Sequelize.DATE, allowNull: true }, sess: Sequelize.JSON }); return Sessions; 

Rest of the code working with this

 var pgSession = require('connect-pg-simple')(expressSession), sessionSettings = { store: new pgSession ({ conString: 'postgres://' + dbConfig.user +':' + dbConfig.password+ '@' + dbConfig.host+ ':5432/' + dbConfig.database, tableName : 'sessions' }), secret: 'whatevergoeshere', resave: false, saveUninitialized: false, cookie: { maxAge: 7*24*60*60*1000 } }; app.use(expressSession(sessionSettings)); 

I have no idea how to include the missing parts and document the documentation, either briefly on the topic, or I did not have enough sleep

The rest of the code is available upon request, but pretty sure that it does not affect anything, because if I do not force synchronization with the sequelize model and use the request, everything will work.

In addition, here is how he looks at pgAdmin, the upper one is created with a request in pgAdmin, the lower one using sequelize

PostgreSQL

+5
source share
2 answers

Sequelize creates by default the createdAt and updatedAt columns, which are not allowed to be NULL. Using the connect-pt-simple module to add a string, it silently fails, because it does not provide a value for these two fields. Either handle this case yourself, or update these values, or in your model, set these two using the allowNull: true property or add a default value. Of course, the first option is preferable, and in the future it may be convenient.

+1
source

To automatically generate a session table using "connect-session-sequelize", you need to call the sync () method on the SequelizeStore instance

 var session = require('express-session'); var Sequelize = require('sequelize'); var db = new Sequelize('test', 'root', '****'); var SequelizeStore = require('connect-session-sequelize')(session.Store); var sessionStore = new SequelizeStore({ db: db, checkExpirationInterval: 15 * 60 * 1000, expiration: 7 * 24 * 60 * 60 * 1000 }); app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false, store: sessionStore })); sessionStore.sync() 
+1
source

Source: https://habr.com/ru/post/1237004/


All Articles