Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented connect session

  • Loading branch information...
commit 4bd7daf279f7dcec97a3e4ae771ae2eb9fd464c5 1 parent bc09673
James Bloomer jamesbloomer authored roylines committed
Showing with 87 additions and 0 deletions.
  1. +1 −0  index.js
  2. +56 −0 lib/connect-cassandra.js
  3. +30 −0 package.json
1  index.js
View
@@ -0,0 +1 @@
+module.exports = require('./lib/connect-cassandra');
56 lib/connect-cassandra.js
View
@@ -0,0 +1,56 @@
+/* jshint proto:false */
+var util = require('util');
+
+module.exports = function (connect) {
+ var Store = connect.session.Store;
+ var columnFamily = 'connect_session';
+
+ function CassandraStore(options) {
+ var self = this;
+ Store.call(self, options);
+ self.pool = options.pool;
+
+ self.pool.cql(util.format('select * from %s limit 1', columnFamily), [], function(e1) {
+ if(e1) {
+ self.pool.cql(util.format('create columnfamily %s (key text PRIMARY KEY)', columnFamily), [], function(e2) {
+ if(e2) {
+ throw err;
+ }
+ });
+ }
+ });
+ }
+
+ CassandraStore.prototype.__proto__ = Store.prototype;
+
+ CassandraStore.prototype.get = function (sid, callback) {
+ this.pool.cql(util.format('select %s from %s where key = \'SESSIONS\'', sid, columnFamily), [sid], function(err, result) {
+ if(err) {
+ return callback(err);
+ }
+
+ if(result.length != 1) {
+ return callback();
+ }
+
+ return callback(null, JSON.parse(result[0].get(sid).value));
+ });
+ };
+
+ CassandraStore.prototype.set = function (sid, session, callback) {
+ var maxAge = session.cookie.maxAge;
+ var ttl = ('number' == typeof maxAge ? maxAge / 1000 | 0 : 86400);
+ session = JSON.stringify(session);
+ this.pool.cql(util.format('update %s set %s = ? where key = \'SESSIONS\' USING TTL %d', columnFamily, session, ttl), [sid], function(err) {
+ callback(err);
+ });
+ };
+
+ CassandraStore.prototype.destroy = function (sid, callback) {
+ this.pool.cql(util.format('delete %s from %s where key = \'SESSIONS\'', sid, columnFamily), [], function(err) {
+ callback(err);
+ });
+ };
+
+ return CassandraStore;
+};
30 package.json
View
@@ -0,0 +1,30 @@
+{
+ "name": "connect-cassandra",
+ "description": "a cassandra session store for connect.",
+ "version": "0.0.1",
+ "licenses": [{
+ "type": "MIT",
+ "url": "https://github.com/B2MSolutions/connect-cassandra/raw/master/LICENSE"
+ }],
+ "author": { "name": "B2M Solutions", "url": "https://github.com/B2MSolutions" },
+ "contributors": [
+ { "name": "Roy Lines", "url":"http://roylines.co.uk" },
+ { "name": "James Bloomer", "url":"https://github.com/jamesbloomer" }
+ ],
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/B2MSolutions/connect-cassandra.git"
+ },
+ "keywords": ["cassandra", "connect", "session"],
+ "dependencies": {
+ },
+ "devDependencies" : {
+ "mocha" : "x.x.x",
+ "connect" : "x.x.x",
+ "helenus" : "x.x.x"
+ },
+ "main": "./lib/connect-cassandra",
+ "scripts": { "test": "mocha test/*_test.js --reporter spec" },
+ "engines": { "node": ">= 0.6.0" }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.