Skip to content

feat: add mongodb session store #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

belfhi
Copy link

@belfhi belfhi commented Mar 31, 2025

resolves #1803

@belfhi belfhi changed the title Draft: add mongodb session store feat: add mongodb session store Mar 31, 2025
@belfhi belfhi force-pushed the add-mongodb-session-store branch from 7bdd0a1 to 8ebf888 Compare March 31, 2025 18:20
Copy link
Contributor

@minottic minottic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! I have left a couple of comments, maybe we could also add a test here.

store: MongoStore.create({
mongoUrl, // MongoDB connection string
collectionName: "sessions", // Collection name for storing sessions
ttl: 24 * 60 * 60, // Session TTL (24 hours)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this equal this?

if (expressSessionSecret) {
app.use(
session({
secret: expressSessionSecret,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl, // MongoDB connection string
collectionName: "sessions", // Collection name for storing sessions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "sessions" is the default, so maybe we can avoid specifying it here?

if (expressSessionSecret) {
app.use(
session({
secret: expressSessionSecret,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we leave space for choosing if to use mongoStore or in memory one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
I would add a property in configuration to select the current behavior or session in mongodb.

@@ -1,4 +1,5 @@
import session from "express-session";
import MongoStore from "connect-mongo";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to have both mongoose and connect-mongo?
Can we achieve the same functionality just with mongoose?

Comment on lines +107 to +114
store: MongoStore.create({
mongoUrl, // MongoDB connection string
collectionName: "sessions", // Collection name for storing sessions
ttl: 24 * 60 * 60, // Session TTL (24 hours)
}),
cookie: {
secure: true,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this post, it is possible to have only one connection to the database.
https://stackoverflow.com/questions/66731669/mongoose-and-connect-mongo

Can you look into it and see if we can reduce duplication?

Copy link
Collaborator

@martin-trajanovski martin-trajanovski Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this mongoose example in the connect-mongo package (https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js). But as we use the wrapper for mongoose @nestjs/mongoose then there might be some way to get the connection in a "nestjs" way. So I guess the idea is to reuse the existing mongoose connection if possible here instead of creating a new connection for the connect-mongo.

@Junjiequan
Copy link
Member

I'm second to nitrosx's comment, to avoid 2 db connections.
Although I'm more inclined utilizing Redis similar solutions to deal with temporary data like sessions. However, in this case that might just be a overkill

@nitrosx
Copy link
Member

nitrosx commented May 5, 2025

@belfhi do you have any update on this PR? Thank you so much

@minottic minottic mentioned this pull request May 5, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable multiple backend replicas
5 participants