<commit_before>'use strict';

export default {
  load,
};

/**
 * Module dependencies.
 */

import Sequelize from 'sequelize';
import capitalize from 'capitalize';
import { forEach } from 'lodash';

const debug = require('debug')('identity-desk:database');

/**
 * Create a database object and load models
 * 
 * @param {Object|string} options 
 * @param {Object} models 
 * @return {Object}
 */
function load(options, models) {
  return new Database(options, models);
}

class Database {
  /**
   * Create a database object and load models
   *
   * @param {Object|string} options Sequelize database options
   * @param {Object} models
   */
  constructor(options, models) {
    this._models = models; // hang on to the definitions
    this.connection = new Sequelize(options);
    this.models = {};

    forEach(this._models, (model, name) => this._define(name, model));
    this._afterDefine();
  }

  _afterDefine() {
    Object.keys(this._models).forEach(name => {
      const Model = this._models[name];
      if (Model.define) {
        // Model.define is where class and instance methods may be defined
        Model.define(this.connection.models(name));
        debug(`ran custom definitions for model ${capitalize(name)}`);
      }
      if (Model.associate) {
        // Model.associate is where associations may be defined
        Model.associate(this.models);
        debug(`created associations defined by model ${capitalize(name)}`);
      }
    });
  }

  _define(name, { attributes = () => {}, options = {} }) {
    const Model = this.connection.define(name, attributes(Sequelize), options);
    this.models[capitalize(name)] = Model;
    debug(`defined model ${capitalize(name)}`);
  }

  async init() {
    await this.connection.authenticate();
    await this.connection.sync();
  }<commit_msg>Fix bug in model definition logic<commit_after>'use strict';

export default {
  load,
};

/**
 * Module dependencies.
 */

import Sequelize from 'sequelize';
import capitalize from 'capitalize';
import { forEach } from 'lodash';

const debug = require('debug')('identity-desk:database');

/**
 * Create a database object and load models
 * 
 * @param {Object|string} options 
 * @param {Object} models 
 * @return {Object}
 */
function load(options, models) {
  return new Database(options, models);
}

class Database {
  /**
   * Create a database object and load models
   *
   * @param {Object|string} options Sequelize database options
   * @param {Object} models
   */
  constructor(options, models) {
    this._models = models; // hang on to the definitions
    this.connection = new Sequelize(options);
    this.models = {};

    forEach(this._models, (model, name) => this._define(name, model));
    this._afterDefine();
  }

  _afterDefine() {
    Object.keys(this._models).forEach(name => {
      const Model = this._models[name];
      if (Model.define) {
        // Model.define is where class and instance methods may be defined
        Model.define(this.connection.models[name]);
        debug(`ran custom definitions for model ${capitalize(name)}`);
      }
      if (Model.associate) {
        // Model.associate is where associations may be defined
        Model.associate(this.models);
        debug(`created associations defined by model ${capitalize(name)}`);
      }
    });
  }

  _define(name, { attributes = () => {}, options = {} }) {
    const Model = this.connection.define(name, attributes(Sequelize), options);
    this.models[capitalize(name)] = Model;
    debug(`defined model ${capitalize(name)}`);
  }

  async init() {
    await this.connection.authenticate();
    await this.connection.sync();
  }
{'fraction_non_alphanumeric': 0.1387822270981898, 'fraction_numerical': 0.0, 'mean_word_length': 3.3007075471698113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6233741', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1040, 'n_words': 285}