Store a session only after a specific condition

if i do db.sessions.find({}) we found a record, even if you do execute the next() method.

I need that the record in database (sessions store) be created only when necessary and not ever a new request appear. I want store any information in mongodb after a specific condition be true. I need that the record in database be created only when necessary.

I created a session store and i would use it like a middleware:

var sessionStore = express.session({
    store: new MongoStore({
      url: 'mongodb://localhost:27017/3xam9l3'
    }),
    secret: '1234567890QWERTY'
});

Specific when i want use session, i do something like that:

app.get('/home', sessionStore, function(req, res) {
 ...
}

how do this ?

[]'s

Im not pretty sure if I understand your question but here comes my answer. In app we can use this:

app.use(session({secret: config.session.secret, saveUninitialized: true, resave: true}));

In the middleware you can use a condition like this:

var sessionStore = function(req, res, next){
   if (validate()){
     req.userName = "Jhon";
   }
   else{
     //do something else
   }
   next();
}

So if validate() is true you create a variable session, if not you do something else.

I reached a temporary solution. Anyway the session will be created in memory but it only be stored if a specific property of the session did created.

See in https://github.com/alvieirajr/connect-mongo/blob/master/lib/connect-mongo.js#L247