npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knex-schema-session-store

v0.6.0

Published

knex session store with schema

Downloads

4

Readme

knex-schema-session-store

Store sessions in a database using knex, and keep the session table in user defined schema.

Why this

You store many data in the session, but you can not search the session table because the data is stored in a text column. For example, a web application has many clients on different paltforms like mobile, web. Different client can login to the system at the same time, so an account can login on mobile and on web. But what if the user want to kick off his other platform login information on the web. We need to search the session table with the user_id, show them to the user, and let the user delete one. So user_id cannot be in the text column storing the non-schema session data, it should has its own column. That's the reason I made this.

Usage

It stores session data in a Knex connected database. It split the session data to with_schema part and no_schema part. The with_schema part will be stored in relevant columns in database. The no_schema part will be stored in a text column in database.

It expose functions:

constructor(knex: Knex, options?: Options);

wait_for_sync(): Promise<void>;
get(sid: string): Promise<any>;
set(sid: string, sess: any, max_age?: number): Promise<void>;
touch(sid: string, max_age?: number): Promise<void>;
destroy(sid: string): Promise<void>;
gc(): Promise<void>;
clear(): Promise<void>;
repo(): Knex.QueryBuilder;

Installation

npm install --save knex-schema-session-store

Example

import Store from 'knex-schema-session-store';

const StoreOptions = {
    table_name: 'sessions',
    sid_name: 'sid',
    expire_at_name: 'expire_at',
    additional_name: 'additional',
    sync: true,
    sync_timeout: 3000,
    gc_interval: 1000 * 60 * 60,
    timestamps: false,
    max_age: 1000 * 60 * 60 * 24,
    schemas: [],
};

StoreOptions.schemas.push({name: 'user_id', type: 'string', args: [100], extra: cb => cb.notNullable()});

const store = new Store(knex, StoreOption)

app.use(session({ store }));

store.wait_for_sync().then(() => {
    app.listen(3000);
});

Options

interface Options {
    // Name of the session table in the db (default: `sessions`)
    table_name?: string
    // Name of the sid column in the table (default: `sid`)
    sid_name?: string
    // Name of the expire_at column in the table (default: `expire_at`)
    expire_at_name?: string
    // Name of the `no_schema` part column in the table (default: `additional`)
    additional_name?: string
    // If true, the table will have `updated_at` and `created_at` columns (default: `false`)
    timestamps?: boolean
    // Create the sessions table if it doesn’t exist (default: `true`)
    sync?: boolean
    // If we create the sessions table, how long will we wait (in milliseconds, default: 3000)
    sync_timeout?: number
    // Do garbage collection every gc_interval (in milliseconds, default: 1000 * 60 * 60, aka an hour)
    gc_interval?: number
    // If the session package doesn't pass a max_age to this store, how long will this package remember the session(in milliseconds, default: 1000 * 60 * 60 * 24, aka one day)
    max_age?: number,
    // Defined the `with_schema` part using knex SchemaBuilder
    schemas?: SchemaField[],
}

SchemaField

/**
 * It will use knex as this to build the schema:
 *      extra ? extra(table_builder[type](name, ...args)) : null
 * And it will use these schemas to split and merge the session data
 */
interface SchemaField {
    name: string,
    type: string,
    args?: any[],
    extra?: (cb: Knex.ColumnBuilder) => void,
}