<commit_before>var Promise = require('bluebird');
var _ = require('lodash');

Krypton.Relation.HasManyThrough = Class(Krypton.Relation, 'HasManyThrough').inherits(Krypton.Relation)({
  prototype : {
    fetch : function(records) {
      var relation = this;

      var promises = _.map(records, function(record) {
        var joinQuery;

        if (relation.knex) {
          joinQuery = relation.knex.table(relation.ownerModel.tableName);
        } else {
          joinQuery = relation.ownerModel.knex().table(relation.ownerModel.tableName);
        }

        var relatedQuery = relation.relatedModel.query(relation.knex);

        joinQuery
          .select(relation.ownerModel.tableName + '.' + relation.ownerCol,
            relation.through.tableName + '.' + relation.through.relatedCol)
          .leftOuterJoin(relation.through.tableName,
            relation.through.tableName + '.' + relation.through.ownerCol,
            relation.ownerModel.tableName + '.' + relation.ownerCol)
          .where(relation.ownerModel.tableName + '.' + relation.ownerCol, '=', record[relation.ownerCol]);

        if (relation.through.scope) {
          joinQuery.andWhere.apply(joinQuery, relation.through.scope);
        }

        joinQuery.then(function(joinResults) {
            var relatedIds = joinResults.map(function(item) {
              return item[relation.through.relatedCol];
            });

            relatedQuery.whereIn(relation.relatedCol, relatedIds)

            if (relation.scope) {
              relatedQuery.andWhere.apply(relatedQuery, relation.scope);
            }
          });

        return joinQuery.then(function() {
          return relatedQuery.then(function(results) {
            record[relation.name] = results;
          });
        })
      });

      return Promise.all(promises).then(function() {
        return _.map(records, function(item) {
          return item[relation.name];
        })[0];
      });
    }
  }
});

module.exports = Krypton.Relation.HasMany;<commit_msg>Fix race condition in HasManyThrough<commit_after>var Promise = require('bluebird');
var _ = require('lodash');

Krypton.Relation.HasManyThrough = Class(Krypton.Relation, 'HasManyThrough').inherits(Krypton.Relation)({
  prototype : {
    fetch : function(records) {
      var relation = this;

      var promises = _.map(records, function(record) {
        var joinQuery;

        if (relation.knex) {
          joinQuery = relation.knex.table(relation.ownerModel.tableName);
        } else {
          joinQuery = relation.ownerModel.knex().table(relation.ownerModel.tableName);
        }

        var relatedQuery = relation.relatedModel.query(relation.knex);

        joinQuery
          .select(relation.ownerModel.tableName + '.' + relation.ownerCol,
            relation.through.tableName + '.' + relation.through.relatedCol)
          .leftOuterJoin(relation.through.tableName,
            relation.through.tableName + '.' + relation.through.ownerCol,
            relation.ownerModel.tableName + '.' + relation.ownerCol)
          .where(relation.ownerModel.tableName + '.' + relation.ownerCol, '=', record[relation.ownerCol]);

        if (relation.through.scope) {
          joinQuery.andWhere.apply(joinQuery, relation.through.scope);
        }

        return joinQuery
          .then(function(joinResults) {
            var relatedIds = joinResults.map(function(item) {
              return item[relation.through.relatedCol];
            });

            relatedQuery.whereIn(relation.relatedCol, relatedIds)

            if (relation.scope) {
              relatedQuery.andWhere.apply(relatedQuery, relation.scope);
            }
          })
          .then(function() {
            return relatedQuery.then(function(results) {
              record[relation.name] = results;
            });
          });
      });

      return Promise.all(promises).then(function() {
        return _.map(records, function(item) {
          return item[relation.name];
        })[0];
      });
    }
  }
});

module.exports = Krypton.Relation.HasMany;
{'fraction_non_alphanumeric': 0.12586037364798427, 'fraction_numerical': 0.0004916420845624386, 'mean_word_length': 2.3379819524200163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '347455', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1105, 'n_words': 154}