Skip to content
Browse files

working, need some tests

  • Loading branch information...
0 parents commit f4494e6ead3201cf74e97221bcd5f1cd979339de @adamansky adamansky committed Jan 3, 2013
Showing with 159 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +22 −0 LICENSE
  3. +2 −0 README.md
  4. +13 −0 connect-session-ejdb.iml
  5. 0 example/example1.js
  6. +90 −0 lib/connect-session-ejdb.js
  7. +30 −0 package.json
2 .gitignore
@@ -0,0 +1,2 @@
+/.idea
+/ejdb-connect-session.iml
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Adamansky Anton <adamansky@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 README.md
@@ -0,0 +1,2 @@
+**This is a [connect](https://github.com/senchalabs/connect)
+session store backed by [EJDB database](https://github.com/Softmotions/ejdb)**
13 connect-session-ejdb.iml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$" />
+ <content url="file://$USER_HOME$/node_modules/connect/lib">
+ <excludeFolder url="file://$USER_HOME$/node_modules/connect/lib/public" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ </component>
+</module>
+
0 example/example1.js
No changes.
90 lib/connect-session-ejdb.js
@@ -0,0 +1,90 @@
+var Store = require("connect").session.Store;
+
+var EJS = module.exports = function(ejdb, cname, opts) {
+ if (ejdb == null) {
+ throw new Error("Missing required 'ejdb' arg");
+ }
+ cname = cname || "connect-sessions";
+ this.cname = cname;
+ this.ejdb = ejdb;
+ ejdb.ensureStringIndex(cname, "s");
+};
+
+/**
+ * Inherit from `Store.prototype`.
+ */
+
+EJS.prototype.__proto__ = Store.prototype;
+
+EJS.prototype.set = function(sid, sess, cb) {
+ this.ejdb.update(this.cname,
+ {s : sid,
+ $upsert : {
+ s : sid, //Session ID
+ t : +new Date(), //Update session time
+ d : sess //Session data
+ }
+ }, cb);
+};
+
+EJS.prototype.get = function(sid, cb) {
+ var me = this;
+ this.ejdb.findOne(this.cname, {s : sid}, function(err, obj) {
+ if (err) {
+ cb && cb(err);
+ return;
+ }
+ var d = obj ? obj.d : null;
+ if (d) {
+ var expires = (d.cookie && (typeof d.cookie.expires === "string")) ?
+ new Date(d.cookie.expires) : d.cookie.expires;
+ if (!expires || new Date < expires) {
+ cb && cb(null, d);
+ } else {
+ me.destroy(sid, cb);
+ }
+ } else if (cb) {
+ cb();
+ }
+ });
+};
+
+
+EJS.prototype.destroy = function(sid, cb) {
+ this.ejdb.update(this.cname, {s : sid, $dropall : true}, cb);
+};
+
+EJS.prototype.length = function(cb) {
+ this.ejdb.count(this.cname, {}, cb);
+};
+
+EJS.prototype.clear = function(cb) {
+ this.ejdb.update(this.cname, {$dropall : true}, cb);
+};
+
+EJS.prototype.all = function(cb) {
+ if (!cb) {
+ return;
+ }
+ this.ejdb.find(this.cname, {}, function(err, cursor, count) {
+ if (err) {
+ cb(err);
+ return;
+ }
+ var res = new Array(count);
+ for (var i = 0; cursor.next(); ++i) {
+ res[i] = cursor.field("d");
+ }
+ cb(null, res);
+ });
+};
+
+
+
+
+
+
+
+
+
+
30 package.json
@@ -0,0 +1,30 @@
+{
+ "name" : "connect-session-ejdb",
+ "version" : "1.0.0",
+ "description" : "EJDB session store for connect middleware",
+ "keywords" : ["ejdb",
+ "connect",
+ "session",
+ "store"],
+ "author" : "Anton Adamansky <adamansky@gmail.com>",
+ "contributors" : [
+ ],
+ "bugs" : {
+ "web" : "https://github.com/Softmotions/connect-session-ejdb/issues"
+ },
+ "os" : [
+ "linux",
+ "darwin",
+ "sunos"
+ ],
+ "engines" : {
+ "node" : "0.8.x"
+ },
+ "directories" : {
+ "lib" : "lib"
+ },
+ "main" : "./lib/connect-session-ejdb",
+ "dependencies" : {
+ "connect" : ">=2.0.0"
+ }
+}

0 comments on commit f4494e6

Please sign in to comment.
Something went wrong with that request. Please try again.