8

I'm trying to use express-session and express-mysql-session within a Typescript project. Here's the relevant part of my code:

import * as express from "express";
import * as expressSession from "express-session";
import * as expressMySqlSession from "express-mysql-session";

this.express = express();
const sessionStore = new expressMySqlSession(sessionStoreConfig, this.dbConnection);
const sessionHandler = expressSession({
                ...,
                store: sessionStore
            });
this.express.use(sessionHandler);

It does not compile because the store option is of type expressSession.Store | expressSession.MemoryStore | undefined while sessionStore is of type MySQLStore. What am I doing wrong?

Regards

Timo Kunze
  • 135
  • 1
  • 6
  • Notice how `session` is passed to the result of `require('express-mysql-session')` [in the example code](https://github.com/chill117/express-mysql-session#how-to-use), which is a step that you seem to be missing. – robertklep Jul 05 '17 at 08:53
  • Hmm, yes, you are right, but how would this call look like in ES6 syntax? The following lines should be equivalent, right? `import * as expressSession from "express-session";` `import expressSession = require("express-session");` The first one is the newer, ES6 syntax. But how would I write `require("xyz")(someparameter)` in ES6 syntax? Besides that would this really fix the type mismatch? – Timo Kunze Jul 05 '17 at 11:50
  • It's ugly, but adding: regenerate: (req: any, fn: (err?: any) => any) => void; load: (sid: string, fn: (err: any, session?: any | null) => any) => void; createSession: (req: any, sess: Express.SessionData) => void; all: (callback: (err: any, obj?: { [sid: string]: any; } | null) => void) => void; to MySQLStore declaration in index.d.ts solves it. – Max Oct 02 '18 at 10:28

3 Answers3

4

Try this:

import * as express        from "express";
import * as expressSession from "express-session";
import expressMySqlSession from "express-mysql-session";

const MySQLStore   = expressMySqlSession(expressSession);
const sessionStore = new MySQLStore(sessionStoreConfig, this.dbConnection);
...
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 1
    This does not compile. I get the following error (translated from German) on the line `const MySQLStore`: "The value of type 'typeof MySQLStore' cannot be called. Do you want to include 'new'?" I found a solution, but I have the feeling that it uses an old syntax: `const expressMySqlSession = require("express-mysql-session")(expressSession);`, followed by `return new expressMySqlSession(sessionStoreConfig, this.dbConnection);` By the way, I use Typescript 2.4.1 in case this matters. – Timo Kunze Jul 05 '17 at 13:37
  • @TimoKunze see edit, the import for `express-mysql-session` was incorrect. The code above works in "regular" JS. – robertklep Jul 05 '17 at 14:06
  • 2
    Now the import of expressMySqlSession fails with "Module '.../@types/express-mysql-session/index' has no default export." – Timo Kunze Jul 05 '17 at 14:52
2

With both express-session, express-mysql-session, and @types/express-mysql-session listed as dependencies:

Current version of the types (from about two years ago) tracks an older version of express-mysql-session, where the default export is the class. Fortunately, there's some backwards-compatibility in the library for this older version, so you can instantiate it directly:

import MySQLSessionStore from "express-mysql-session";

const store = new MySQLSessionStore({...});

And the library will handle importing the express-session dependency for you.

Code never lies!

adrian
  • 1,701
  • 1
  • 11
  • 5
0
import * as express from 'express';
import * as session from 'express-session';
const MySQLStore = require('express-mysql-session')(session);

import pool from './database';
const sessionStore = new MySQLStore({
  schema: {
    tableName: 'mire_sessions',
    columnNames: {
      session_id: 'session_id',
      expires: 'expires',
      data: 'data'
    }
  }
}, pool);

const app = express();
app.use(session({
  secret: 'z',
  resave: false,
  saveUninitialized: true,
  store: sessionStore
}))

This code worked for me. But I don't know why

Byeongin Yoon
  • 3,233
  • 6
  • 26
  • 44