I'm using sequelize as an ORM and passport.js (passport-local) for authentication. I noticed that every HTTP request is resulting in a separate database command. I started looking at the deserializeUser() function.
When loading a single page, this is what I get:
Executing: SELECT * FROM
Users
WHEREUsers
.id
=1 LIMIT 1;Over and over and over!
GET / 200 12ms - 780
Executing: SELECT * FROM
Users
WHEREUsers
.id
=1 LIMIT 1;Executing: SELECT * FROM
Users
WHEREUsers
.id
=1 LIMIT 1;Over and over and over!
GET /js/ui.js 304 4ms
Over and over and over!
GET /stylesheets/main.css 304 6ms
Executing: SELECT * FROM
Users
WHEREUsers
.id
=1 LIMIT 1;Over and over and over!
GET /images/logo.jpg 304 3ms
Here's how passport.deserializeUser looks:
passport.deserializeUser(function(id, done) {
User.find(id).success(function(user) {
console.log('Over and over and over!');
done(null, user);
}).error(function(err) {
done(err, null);
});
});
The page I'm requesting is:
index: function(req, res) {
res.render('index', {
title: "Welcome to EKIPLE!",
currentUser: req.user
});
}
Is the deserializeUser supposed to run for every image, html, css file requested? If so, is there a way of reducing the number of requests to the DB?