From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Storing sessions in MySQL

Storing sessions in MySQL

Generally, the sessions are unique IDs which are stored on the server, but they are stored in the memory. Now, when we have a large number of users, and in case many concurrent users especially, it will be very heavy on the server. So we should store the session IDs in the database rather than the server memory. And for that, we will use a package named express MySQL session, which will store the session data in the database. So let's install the package. I'll give the command npm install express MySQL session. After installing the package, let's import it inside the main TS file. I'll give const mysqlStore is equal to require express mysqlSession and will pass the session. So basically we are passing the created session object with the require statement. This is going to create an internal connection and pool to deal with the database table which will store the sessions. Next step is to create a store. So I will create a constant named session store and assign the new MySQL store…

Contents