Skip to content

theogravity/express-session-sqlite

Repository files navigation

express-session-sqlite

npm version built with typescript JavaScript Style Guide CircleCI

A session store for express-session using SQLite.

Fully unit tested. PRs welcomed.

Install

$ npm i express-session-sqlite sqlite3 --save

Usage

import * as sqlite3 from 'sqlite3'
import * as express from 'express'
import * as session from 'express-session'
import sqliteStoreFactory from 'express-session-sqlite'

const SqliteStore = sqliteStoreFactory(session)
const app = express()

const store = new SqliteStore({
      // Database library to use. Any library is fine as long as the API is compatible
      // with sqlite3, such as sqlite3-offline
      driver: sqlite3.Database,
      // for in-memory database
      // path: ':memory:'
      path: '/tmp/sqlite.db',
      // Session TTL in milliseconds
      ttl: 1234,
      // (optional) Session id prefix. Default is no prefix.
      prefix: 'sess:',
      // (optional) Adjusts the cleanup timer in milliseconds for deleting expired session rows.
      // Default is 5 minutes.
      cleanupInterval: 300000
    })

app.use(session({
    store,
    //... don't forget other express-session options you might need
}))

Clean up stale sessions

There may be stale sessions that are left in the db. You may need a background process that executes the following:

await store.removeExpiredSessions()

Debugging

This module uses debug under the name express-session-sqlite. When starting up your app, do the following:

$ DEBUG=express-session-sqlite node app.js

About

SQLite-backed session store for express-session written in Typescript

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •