├── .gitignore ├── package.json ├── gulpfile.js ├── LICENSE.md ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .DS_Store 3 | *.pyc 4 | *.xcuserstate 5 | ._* 6 | .Spotlight-V100 7 | .Trashes 8 | Icon? 9 | ehthumbs.db 10 | Thumbs.db 11 | node_modules 12 | bower_components 13 | build 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-session-rethinkdb", 3 | "version": "0.4.1", 4 | "description": "RethinkDB session store for Express 4.x", 5 | "repository": { 6 | "type": "git", 7 | "url": "git://github.com/armenfilipetyan/express-session-rethinkdb.git" 8 | }, 9 | "keywords": [ 10 | "expressjs", 11 | "session", 12 | "store", 13 | "rethinkdb", 14 | "rethink", 15 | "db", 16 | "connect" 17 | ], 18 | "author": "armenfilipetyan", 19 | "license": "MIT", 20 | "dependencies": { 21 | "express-session": "^1.13.0", 22 | "memory-cache": "^0.1.4", 23 | "rethinkdbdash": "^2.2.18" 24 | }, 25 | "devDependencies": { 26 | "gulp": "^3.9.1", 27 | "gulp-jshint": "^2.0.0", 28 | "gulp-notify": "^2.2.0", 29 | "gulp-uglify": "^1.5.3", 30 | "jshint": "^2.9.1" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Gulp Build File 3 | */ 4 | 5 | 'use strict'; 6 | 7 | var gulp = require('gulp'); 8 | var uglify = require('gulp-uglify'); 9 | var notify = require('gulp-notify'); 10 | var jshint = require('gulp-jshint'); 11 | var bundle_version = require('./package.json').version; 12 | var bundle_name = require('./package.json').name; 13 | 14 | gulp.task('default', function() { 15 | return gulp.src('./lib/express-session-rethinkdb.js') 16 | .pipe(jshint()) 17 | .pipe(jshint.reporter('default')) 18 | .pipe(uglify()) 19 | .pipe(gulp.dest('./build/express-session-rethinkdb-' + bundle_version )) 20 | .pipe(notify({ message: bundle_name + ' ' + bundle_version + ' build complete' })); 21 | }); 22 | 23 | gulp.task('watch', function() { 24 | gulp.watch('./lib/express-session-rethinkdb.js', ['default']); 25 | }); 26 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015 Armen Filipetyan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Express Session RethinkDB 3 | * MIT Licensed 4 | */ 5 | 6 | var rethinkdb = require('rethinkdbdash'); 7 | var cache = require('memory-cache'); 8 | 9 | module.exports = function (session) { 10 | var Store = session.Store; 11 | 12 | function RethinkStore(options) { 13 | options = options || {}; 14 | options.connectOptions = options.connectOptions || {}; 15 | 16 | Store.call(this, options); 17 | 18 | r = options.connection || new rethinkdb(options.connectOptions); 19 | 20 | this.emit('connect'); 21 | this.sessionTimeout = options.sessionTimeout || 86400000; // 1 day 22 | this.table = options.table || 'session'; 23 | this.debug = options.debug || false; 24 | setInterval( function() { 25 | try { 26 | r.table(this.table).filter( r.row('expires').lt(r.now().toEpochTime().mul(1000)) ).delete().run(function(err, user) { 27 | return null; 28 | }); 29 | } 30 | catch (error) { 31 | console.error( error ); 32 | return null; 33 | } 34 | }.bind( this ), options.flushInterval || 60000 ); 35 | } 36 | 37 | RethinkStore.prototype = new Store(); 38 | 39 | // Get Session 40 | RethinkStore.prototype.get = function (sid, fn) { 41 | var sdata = cache.get('sess-'+sid); 42 | if (sdata) { 43 | if( this.debug ){ console.log( 'SESSION: (get)', JSON.parse(sdata.session) ) }; 44 | return fn(null, JSON.parse(sdata.session)); 45 | } else { 46 | r.table(this.table).get(sid).run().then(function (data) { 47 | return fn(null, data ? JSON.parse(data.session) : null); 48 | }).error(function (err) { 49 | return fn(err); 50 | }); 51 | } 52 | }; 53 | 54 | // Set Session 55 | RethinkStore.prototype.set = function (sid, sess, fn) { 56 | var sessionToStore = { 57 | id: sid, 58 | expires: new Date().getTime() + (sess.cookie.originalMaxAge || this.sessionTimeout), 59 | session: JSON.stringify(sess) 60 | }; 61 | 62 | r.table(this.table).insert(sessionToStore, { conflict: 'replace', returnChanges: true }).run().then(function (data) { 63 | var sdata = null; 64 | if(data.changes[0] != null) 65 | sdata = data.changes[0].new_val || null; 66 | 67 | if (sdata){ 68 | if (this.debug){ console.log( 'SESSION: (set)', sdata.id ); } 69 | cache.put( 'sess-'+ sdata.id, sdata, 30000 ); 70 | } 71 | if (typeof fn === 'function') { 72 | return fn(); 73 | } 74 | else 75 | return null 76 | }).error(function (err) { 77 | return fn(err); 78 | }); 79 | }; 80 | 81 | // Destroy Session 82 | RethinkStore.prototype.destroy = function (sid, fn) { 83 | if (this.debug){ console.log( 'SESSION: (destroy)', sid ); } 84 | cache.del('sess-'+sid); 85 | r.table(this.table).get(sid).delete().run().then(function (data) { 86 | if (typeof fn === 'function'){ 87 | return fn(); 88 | } 89 | else return null; 90 | }).error(function (err) { 91 | return fn(err); 92 | }); 93 | }; 94 | 95 | return RethinkStore; 96 | }; 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express Session RethinkDB 2 | 3 | RethinkDB session store for Express 4.x 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install express-session-rethinkdb --save 9 | ``` 10 | 11 | ## Getting started 12 | 13 | Note that you must already have Express Session already installed (`npm install express-session --save`). Also this specific package uses `rethinkdbdash` driver which has more advanced features. 14 | *See [rethinkdbdash](https://github.com/neumino/rethinkdbdash)* 15 | 16 | ```javascript 17 | var express = require('express'); 18 | var cookie = require('cookie-parser'); 19 | var session = require('express-session'); 20 | var RDBStore = require('express-session-rethinkdb')(session); 21 | 22 | var app = express(); 23 | var rdbStore = new RDBStore({ 24 | connectOptions: { 25 | servers: [ 26 | { host: '192.168.0.100', port: 28015 }, 27 | { host: '192.168.0.101', port: 28015 }, 28 | { host: '192.168.0.102', port: 28015 } 29 | ], 30 | db: 'test', 31 | discovery: false, 32 | pool: false, 33 | buffer: 50, 34 | max: 1000, 35 | timeout: 20, 36 | timeoutError: 1000 37 | }, 38 | table: 'session', 39 | sessionTimeout: 86400000, 40 | flushInterval: 60000, 41 | debug: false 42 | }); 43 | 44 | app.use(cookie()); 45 | app.use(session({ 46 | key: 'sid', 47 | secret: 'my5uperSEC537(key)!', 48 | cookie: { maxAge: 860000 }, 49 | store: rdbStore 50 | })); 51 | 52 | // the rest of your server code.. 53 | ``` 54 | ###With rethinkdbdash connection 55 | 56 | ```javascript 57 | var express = require('express'); 58 | var cookie = require('cookie-parser'); 59 | var session = require('express-session'); 60 | var RDBStore = require('express-session-rethinkdb')(session); 61 | var rethinkdb = require('rethinkdbdash'); 62 | 63 | var r = new rethinkdb({db: "test"}) //normal rethinkdbdash connection can be used across your project 64 | var app = express(); 65 | var rdbStore = new RDBStore({ 66 | connection: r, 67 | table: 'session', 68 | sessionTimeout: 86400000, 69 | flushInterval: 60000, 70 | debug: false 71 | }); 72 | 73 | app.use(cookie()); 74 | app.use(session({ 75 | key: 'sid', 76 | secret: 'my5uperSEC537(key)!', 77 | cookie: { maxAge: 860000 }, 78 | store: rdbStore 79 | })); 80 | 81 | // the rest of your server code.. 82 | ``` 83 | ###With Thinky 84 | 85 | ```javascript 86 | var express = require('express'); 87 | var cookie = require('cookie-parser'); 88 | var session = require('express-session'); 89 | var RDBStore = require('express-session-rethinkdb')(session); 90 | var thinky = require('thinky')({db: "test"}); 91 | 92 | var app = express(); 93 | var rdbStore = new RDBStore({ 94 | connection: thinky.r, 95 | table: 'session', 96 | sessionTimeout: 86400000, 97 | flushInterval: 60000, 98 | debug: false 99 | }); 100 | 101 | app.use(cookie()); 102 | app.use(session({ 103 | key: 'sid', 104 | secret: 'my5uperSEC537(key)!', 105 | cookie: { maxAge: 860000 }, 106 | store: rdbStore 107 | })); 108 | 109 | // the rest of your server code.. 110 | ``` 111 | 112 | ##Constructor options 113 | 114 | ###connection 115 | rethinkdbdash connection instance. 116 | *See [RethinkDB's doc](http://www.rethinkdb.com/api/javascript/#connect)* 117 | *Also see [rethinkdbdash](https://github.com/neumino/rethinkdbdash)* 118 | 119 | ###connectOptions 120 | Options for connecting to the database server. (if connection is set connectOptions will be ignored) 121 | *See [RethinkDB's doc](http://www.rethinkdb.com/api/javascript/#connect)* 122 | *Also see [rethinkdbdash](https://github.com/neumino/rethinkdbdash)* 123 | 124 | ###table 125 | Name of the table in which session data will be stored. 126 | `Default: 'session'` 127 | 128 | ###sessionTimeout 129 | If you do not set ```cookie.maxAge``` in ```session``` middleware, sessions will last until the user closes their browser. 130 | However we cannot keep the session data infinitely (for size and security reasons). 131 | In this case, this setting defines the maximum length of a session, even if the user does not close their browser. 132 | `Default: 86400000` *1 day* 133 | 134 | ###flushInterval 135 | RethinkDB does not yet provide an expiration function ( like ```SETEX``` for Redis ), so we have to remove the old expired sessions from the database intermittently. This is the time interval in milliseconds between flushing of expired sessions. 136 | `Default: 60000` *60 seconds* 137 | 138 | ###debug 139 | If debug is enabled, this middleware will start using console.log() to print debug type messages for session get, set, and destroy. These are currently mostly geared toward the memory caching. It takes a ```true``` or ```false``` value. `Default: false` 140 | 141 | ## Attribution 142 | 143 | *Inspired by TJ Holowaychuk's [Connect Redis](https://github.com/visionmedia/connect-redis)* 144 | 145 | *Inspired by guillaumervls [Connect RethinkDB](https://github.com/guillaumervls/connect-rethinkdb)* 146 | 147 | ##License 148 | 149 | The MIT License (MIT) 150 | 151 | Copyright (c) 2014-2015 Armen Filipetyan 152 | 153 | Permission is hereby granted, free of charge, to any person obtaining a copy 154 | of this software and associated documentation files (the "Software"), to deal 155 | in the Software without restriction, including without limitation the rights 156 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 157 | copies of the Software, and to permit persons to whom the Software is 158 | furnished to do so, subject to the following conditions: 159 | 160 | The above copyright notice and this permission notice shall be included in 161 | all copies or substantial portions of the Software. 162 | 163 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 164 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 165 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 166 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 167 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 168 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 169 | THE SOFTWARE. 170 | --------------------------------------------------------------------------------