0.1.2 • Published 4 months ago

express-sqlite v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

express-sqlite

An SQLite-based session store for use with express-session, using better-sqlite3, with TypeScript support.

npm version Last updated Publish on npm License

Table of contents

Usage

import express from "express";
import session from "express-session";
import { SQLiteStore } from "express-sqlite";

const app = express();

// With required arguments only
const store = new SQLiteStore("sessions.db");

// With customized options
const store = new SQLiteStore("sessions.db", {
  tableName: "Sessions",
  databaseOptions: { fileMustExist: false },
  wal: true,
});

app.use(
  session({ store, secret: "your-secret-key", cookie: { secure: true } }),
);

SQLiteStore constructor

class SQLiteStore extends Store {
  constructor(database: StoreDatabase, storeOptions?: StoreOptions) {}
}
ArgumentTypeDefault valueDescription
databaseStoreDatabaseRequired argumentThe SQLite database to store sessions in. Can be specified as a file path, a Buffer, or a BetterSqlite3.Database instance.
storeOptionsStoreOptions{ tableName: "Sessions", wal: true }Options for initializing the store.

StoreOptions

PropertyTypeDescription
databaseOptionsDatabase.OptionsOptions passed to better-sqlite3's Database constructor. Additionally, its fileMustExist property also controls whether the directory structure leading to the database file is created by this library, if it doesn't exist.
tableNamestringName of the table to store sessions in.
walbooleanControls the usage of SQLite's WAL-mode.

Types

TypeDefinitionDescription
ErrorOnlyCallback(error?: unknown) => voidCallback function with only the error argument. Used for store methods that don't return any data on success (clear, destroy, set).
SessionDataWithIdSessionData & { id: string }express-session's SessionData type extended with an id field that holds the session ID. Used for the all method.
StoreDatabasestring \| Buffer \| BetterSqlite3.DatabaseA union type for the values that can be used in the SQLiteStore constructor to specify the database used for storing sessions.

Sessions table structure

Column nameData typeConstraints
idTEXTPRIMARY KEY
dataTEXTNOT NULL

!NOTE If the table doesn't exist in the database, it's automatically created.

Methods

  • all(callback: (error: unknown, sessions?: SessionDataWithId[]) => void): void

    Retrieves all sessions in the store, including their IDs.

  • destroy(sid: string, callback?: ErrorOnlyCallback): void

    Removes the session with the given ID (sid), if it exists. No error is thrown if there is no such session.

  • clear(callback?: ErrorOnlyCallback): void

    Removes all sessions from the store.

  • length(callback: (error: unknown, length?: number) => void): void

    Returns the number of active sessions (length).

  • get(sid: string, callback: (error: unknown, session?: SessionData | null) => void): void

    Retrieves a session by its ID (sid). session is null if there is no such session in the store.

  • set(sid: string, session: SessionData, callback?: ErrorOnlyCallback): void

    Saves a session with the given ID (sid). If such a session already exists, its data will be overwritten.

!NOTE If an error occurs in any of the methods, the callback function will only have its error argument defined.

0.1.2

4 months ago

0.1.1

6 months ago

0.1.0

6 months ago