{"size": 2272, "ext": "js", "max_stars_repo_path": "lib/db.js", "max_stars_repo_name": "webcredits/wc_db", "max_stars_repo_stars_event_min_datetime": "2016-06-01T07:11:40.000Z", "max_stars_repo_stars_event_max_datetime": "2016-06-01T07:11:40.000Z", "max_issues_repo_path": "lib/db.js", "max_issues_repo_name": "webcredits/wc_db", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/db.js", "max_forks_repo_name": "webcredits/wc_db", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.049382716, "max_line_length": 82, "alphanum_fraction": 0.6329225352}
module.exports = {
  getConnection: getConnection,
  runSQL: runSQL
}

// Requires
var Sequelize = require('sequelize')

/**
* Setup database.
* @param  {Object} config          The config object.
* @param  {string} config.dialect  The db dialect sqlite | mysql.
* @param  {string} config.host     The db host.
* @param  {string} config.database The db database name.
* @param  {string} config.username The db username.
* @param  {string} config.password The db password.
* @return {Object}                 Sequelize db object.
*/
function getConnection (config) {
  var sequelize
  var defaultStorage = 'store.db'
  var logging = config.logging || false

  if (config.dialect === 'sqlite') {
    if (!config.storage) {
      config.storage = defaultStorage
    }

    sequelize = new Sequelize(config.database, config.username, config.password, {
      host: config.host,
      dialect: config.dialect,
      storage: config.storage,
      logging: logging
    })
  } else {
    sequelize = new Sequelize(config.database, config.username, config.password, {
      host: config.host,
      dialect: config.dialect,
      logging: logging
    })
  }
  return sequelize
}

/**
 * Run SQL as promise
 * @param  {string} sql          The SQL to run
 * @param  {Object} config          The config object.
 * @param  {string} config.dialect  The db dialect sqlite | mysql.
 * @param  {string} config.host     The db host.
 * @param  {string} config.database The db database name.
 * @param  {string} config.username The db username.
 * @param  {string} config.password The db password.
 * @param  {Object} conn            Sequelize connection.
 * @param  {Object} replacements    Possiblle replacements.
 * @return {Object}                 Promise
 */
function runSQL (sql, config, conn, replacements) {
  return new Promise(function (resolve, reject) {
    if (!config && !conn) {
      reject('Must set config or connection')
    }

    if (!sql) {
      reject('Need some sql')
    }

    if (!conn) {
      conn = getConnection(config)
    }

    replacements = replacements || {}

    conn.query(sql, { replacements: replacements }).then(function (ret) {
      return resolve({'ret': ret, 'conn': conn})
    }).catch(function (err) {
      return reject(err)
    })
  })
}
