From 1490a9f9e6ae45b9780299ed7649d226ce23530f Mon Sep 17 00:00:00 2001 From: Jamison Dance Date: Mon, 30 Mar 2015 14:51:20 -0600 Subject: [PATCH] initial commit --- .gitignore | 2 ++ index.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++ test/test.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd4f2b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.DS_Store diff --git a/index.js b/index.js new file mode 100644 index 0000000..789270c --- /dev/null +++ b/index.js @@ -0,0 +1,66 @@ +var _ = require('lodash') + +function RethinkSession(opts) { + this.connection = opts.connection + this.dbName = opts.db + this.tableName = opts.table +} + +RethinkSession.prototype.setup = function*() { + var errors = [] + try { + yield this.connection.dbCreate(this.dbName) + } catch (e) { + errors.push(e) + } + + try { + yield this.connection.db(this.dbName).tableCreate(this.tableName) + } catch (e) { + errors.push(e) + } + + try { + yield this.connection.db(this.dbName).table(this.tableName).indexCreate('sid') + } catch (e) { + errors.push(e) + } + + return errors +} + +RethinkSession.prototype.table = function() { + return this.connection.db(this.dbName).table(this.tableName) +} + +RethinkSession.prototype.get = function* (sid) { + var res = yield this.table().getAll(sid, {index: 'sid'}) + return res[0] +} + +RethinkSession.prototype.set = function* (sid, session) { + // check if there is a doc with that id + var res = yield this.table().getAll(sid, {index: 'sid'}) + if (res[0]) { + res = res[0] + var payload = _.extend({ + sid: sid, + id: res.id + }, session) + + return yield this.table().get(res.id).replace(payload) + } else { + return yield this.table().insert(_.extend({ + sid: sid + }, session)) + } +} + +RethinkSession.prototype.destroy = function* (sid) { + var res = yield this.table().getAll(sid, {index: 'sid'}) + if (res[0]) { + return yield this.table().get(res[0].id).delete() + } +} + +module.exports = RethinkSession diff --git a/package.json b/package.json new file mode 100644 index 0000000..301fa11 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "koa-generic-session-rethinkdb", + "version": "1.0.0", + "description": "A RethinkDB session store for koa-generic-session", + "main": "index.js", + "scripts": { + "test": "mocha -R dot test/*.js" + }, + "keywords": [ + "koa", + "session", + "rethinkdb" + ], + "author": "Jamison Dance (http://jamisondance.com)", + "license": "ISC", + "devDependencies": { + "co-mocha": "1.1.0", + "rethinkdbdash": "1.16.13" + }, + "dependencies": { + "lodash": "3.6.0" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..f020c4e --- /dev/null +++ b/test/test.js @@ -0,0 +1,71 @@ +var RethinkSession = require('../index') +var assert = require('assert') +var rethinkdb = require('rethinkdbdash') + +require('co-mocha') + +describe('RethinkSession', function() { + var connection = rethinkdb() + var TEST_DB = 'koarethinkdbsession' + var TEST_TABLE = 'session' + + function makeRS() { + return new RethinkSession({ + connection: connection, + db: TEST_DB, + table: TEST_TABLE + }) + } + + before(function*() { + try { + yield connection.dbDrop(TEST_DB) + } catch (e) {} + }) + + it('has a `setup` function that creates a db, table, and indexes', function*() { + var rs = makeRS() + + yield rs.setup() + var res = yield connection.db(TEST_DB).table(TEST_TABLE) + assert(Array.isArray(res)) + assert.equal(res.length, 0) + + var indices = yield connection.db(TEST_DB).table(TEST_TABLE).indexList() + assert(Array.isArray(indices)) + assert.equal(indices.length, 1) + }) + + it('sets sessions', function* () { + var rs = makeRS() + var session = { wut: 'okay' } + yield rs.set('bar', session) + + var sessions = yield connection.db(TEST_DB).table(TEST_TABLE).getAll('bar', {index: 'sid'}) + + assert(Array.isArray(sessions)) + assert.equal(sessions[0].sid, 'bar') + assert.equal(sessions[0].wut, session.wut) + }) + + it('gets sessions', function* () { + var rs = makeRS() + var session = { sid: 'butts', yolo: 'swaggins' } + yield connection.db(TEST_DB).table(TEST_TABLE).insert(session) + var res = yield rs.get(session.sid) + assert.equal(res.yolo, session.yolo) + assert.equal(res.sid, session.sid) + }) + + it('deletes sessions', function* () { + var rs = makeRS() + var session = { deleteTest: 'okay' } + var sid = 'stuff' + yield rs.set(sid, session) + var sessions = yield connection.db(TEST_DB).table(TEST_TABLE).getAll(sid, {index: 'sid'}) + assert.equal(sessions.length, 1) + yield rs.destroy(sid) + sessions = yield connection.db(TEST_DB).table(TEST_TABLE).getAll(sid, {index: 'sid'}) + assert.equal(sessions.length, 0) + }) +})