<commit_before>import _ from 'lodash';
import {foreignKeyRelationshipName} from './foreign_key_name';

export const READ = 'READ';
export const WRITE = 'WRITE';

const buildInclude = (mode, model, db, models, exclude) => {
  const include = db.relationships.map(rel => {
    if (_.find(exclude, e => e === rel.model || e === rel.references)) {
      return null;
    }

    if (mode === READ && !rel.includeInSelect) {
      return null;
    }

    if (mode === WRITE && !rel.includeInWrite) {
      return null;
    }

    if (rel.model === model.name) {
      const related = models[rel.references];
      return {
        as: foreignKeyRelationshipName(rel),
        model: related,
        include: buildInclude(mode, related, db, models, [...exclude, model.name]),
      };
    }

    if (rel.references === model.name) {
      const related = models[rel.model];
      return {
        as: foreignKeyRelationshipName(rel),
        model: related,
        include: buildInclude(mode, related, db, models, [...exclude, model.name]),
      };
    }

    return null;
  });

  return _.filter(include, m => !_.isNil(m));
};

export const buildOptions = (mode, model, db, models) => {
  const include = buildInclude(mode, model, db, models, []);

  return {include};
};<commit_msg>Support for additional includes config<commit_after>import _ from 'lodash';
import {MANY_TO_ONE, MANY_TO_MANY} from 'node-bits';
import {foreignKeyRelationshipName} from './foreign_key_name';

export const READ = 'READ';
export const WRITE = 'WRITE';

const buildInclude = (mode, model, db, models, exclude) => {
  const include = db.relationships.map(rel => {
    if (_.find(exclude, e => e === rel.model || e === rel.references)) {
      return null;
    }

    let currentConfig = mode === WRITE
      ? rel.includeInWrite
      : rel.includeInSelect;

    if (!currentConfig) {
      return null;
    } else if (currentConfig === true) {
      currentConfig = {
        model: true,
        reverse: true,
        separate: false,
      };
    }
    if (rel.model === model.name && currentConfig.model) {
      const related = models[rel.references];
      return {
        as: foreignKeyRelationshipName(rel),
        model: related,
        include: buildInclude(mode, related, db, models, [...exclude, model.name]),
        separate: currentConfig.separate && (rel.type === MANY_TO_MANY || rel.type === MANY_TO_ONE),
      };
    }

    if (rel.references === model.name && currentConfig.reverse) {
      const related = models[rel.model];
      return {
        as: foreignKeyRelationshipName(rel),
        model: related,
        include: buildInclude(mode, related, db, models, [...exclude, model.name]),
        separate: currentConfig.separate && (rel.type === MANY_TO_MANY || rel.type === MANY_TO_ONE),
      };
    }

    return null;
  });

  return _.filter(include, m => !_.isNil(m));
};

export const buildOptions = (mode, model, db, models) => {
  const include = buildInclude(mode, model, db, models, []);

  return {include};
};
{'fraction_non_alphanumeric': 0.15016501650165018, 'fraction_numerical': 0.0, 'mean_word_length': 2.8513341804320205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4521843', 'n_tokens_mistral': 941, 'n_tokens_neox': 920, 'n_words': 238}