Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first release

  • Loading branch information...
commit c9e53882c05265a93608f7e809a943fe083acb38 0 parents
Federico Fissore authored
2  .gitignore
@@ -0,0 +1,2 @@
+.idea
+node_modules
6 Makefile
@@ -0,0 +1,6 @@
+REPORTER = spec
+
+test:
+ node_modules/mocha/bin/mocha --reporter $(REPORTER)
+
+.PHONY: test
2  index.js
@@ -0,0 +1,2 @@
+require("coffee-script");
+module.exports = require('./lib/connect-orientdb.coffee');
93 lib/connect-orientdb.coffee
@@ -0,0 +1,93 @@
+# connect-orientdb
+# Copyright(c) 2012 Federico Fissore <federico@fissore.org>
+# MIT Licensed
+
+orient = require "orientdb"
+connect = require "connect"
+
+default_options =
+ host: "localhost"
+ port: 2424
+ database: "sessions"
+ class_name: "Session"
+ user_name: "admin"
+ user_password: "admin"
+
+class OrientDBStore extends connect.session.Store
+
+ constructor: (options, callback) ->
+ options = options or {}
+
+ server = new orient.Server
+ host: options.host or default_options.host
+ port: options.port or default_options.port
+ @db = new orient.Db options.database, server,
+ user_name: options.user_name or default_options.user_name
+ user_password: options.user_password or default_options.user_password
+ @class_name = options.class_name or default_options.class_name
+
+ @db.open (err) =>
+ return callback(err) if err?
+
+ cluster = @db.getClusterByClass @class_name
+ return callback(null, @) if cluster?
+
+ @db.createClass @class_name, (err) =>
+ return callback(err) if err?
+
+ @db.command "CREATE PROPERTY #{@class_name}.sid STRING", (err) =>
+ return callback(err) if err?
+
+ @db.command "CREATE INDEX #{@class_name}.sid UNIQUE", (err) =>
+ return callback(err) if err?
+
+ @db.reload (err) =>
+ return callback(err) if err?
+
+ callback(null, @)
+
+ load_session_doc = (self, sid, callback) ->
+ self.db.command "SELECT FROM #{self.class_name} WHERE sid = '#{sid}'", (err, results) =>
+ return callback(err) if err?
+ if results.length > 0
+ callback(null, results[0])
+ else
+ callback()
+
+ get: (sid, callback) ->
+ load_session_doc @, sid, (err, session_doc) =>
+ return callback(err) if err?
+ return callback() if !session_doc
+
+ if !session_doc.expires or new Date() < session_doc.expires
+ callback(null, session_doc.session)
+ else
+ @destroy(sid, callback)
+
+ set: (sid, session, callback) ->
+ load_session_doc @, sid, (err, session_doc) =>
+ session_doc = session_doc || {}
+ session_doc["@class"] = session_doc["@class"] or @class_name
+ session_doc.session = session
+ session_doc.sid = sid
+
+ if session.cookie && session.cookie._expires
+ session_doc.expires = new Date(session.cookie._expires)
+
+ @db.save(session_doc, callback)
+
+ destroy: (sid, callback) ->
+ load_session_doc @, sid, (err, session_doc) =>
+ @db.delete session_doc, callback
+
+ length: (callback) ->
+ clusterName = @db.getClusterByClass(@class_name).name
+ @db.countRecordsInCluster clusterName, callback
+
+ clear: (callback) ->
+ @db.command "DELETE FROM #{@class_name}", callback
+
+ close: (callback) ->
+ @db.close callback
+
+module.exports = OrientDBStore
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "connect-orientdb",
+ "version": "0.0.1",
+ "description": "OrientDB session store for Connect",
+ "keywords": ["connect", "orientdb", "orient", "session", "express"],
+ "author": "Federico Fissore <federico@fissore.org>",
+ "dependencies": {
+ "coffee-script": ">= 1.3.1",
+ "orientdb": "git://github.com/ffissore/node-orientdb.git#master",
+ "connect": ">= 1.x"
+ },
+ "devDependencies": {
+ "mocha": "1.x"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "main": "index",
+ "engines": "node >= 0.6.x"
+}
98 test/connect-orientdb.test.coffee
@@ -0,0 +1,98 @@
+OrientDBStore = require("../index")
+assert = require("assert")
+
+options =
+ database: "temp"
+
+exports.test_set = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set-sid"
+ session =
+ foo: "bar"
+ store.set sid, session, (err, session) ->
+ assert.strictEqual err, null
+
+ store.db.command "select from Session where sid = '#{sid}'", (err, results) ->
+ assert.equal sid, results[0].sid
+ assert.equal "bar", results[0].session.foo
+
+ store.clear ->
+ done()
+
+exports.test_set_expires = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set_expires-sid"
+ data =
+ foo: "bar"
+ cookie:
+ _expires: "2011-04-26T03:10:12.000Z"
+
+ store.set sid, data, (err, session) ->
+ assert.strictEqual err, null
+
+ store.db.command "select from Session where sid = '#{sid}'", (err, results) ->
+ assert.equal sid, results[0].sid
+ assert.equal "bar", results[0].session.foo
+ assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());
+
+ store.clear ->
+ done()
+
+exports.test_set_get = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set-sid"
+ session =
+ foo: "bar"
+ store.set sid, session, (err, session) ->
+ assert.strictEqual err, null
+
+ store.get sid, (err, session) ->
+ assert.equal "bar", session.foo
+
+ store.clear ->
+ done()
+
+exports.test_set_length = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set-sid"
+ session =
+ foo: "bar"
+ store.set sid, session, (err, session) ->
+ assert.strictEqual err, null
+
+ store.length (err, count) ->
+ assert.equal 1, count
+
+ store.clear ->
+ done()
+
+exports.test_set_destroy = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set-sid"
+ session =
+ foo: "bar"
+ store.set sid, session, (err, session) ->
+ assert.strictEqual err, null
+
+ store.destroy sid, (err) ->
+ assert.strictEqual err, null
+
+ store.clear ->
+ done()
+
+exports.test_set_length_clear = (done) ->
+ new OrientDBStore options, (err, store) ->
+ sid = "test_set-sid"
+ session =
+ foo: "bar"
+ store.set sid, session, (err, session) ->
+ assert.strictEqual err, null
+
+ store.length (err, count) ->
+ assert.equal 1, count
+
+ store.clear ->
+ store.length (err, count) ->
+ assert.equal 0, count
+
+ done()
1  test/mocha.opts
@@ -0,0 +1 @@
+--ui exports --compilers coffee:coffee-script
Please sign in to comment.
Something went wrong with that request. Please try again.