Use any database that databank supports as a connect session store
Copyright 2012, E14N Inc.
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.
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 connect 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 connect session backend.
Try something like this.
var connect = require("connect"),
Logger = require("bunyan"),
Databank = require("databank").Databank,
DatabankStore = require("connect-databank")(connect),
driver,
params,
db;
// Example params. See databank for details.
driver = "disk";
params = {dir: "/var/lib/databank/session"};
// Get a bank and connect...
db = Databank.get(driver, params);
db.connect({}, 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 = connect();
app.use(connect.cookieParser());
app.use(connect.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.
-
new DatabankStore(bank, log, cleanup)
Create a new databank store.
bank
is aDatabank
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
connect.session()
as thestore
parameter. See the connect session middleware documentation for more details.
This is the interface that connect 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
andsession
.Note that in the case that a session is not available, the
err
value isnull
andsession
is alsonull
. -
set(sid, session, callback)
Save session
session
with idsid
; thecallback
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
; thecallback
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: anerr
and an array of sessions. -
length(callback)
Get the count of sessions have not expired.
callback
takes two parameters: anerr
and an integer count. -
clear(callback)
Delete all sessions.
callback
takes one parameter: anerr
value.
-
cleanup(callback)
Delete all sessions that have expired. This is the method that's called to garbage-collect sessions.
callback
takes one parameter: anerr
.NOTE that you have to have a
maxAge
on yourcookie
parameter forconnect.session()
for this to work. It won't clean up sessions that never expire -- browser sessions -- even if they're very, very old.