Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use express-mysql-session in Typescript projects?

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

like image 224
Timo Kunze Avatar asked Jul 05 '17 08:07

Timo Kunze


2 Answers

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);
...
like image 75
robertklep Avatar answered Sep 26 '22 21:09

robertklep


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!

like image 44
adrian Avatar answered Sep 26 '22 21:09

adrian