Skip to content

Commit

Permalink
- re-wrote unit tests with Mocha and Should.js
Browse files Browse the repository at this point in the history
    - added option param for db file name (seperate from table name)
    - updated readme
  • Loading branch information
rawberg committed Sep 12, 2012
1 parent 648b36f commit 16bfc33
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 194 deletions.
12 changes: 12 additions & 0 deletions History.md
@@ -1,3 +1,15 @@
0.9.0 / 2012-09-11
==================
- re-wrote unit tests with Mocha and Should.js
- added option param for db file name (seperate from table name)
- updated readme

0.8.0 / 2012-09-10
==================
- forked previous lib
- swapped out sqlite lib for node-sqlite3
- made sure existing unit tests work

0.0.1 / 2011-09-24
==================

Expand Down
17 changes: 8 additions & 9 deletions Readme.md
@@ -1,23 +1,22 @@

# Connect SQLite
# Connect SQLite3

connect-sqlite is a SQLite session store, just copied connect-redis.

connect-sqlite support only connect `>= 1.4.0`.
connect-sqlite3 is a SQLite3 session store, modeled after connect-redis.

## Installation

$ npm install connect-sqlite
$ npm install connect-sqlite3

## Options

- `db='sessions'` Database file & table name
- `db='sessions'` Database table name
- `db= Database file name (defaults to table name)
- `dir='.'` Direcotry to save '<db>.db' file

## Usage

var connect = require('connect')
, SQLiteStore = require('connect-sqlite')(connect);
var connect = require('connect'),
SQLiteStore = require('connect-sqlite3')(connect);

connect.createServer(
connect.cookieParser(),
Expand All @@ -26,7 +25,7 @@ connect-sqlite is a SQLite session store, just copied connect-redis.

with express

var SQLiteStore = require('connect-sqlite')(express);
var SQLiteStore = require('connect-sqlite3')(express);

app.configure(function() {
app.set('views', __dirname + '/views');
Expand Down
249 changes: 127 additions & 122 deletions lib/connect-sqlite3.js
Expand Up @@ -9,8 +9,8 @@
* Module dependencies.
*/

var sqlite3 = require('sqlite3');
var events = require('events');
var sqlite3 = require('sqlite3'),
events = require('events');

/**
* One day in seconds.
Expand All @@ -28,134 +28,139 @@ var oneDay = 86400;

module.exports = function(connect) {

/**
* Connect's Store.
*/
/**
* Connect's Store.
*/

var Store = connect.session.Store;
var Store = connect.session.Store;

/**
* Initialize SQLiteStore with the given `options`.
*
* @param {Object} options
* @api public
*/
/**
* Initialize SQLiteStore with the given options.
*
* @param {Object} options
* @api public
*/

function SQLiteStore(options) {
options = options || {};
Store.call(this, options);
function SQLiteStore(options) {
options = options || {};
Store.call(this, options);

this.table = options.db || 'sessions';
var dbFile = (options.dir || '.') + '/' + this.table + '.db';
this.table = options.table || 'sessions';
this.db = options.db || this.table;
var dbPath;

this.db = new sqlite3.Database(dbFile);
this.client = new events.EventEmitter();
var self = this;
if(this.db !== ':memory:') {
dbPath = (options.dir || '.') + '/' + this.db + '.db';
} else {
dbPath = this.db;
}


this.db = new sqlite3.Database(dbPath);
this.client = new events.EventEmitter();
var self = this;

this.db.exec('CREATE TABLE IF NOT EXISTS ' + this.table + ' (' + 'sid PRIMARY KEY, ' + 'expired, sess)',
function(err) {
if (err) throw err;
self.client.emit('connect');
}
);
}
this.db.exec('CREATE TABLE IF NOT EXISTS ' + this.table + ' (' + 'sid PRIMARY KEY, ' + 'expired, sess)',
function(err) {
if (err) throw err;
self.client.emit('connect');
}
);
}

/**
* Inherit from `Store`.
*/

SQLiteStore.prototype.__proto__ = Store.prototype;

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.get = function(sid, fn) {
var now = new Date().getTime();
this.db.get('SELECT sess FROM ' + this.table + ' WHERE sid = ? AND ? <= expired', [sid, now],
function(err, row) {
if (err) fn(err);
fn(null, JSON.parse(row.sess));
}
);
};


/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.set = function(sid, sess, fn) {
try {
var maxAge = sess.cookie.maxAge;
var now = new Date().getTime();
var expired = maxAge ? now + maxAge : now + oneDay;
sess = JSON.stringify(sess);

this.db.all('INSERT OR REPLACE INTO ' + this.table + ' VALUES (?, ?, ?)',
[sid, expired, sess],
function(err, rows) {
if (fn) fn.apply(this, arguments);
/**
* Inherit from Store.
*/

SQLiteStore.prototype.__proto__ = Store.prototype;

/**
* Attempt to fetch session by the given sid.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.get = function(sid, fn) {
var now = new Date().getTime();
this.db.get('SELECT sess FROM ' + this.table + ' WHERE sid = ? AND ? <= expired', [sid, now],
function(err, row) {
if (err) fn(err);
fn(null, JSON.parse(row.sess));
}
);
};


/**
* Commit the given sess object associated with the given sid.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.set = function(sid, sess, fn) {
try {
var maxAge = sess.cookie.maxAge;
var now = new Date().getTime();
var expired = maxAge ? now + maxAge : now + oneDay;
sess = JSON.stringify(sess);

this.db.all('INSERT OR REPLACE INTO ' + this.table + ' VALUES (?, ?, ?)',
[sid, expired, sess],
function(err, rows) {
if (fn) fn.apply(this, arguments);
}
);
} catch (e) {
if (fn) fn(e);
}
);
} catch (e) {
if (fn) fn(e);
}
};


/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/

SQLiteStore.prototype.destroy = function(sid, fn) {
this.db.run('DELETE FROM ' + this.table + ' WHERE sid = ?', [sid], fn);
};


/**
* Fetch number of sessions.
*
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.length = function(fn) {
this.db.all('SELECT COUNT(*) AS count FROM ' + this.table + '', function(err, rows) {
if (err) fn(err);
fn(null, rows[0].count);
});
};


/**
* Clear all sessions.
*
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.clear = function(fn) {
this.db.exec('DELETE FROM ' + this.table + '', function(err) {
if (err) fn(err);
fn(null, true);
});
};

return SQLiteStore;
};


/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/

SQLiteStore.prototype.destroy = function(sid, fn) {
this.db.run('DELETE FROM ' + this.table + ' WHERE sid = ?', [sid], fn);
};


/**
* Fetch number of sessions.
*
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.length = function(fn) {
this.db.all('SELECT COUNT(*) AS count FROM ' + this.table + '', function(err, rows) {
if (err) fn(err);
fn(null, rows[0].count);
});
};


/**
* Clear all sessions.
*
* @param {Function} fn
* @api public
*/

SQLiteStore.prototype.clear = function(fn) {
this.db.exec('DELETE FROM ' + this.table + '', function(err) {
if (err) fn(err);
fn(null, true);
});
};

return SQLiteStore;

};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/rawberg/connect-sqlite3.git"
"url": "git://github.com/rawberg/connect-sqlite3.git"
},
"licenses": [
{
Expand Down
Empty file removed sessions
Empty file.
62 changes: 0 additions & 62 deletions test.js

This file was deleted.

0 comments on commit 16bfc33

Please sign in to comment.