-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1490a9f
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> (http://jamisondance.com)", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"co-mocha": "1.1.0", | ||
"rethinkdbdash": "1.16.13" | ||
}, | ||
"dependencies": { | ||
"lodash": "3.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |