'Express-session and MongoStore with Variable DB Name

Is there anyway to use a variable DB Name with Express-Sessions using Connect-Mongo/MongoStore for the store? I tried something like this:

app.use(session({
  secret: 'Secret',
  resave: false,
  cookie: {
    maxAge: 24 * 60 * 60 * 1000 * 365, //one year
    secure: false
  },
  saveUninitialized: true,
  store: MongoStore.create({
    mongoUrl: process.env.DATABASE,
    dbName: (req) => {
      return getDBName(req)
    }
  })
}));

But keep getting this error: MongoError: database name must be a string

Even when I switch getDBName(req) to just 'databaseName' I get the same error.

Any thoughts? Is it impossible? Do I just have to create my own session middleware?

Also, is it possible to just use a cached mongoose connection instead of creating a new connection?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source