typescriptexpresssessionnestjssession-store

Express mysql session store with Typescript


I'm trying to create a mysql session storage in NestJS with Typescript. I have installed the express-session, express-mysql-session and @types/express-mysql-session package. The code below compiles (main.ts file), but then there's an error in the console:

import { AppModule } from "./app.module";
import { env } from "./common/env";
import session from "express-session";
import MySQLStore from "express-mysql-session";

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    cors: { credentials: env.ENABLE_CORS, origin: env.CLIENT_HOST },
  });

  //session store
  const options = {
    host: "db",
    port: 3306,
    user: env.DB_USER,
    password: env.DB_PASSWD,
    database: env.DATABASE,
    checkExpirationInterval: 1000 * 60 * 60 * 2,
    expiration: 1000 * 60 * 60 * 24,
  };

  const store = MySQLStore(session);
  const sessionStore = new store(options);

  app.use(
    session({
      secret: env.COOKIE_SECRET,
      store: sessionStore,
      resave: false,
      saveUninitialized: false,
      cookie: {
        httpOnly: true,
        maxAge: 1000 * 60 * 60 * 24,
      },
    }),
  );

  await app.listen(env.PORT_BACKEND || 8080);
}
bootstrap();

And the error is picture here

Doing it with require instead of import won't even compile, I have tried the express-mysql-session package way:

var MySQLStore = require('express-mysql-session')(session);

How should I make it work? Or is there another better package for that?


Solution

  • Ok, so I made it work. Ended up installing also mysql2 package, you can read here why.

    Code ended up like this:

    import { NestFactory } from "@nestjs/core";
    import { env } from "./common/env";
    import { AppModule } from "./modules/app/app.module";
    import * as session from "express-session";
    import * as MySQLStoreCreator from "express-mysql-session";
    import * as mysql2 from "mysql2/promise";
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule, {
        cors: { credentials: env.ENABLE_CORS, origin: env.CLIENT_HOST },
      });
    
      //session store
      const options = {
        host: "db",
        port: 3306,
        user: env.DB_USER,
        password: env.DB_PASSWD,
        database: env.DATABASE,
        checkExpirationInterval: 1000 * 60 * 60 * 2,
        expiration: 1000 * 60 * 60 * 24,
      };
    
      const connection = mysql2.createPool(options);
      const sessionStore = new (MySQLStoreCreator(session))({}, connection);
    
      app.use(
        session({
          secret: env.COOKIE_SECRET,
          store: sessionStore,
          resave: false,
          saveUninitialized: false,
          cookie: {
            httpOnly: true,
            maxAge: 1000 * 60 * 60 * 24,
          },
        }),
      );
    
      await app.listen(env.PORT_BACKEND || 8080);
    }
    bootstrap();
    

    Maybe someone will find it helpful C: