description and source-codeexpress-mysql-session = function (session) {
var constructorArgs;
if (typeof session.Store === 'undefined') {
session = require('express-session');
constructorArgs = Array.prototype.slice.call(arguments);
}
var Store = session.Store;
var MySQLStore = function(options, connection, cb) {
debug_log('Creating session store');
this.options = this.clone(options || {});
this.setDefaultOptions();
if (this.options.debug) {
deprecate('The \'debug\' option has been removed. This module now uses the debug module to output logs and error messages. Run
your app with `DEBUG=express-mysql-session* node your-app.js` to have all logs and errors outputted to the console.');
}
if (typeof connection === 'function') {
cb = connection;
connection = null;
}
this.connection = connection || mysql.createPool(this.options);
var done = function() {
this.setExpirationInterval();
if (cb) {
cb.apply(undefined, arguments);
}
}.bind(this);
if (!this.options.createDatabaseTable) {
setTimeout(done, 0);
return;
}
this.createDatabaseTable(done);
};
util.inherits(MySQLStore, Store);
MySQLStore.prototype.setDefaultOptions = function() {
debug_log('Setting default options');
this.options = this.defaults(this.options, defaultOptions, { recursive: true });
};
MySQLStore.prototype.createDatabaseTable = function(cb) {
debug_log('Creating sessions database table');
var fs = require('fs');
fs.readFile(__dirname + '/../schema.sql', 'utf-8', function(error, sql) {
if (error) {
return cb && cb(error);
}
sql = sql.replace(/`[^`]+`/g, '??');
var params = [
this.options.schema.tableName,
this.options.schema.columnNames.session_id,
this.options.schema.columnNames.expires,
this.options.schema.columnNames.data,
this.options.schema.columnNames.session_id
];
this.connection.query(sql, params, function(error) {
if (error) {
debug_error('Failed to create sessions database table.');
debug_error(error);
return cb && cb(error);
}
cb && cb();
});
}.bind(this));
};
MySQLStore.prototype.get = function(session_id, cb) {
debug_log('Getting session: ' + session_id);
var sql = 'SELECT ?? AS data FROM ?? WHERE ?? = ? LIMIT 1';
var params = [
this.options.schema.columnNames.data,
this.options.schema.tableName,
this.options.schema.columnNames.session_id,
session_id
];
this.connection.query(sql, params, function(error, rows) {
if (error) {
debug_error('Failed to get session.');
debug_error(error);
return cb(error, null);
}
try {
var session = rows[0] ? JSON.parse(rows[0].data) : null;
} catch (error) {
debug_error(error);
return cb(new Error('Failed to parse data for session: ' + session_id));
}
cb(null, session);
});
};
MySQLStore.prototype.set = function(session_id, data, cb) {
debug_log('Setting session: ' + session_id);
var expires;
if (data.cookie) {
if (data.cookie.expires) {
expires = data.cookie.expires;
} else if (data.cookie._expires) {
expires = data.cookie._expires;
}
}
if (!expires) {
expires = Date.now() + this.options.expiration;
}
if (!(expires instanceof Date)) {
expires = new Date(expires);
}
// Use whole seconds here; not milliseconds.
expires = Math.round(expires.getTime() / 1000);
data = JSON.stringify(data);
var sql = 'INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE ?? = VALUES(??), ?? = VALUES(??)';
var params = [
this.options.schema.tableName,
this.options.schema.columnNames.session_id,
this.options.schema.columnNames.expires,
this.options.schema.columnNames.data,
session_id,
expires,
data,
this.options.schema.columnNames.expires,
this.options.schema.columnNames.expires,
this.options.schema.columnNames.data,
this.options.schema.columnNames.data
];
this.connection.query(sql, params, function(error) {
if (error) {
debug_error('Failed to insert session data.');
debug_error(error);
return cb && cb(error);
} ...