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

connect-databank

v1.0.3

Published

Use any databank driver as a store for session sessions

Downloads

56

Readme

connect-databank

Use any database that databank supports as a session store

License

Copyright 2012, 2015, E14N Inc. Copyright 2016, Fuzzy.io.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

What's it do?

The databank package lets you use several different kinds of databases - Mongo, Redis, CouchBase, memory, disk - with a uniform API -- basically CRUD + search.

This module will let you use any of those databases as a session session backend.

I think it's particularly useful if you're distributing software and you don't want to make your users depend on some particular session backend.

How to use it

Try something like this.

var express = require("express"),
    cookieParser = require("cookie-parser"),
    session = require("express-session"),
    Logger = require("bunyan"),
    Databank = require("databank").Databank,
    DatabankStore = require("connect-databank")(session),
      driver,
      params,
    db;

// Example params. See databank for details.

driver = "disk";
params = {dir: "/var/lib/databank/session"};

// Get a bank and session...

db = Databank.get(driver, params);

db.session({}, function(err) {

    var store, app, log;

// Use bunyan for logging

log = new Logger({name: "myapp"});

// cleanup session store every 5 minutes

store = new DatabankStore(db, log, 600000);

app = expess();
app.use(session.cookieParser());
app.use(session.session({secret: "my dog has fleas", store: store, cookie: {maxAge: 180000}}));

app.listen(3000);
});

The DatabankStore will store stuff into the session type ("table") in your databank, keyed by session ID.

It will log out to the logger; you can omit that parameter if you don't want session logs.

External API

  • new DatabankStore(bank, log, cleanup)

    Create a new databank store. bank is a Databank object. log is a bunyan Logger object; if it's passed the store will log session info to that log. cleanup is a session GC interval in milliseconds; if it's not falsy the store will garbage-collect old unused sessions this often.

    You pass the DatabankStore to session() as the store parameter. See the express session middleware documentation for more details.

Store interface

This is the interface that session requires of us. You can fiddle with it if you want, but watch your fingers. The store will be available as req.sessionStore in your routes.

  • get(sid, callback)

    Get an existing session with id sid; return it to the callback. callback gets two parameters: err and session.

    Note that in the case that a session is not available, the err value is null and session is also null.

  • set(sid, session, callback)

    Save session session with id sid; the callback takes a single parameter, err.

    This should work whether or not the session id is new.

  • destroy(sid, callback)

    Delete the session with id sid; the callback takes a single parameter, err.

    This should work whether or not the session exists.

  • all(callback)

    Get all sessions that have not expired. callback takes two parameters: an err and an array of sessions.

  • length(callback)

    Get the count of sessions have not expired. callback takes two parameters: an err and an integer count.

  • clear(callback)

    Delete all sessions. callback takes one parameter: an err value.

Bonus features

  • cleanup(callback)

    Delete all sessions that have expired. This is the method that's called to garbage-collect sessions. callback takes one parameter: an err.

    NOTE that you have to have a maxAge on your cookie parameter for session() for this to work. It won't clean up sessions that never expire -- browser sessions -- even if they're very, very old.