{"size": 1359, "ext": "js", "max_stars_repo_path": "client/base.js", "max_stars_repo_name": "zalando-incubator/roadblock", "max_stars_repo_stars_event_min_datetime": "2019-01-24T16:00:15.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-21T08:58:07.000Z", "max_issues_repo_path": "client/base.js", "max_issues_repo_name": "zalando-incubator/roadblock", "max_issues_repo_issues_event_min_datetime": "2018-05-24T09:12:28.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-15T19:22:12.000Z", "max_forks_repo_path": "client/base.js", "max_forks_repo_name": "zalando-incubator/roadblock", "max_forks_repo_forks_event_min_datetime": "2018-05-24T08:55:29.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-03T15:28:34.000Z"}
{"max_stars_count": 13, "max_issues_count": 22, "max_forks_count": 5, "avg_line_length": 22.2786885246, "max_line_length": 77, "alphanum_fraction": 0.6490066225}
const helper = require('./helper.js');
const mapper = require('object-mapper');

module.exports = class Base {
  constructor(githubClient, databaseClient) {
    this.ghClient = githubClient;
    this.dbClient = databaseClient;

    this.dbConfig = {
      underscored: true,
      timestamps: false,
      freezeTableName: true
    };
  }

  // Defines the model and schema in the sequelize singleton
  define() {
    this.model = this.dbClient.define(this.name, this.schema, this.dbConfig);
  }

  // syncs the schema and any of the intertable relations
  sync(force) {
    this.model.sync(force);
  }

  // generic repository delete statement
  async destroy(id, where = { where: { repository_id: id } }) {
    try {
      return await this.model.destroy(where);
    } catch (ex) {
      console.log(this.name + ' truncating failed: ' + ex);
    }
  }

  async bulkCreate(valArray, externalValues) {
    const dbValArray = this.map
      ? helper.mapArray(valArray, this.map, externalValues)
      : valArray;
    await this.model.bulkCreate(dbValArray);
    return dbValArray;
  }

  async saveOrUpdate(value) {
    const dbValue = mapper(value, this.map);
    await this.model.upsert(dbValue);
    return dbValue;
  }

  getSchema() {
    return this.schema;
  }

  getMapper() {
    return this.mapper;
  }

  getModel() {
    return this.mapper;
  }
};
