chill117 / express-mysql-session

A MySQL session store for the express framework in node
MIT License
313 stars 109 forks source link

Issue with defaults method (options) #68

Closed dom19191 closed 7 years ago

dom19191 commented 7 years ago

I found a correction... for the next release!

File >>>  /lib/index.js

MySQLStore.prototype.defaults = function(object, defaultValues, options) {

        if (!this.isObject(object)) {
            return object;
        }

        object = this.clone(object);
        defaultValues = defaultValues || {};
        options = options || {};
        options.recursive = options.recursive === true;

        for (var key in defaultValues) {

            if (defaultValues.hasOwnProperty(key)) /// -----> ADD THAT !!!
            {
                if (typeof object[key] === 'undefined') {
                    object[key] = defaultValues[key];
                }

                if (options.recursive) {
                    object[key] = this.defaults(object[key], defaultValues[key], options);
                }
            }
        }

        return object;
    }
chill117 commented 7 years ago

What's the problem this fixes?

dom19191 commented 7 years ago

Go over all the internal properties of the object and they are not necessary in this scoop. Else this kind of configuration doesn't work... the previous modification do the job.

var website = require('./db_website.js');
var session_store = {};

module.exports = {

    load: function (app) 
    {   
        var express_session             = require('express-session');
        var express_mysql_session  = require('express-mysql-session');
        var express_session_store    = express_mysql_session(express_session);

        var db_website_session_config = {
                host: website.get('app_db_host'),
                port: 3306,
                user: website.get('app_db_user'),
                password: website.get('app_db_password'),
                database: website.get('app_db_database'),
                schema: {
                tableName: 'sessions',
                columnNames: {
                    session_id: 'session_id',
                    expires: 'expires',
                    data: 'data'
                }},
                checkExpirationInterval: 900000,// How frequently expired sessions will be cleared; milliseconds. 
                expiration: 86400000,// The maximum age of a valid session; milliseconds. 
                connectionLimit: 1// Number of connections when creating a connection pool          
            };  

        session_store = new express_session_store(db_website_session_config);   

        app.use(express_session({
            key: '55555-55555-55555',
            secret: '66666-66666-66666',
            store: session_store,
            resave: false,
            saveUninitialized: false,
            cookie: { secure: true }
        }));
        return true;
    }   
}
chill117 commented 7 years ago

Maybe the solution is to use the _.defaults function from the underscore utility library. Fewer things to break/test in this module is better.

I won't have time for a while. So pull requests are welcome :)

chill117 commented 7 years ago

I've added underscore to replace several methods used internally. An instance of express-mysql-session will now have its default options set using the _.defaults method with the schema option being the only one set recursively.