connect-cosmosdb
Version:
Cosmos DB based session store for express-session
72 lines (71 loc) • 2.7 kB
TypeScript
import { SessionData, Store } from 'express-session';
import { CosmosClient } from '@azure/cosmos';
export interface CosmosStoreOptions {
cosmosClient: CosmosClient;
databaseName: string;
containerName?: string;
ttl?: number | {
(session: SessionData): number;
};
disableTouch?: boolean;
}
export default class CosmosStore extends Store {
private static options;
private database;
private container;
static readonly PARTITION_KEY_PATH = "/id";
private static _store;
static readonly DEFAULT_CONTAINER_NAME = "sessions";
static readonly DEFAULT_TTL = 86400;
private constructor();
private static updateOptions;
/**
* Initialize a Cosmos Store instance for Session Storage.
* @param options Cosmos Store Options
* @returns A Cosmos Store instance
*/
static initializeStore(options: CosmosStoreOptions): Promise<CosmosStore>;
/**
*
* Get the session dat for the session ID
* @param sid Session ID
* @param callback callback function to call after execution
*/
get(sid: string, callback?: (err: any, session?: SessionData | null | undefined) => void): Promise<void>;
/**
* Set the session data for the session ID
* @param sid Session ID
* @param session Session data
* @param callback callback function to call after execution
*/
set(sid: string, session: SessionData, callback?: ((err?: any) => void) | undefined): Promise<void>;
/**
* Destroy a session.
*
* @param sid Session ID to destroy
* @param callback callback function to call after execution
*/
destroy(sid: string, callback?: ((err?: any) => void) | undefined): Promise<void>;
/**
* Get all sessions in the store
* @param callback callback function to call after execution
*/
all(callback?: (err: any, obj?: SessionData[]) => void): Promise<void>;
/** Returns the number of sessions in the store. */
length(callback?: (err: any, length?: number) => void): Promise<void>;
/** Delete all sessions from the store. */
clear(callback?: (err?: any) => void): Promise<void>;
/** "Touches" a given session, resetting the idle timer. */
touch(sid: string, session: SessionData, callback?: () => void): Promise<void>;
private setup;
private createContainer;
/**
* Get Time to live value in seconds for the session.
* If the Expires value is set, TTL will be calculated based on Expiry.
* Else the default value of 1 day would be used.
* @param session
* @returns TTL value in seconds (Cosmos DB uses seconds as TTL)
*/
private getTTL;
static reset(): void;
}