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.

Creating MongoDB session store

Creating MongoDB session store

Now, we are going to create a session store, where we will keep the user sessions. Just like MySQL has its own session store library, which is Express MySQL session, MongoDB has its own session store called Connect Mongo. So let's install the package. I'll give the command npm install hyphen hyphen save connect hyphen mongo and the package is installed. I'll remove the MySQL store and import the connect mongo. We'll say const mongo store is equal to require connect mongo. Now we do not require to define the connection pool for the session because unlike MySQL, Mongoose has its built-in connection pooling, which is totally automated. So we will just configure the MongoStore. I'll give the MongoStore.create method to create the sessions collection and pass a few configuration settings like MongooseConnection to Mongoose.Connection. Mongoose.Connection is the active MongooseConnection instance. When you pass mongoose.connection to mongostore, it uses the existing Mongoose connection to…

Contents