{"size": 2177, "ext": "js", "max_stars_repo_path": "src/query/Query.js", "max_stars_repo_name": "coderich/autograph", "max_stars_repo_stars_event_min_datetime": "2019-12-11T04:20:41.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-22T19:49:43.000Z", "max_issues_repo_path": "src/query/Query.js", "max_issues_repo_name": "coderich/dalmatian", "max_issues_repo_issues_event_min_datetime": "2020-01-18T12:17:51.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T22:09:20.000Z", "max_forks_repo_path": "src/query/Query.js", "max_forks_repo_name": "coderich/dalmatian", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 21.3431372549, "max_line_length": 126, "alphanum_fraction": 0.5884244373}
const { get, unset } = require('lodash');
const { keyPaths } = require('../service/app.service');

module.exports = class Query {
  constructor(resolver, model, query = {}) {
    const { where = {}, sortBy = {}, limit, pagination = {}, options = {} } = query;

    // The fields to sortBy should be $hydrated fields
    const sortFields = keyPaths(sortBy).reduce((prev, path) => {
      const $path = path.split('.').map(s => (s.indexOf('count') === 0 || s.indexOf('.count') === 0 ? s : `$${s}`)).join('.');
      return Object.assign(prev, { [$path]: get(sortBy, path) });
    }, {});

    // Counting
    const countPaths = keyPaths(where).filter(p => p.indexOf('count') === 0 || p.indexOf('.count') > 0);
    const countFields = countPaths.reduce((prev, path) => Object.assign(prev, { [path]: get(where, path) }), {});
    countPaths.forEach(p => unset(where, p));

    //
    this.resolver = resolver;
    this.query = query;
    this.model = model;
    this.countFields = countFields;
    this.countPaths = countPaths;
    this.sortFields = sortFields;
    this.where = where;
    this.sortBy = sortBy;
    this.pagination = pagination;
    this.limit = limit;
    this.options = options;
  }

  getId() {
    return this.query.id;
  }

  getMeta() {
    return this.query.meta;
  }

  getWhere() {
    return this.where;
  }

  getSortBy() {
    return this.sortBy;
  }

  getSortFields() {
    return this.sortFields;
  }

  getCountFields() {
    return this.countFields;
  }

  getCountPaths() {
    return this.countPaths;
  }

  getPagination() {
    return this.pagination;
  }

  getLimit() {
    return this.limit;
  }

  getOptions() {
    return this.options;
  }

  getModel() {
    return this.model;
  }

  getQuery() {
    return this.query;
  }

  getNative() {
    return this.query.native;
  }

  isNative() {
    return Boolean(this.query.native);
  }

  getCacheKey() {
    return {
      // ...this.query,
      id: this.query.id,
      where: this.query.where,
      limit: this.query.limit,
      native: this.query.native,
      sortBy: this.query.sortBy,
      countFields: this.countFields,
      pagination: this.query.pagination,
    };
  }
};
