{"size": 1545, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "midgarjs/db-session", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "midgarjs/db-session", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "midgarjs/db-session", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1052631579, "max_line_length": 120, "alphanum_fraction": 0.6187702265}
const session = require('express-session')
const SequelizeStore = require('connect-session-sequelize')(session.Store)

const Plugin = require('@midgar/midgar/plugin')

/**
 * MidgarSequelizeSession plugin
 * 
 * Add sequelize session storage
 */
class MidgarSequelizeSession extends Plugin {
  /**
   * Init plugin
   */
  async init() {
    this.pm.on('afterLoadPlugins', () => {
      return this._afterLoadPlugins()
    })
  }

  /**
   * afterLoadPlugins callback
   * 
   * Add the sequelize session store
   */
  async _afterLoadPlugins() {
    //get session plugin
    const sessionPlugin = this.pm.plugins['@midgar/session']

    //add sequelise store
    await sessionPlugin.addStore('sequelize', async (midgar) => {
      //if the db is install return sequelize store
      if (midgar.services.db.installed) {

        return new SequelizeStore({
          db: midgar.services.db,
          table: 'session',
          checkExpirationInterval: 15 * 60 * 1000, // The interval at which to cleanup expired sessions in milliseconds.
          expiration: 24 * 60 * 60 * 1000,  // The maximum age (in milliseconds) of a valid session.
          extendDefaultFields: function (defaults, session) {
            return {
              data: defaults.data,
              expires: defaults.expires,
              user_id: session.user ? session.user.id : null,
            }
          },
        })
        //else return null to use default store
      } else {
        return null
      }
    })
  }
}

module.exports = MidgarSequelizeSession
