chill117 / express-mysql-session

A MySQL session store for the express framework in node
MIT License
313 stars 109 forks source link

Cannot specify database when using an existing connection pool #146

Open RedSpid3r opened 1 year ago

RedSpid3r commented 1 year ago

I am trying to using my existing connection pool with express-mysql-session, however, in doing so i am unable to specify the database name.

My app connected to multiple databases which is why I am not setting database name in mysql2 createPool options.

    const sessionStore = new MySQLStore({ database: "db_name" }, pool);
    app.use(
        session({
            key: `APP_${process.env.NODE_ENV}`,
            secret: process.env.SESSION_SECRET,
            store: sessionStore,
            resave: false,
            saveUninitialized: false,
            clearExpired: true,
            checkExpirationInterval: 900000,
            cookie: {
                secure: process.env.NODE_ENV === "production" ? true : false,
                maxAge: 2629800000,
            },
        })
    );

this results in:

  code: 'ER_NO_DB_ERROR',
  errno: 1046,
  sql: "SELECT `data` AS data, `expires` as expires FROM `sessions` WHERE `session_id` = '[REDACTED]'",
  sqlState: '3D000',
  sqlMessage: 'No database selected'

Is there a way to make this work or do i need to keep using 2 separate connection pools?

chill117 commented 1 year ago

When you pass an existing pool or connection object to the MySQLStore constructor, the module will not create a new pool internally - it will use the one that you provided. So the database-related options (e.g. database) will not be used. Create a new pool object with the other database name and then pass that to the constructor.