gx0r/connect-session-knex

default sqlite3 being used instead of mysql table sessions, option issue?

workinghuman opened this issue · 2 comments

const KnexSessionStore = require("connect-session-knex")(session);

app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());

app.use(express.static("public"));
app.use(cors());

let config = {
    timezone: "gmt",
    host: "host",
    user: "user",
    database: "database",
    password: "password"
};

const store = new KnexSessionStore({
     tablename: "sessions",
     sidfieldname: "sid"
});

app.use(
session({
    secret: "secret",
    store: store,
    resave: false,
    saveUninitialized: false,
    cookie: { sameSite: "strict" },
  }),
);

I didn't see an example with the options written in the creation of the KnexSessionStore object so I'm not sure that's right.

I've tried store as:

const store = new KnexSessionStore(
  tablename='sessions',
  sidfieldname='sid'
);
const store = new KnexSessionStore({
  tablename='sessions',
  sidfieldname='sid'
});
const store = new KnexSessionStore({
  tablename: 'sessions',
  sidfieldname: 'sid'
});

Error message:

D:\appTestLab\node_modules\knex\lib\client.js:235
throw new Error(${message}\n${e.message});

Error: Knex: run
$ npm install sqlite3 --save
Cannot find module 'sqlite3'

If anyone could help guide me that'd be great!

Hi, try following this example: https://www.github.com/llambda/connect-session-knex/tree/master/examples%2Fexample-postgres.js but instead of PostgreSQL, pass MySQL.

Hi omarryhan,

Thanks so much! I used:

var mysql = require('mysql');
var session = require('express-session');
const KnexSessionStore = require('connect-session-knex')(session);
const Knex = require('knex');


app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());

app.use(express.static('public'));
app.use(cors());

let config = {
    timezone: 'gmt',
    host: 'host',
    user: 'user',
    database: 'db',
    password: 'password'
};

const knex = Knex({
    client: 'mysql',
    connection: config
});

const store = new KnexSessionStore({
  knex,
  tablename: 'sessions'
});

app.use(
session({
    secret: 'cougarSession',
    store: store,
    resave: false,
    saveUninitialized: false,
    cookie: { sameSite: 'strict' },
  }),
);

For anyone that finds this helpful, I also used this for my mysql table:


create table sessions (
  sid varchar(255) primary key not null,
  sess JSON not null,
  expired datetime not null
);