<commit_before>var Utils = require('./utils');

var Cursor = function(tableName, connection, joins) {
  var self = this;
  this.fieldAlias = {};
  this.pk = connection.getPk(tableName);
  this.connection = connection;
  joins.forEach(function(join) {
    if (join.select) {
      var childPk = connection.getPk(join.child);
      var childPkAlias = connection.dialect.createAlias(join.alias, childPk);
      var definition = connection.getCollection(join.child).definition;
      var parentKey = join.parentKey;
      if (join.collection) {
        parentKey = join.alias;
      }
      join.select.forEach(function(columnName) {
        if (definition[columnName]) {
          var childAlias = connection.dialect.createAlias(join.alias, columnName);
          self.fieldAlias[childAlias] = {columnName: columnName, childPk: childPkAlias, parentKey: parentKey, collection: join.collection, childName: join.alias, type: definition[columnName].type};
        }
      });
    }
  });
};

Cursor.prototype.process = function(dbResults) {
  var self = this;
  var results = [];
  var index = {};
  dbResults.forEach(function(result) {
    var childrenPk = {};
    var parentPk = result[self.pk];
    for (var field in result) {
      if (self.fieldAlias[field]) {
        var childName = self.fieldAlias[field].childName;
        var parentKey = self.fieldAlias[field].parentKey;
        var collection = self.fieldAlias[field].collection;
        var childColumnName = self.fieldAlias[field].columnName;
        var childPkName = self.fieldAlias[field].childPk;
        var type = self.fieldAlias[field].type;

        //first time before result child pk is delete
        if (! childrenPk[childName]) {
          childrenPk[childName] = result[childPkName];
        }
        var childPk = childrenPk[childName];


        //create parent index and add it to results
        if (!index[parentPk]) {
          index[parentPk] = {
            data: result,
            children: {}
          };
          results.push(index[parentPk].data);
        }

        //create association index and empty collection data
        if (! index[parentPk].children[childName]) {
          index[parentPk].children[childName] = {};
          if (collection) {
            index[parentPk].data[childName] = [];
          }
        }

        //create child index
        if (childPk && ! index[parentPk].children[childName][childPk]) {
          index[parentPk].children[childName][childPk] = {};
          if (collection) {
            index[parentPk].data[parentKey].push(index[parentPk].children[childName][childPk]);
          }
          else {
            index[parentPk].data[parentKey] = index[parentPk].children[childName][childPk];
          }
        }
        //insert child column data
        if (childPk) {
          var value = Utils.cast(type,result[field]);
          index[parentPk].children[childName][childPk][childColumnName] = value;
        }
        if (collection && parentKey === field) {
          continue;
        }
        if (result[field] === null) {
          delete result[field];
        }
        if (! collection && field !== parentKey) {
          delete result[field];
        }
        continue;
      }
    }
  });
  return results;
};

module.exports = Cursor;<commit_msg>fix empty belongsTo child and alias key in parent data<commit_after>var Utils = require('./utils');

var Cursor = function(tableName, connection, joins) {
  var self = this;
  this.fieldAlias = {};
  this.pk = connection.getPk(tableName);
  this.definition = connection.getCollection(tableName).definition;
  this.connection = connection;
  joins.forEach(function(join) {
    if (join.select) {
      var childPk = connection.getPk(join.child);
      var childPkAlias = connection.dialect.createAlias(join.alias, childPk);
      var definition = connection.getCollection(join.child).definition;
      var parentKey = join.parentKey;
      if (join.collection) {
        parentKey = join.alias;
      }
      join.select.forEach(function(columnName) {
        if (definition[columnName]) {
          var childAlias = connection.dialect.createAlias(join.alias, columnName);
          self.fieldAlias[childAlias] = {
            columnName: columnName,
            childPk: childPkAlias,
            parentKey: parentKey,
            collection: join.collection,
            childName: join.alias,
            type: definition[columnName].type
          };
        }
      });
    }
  });
};

Cursor.prototype.process = function(dbResults) {
  var self = this;
  var results = [];
  var index = {};
  dbResults.forEach(function(result) {
    var childrenPk = {};
    var parentPk = result[self.pk];
    for (var field in result) {
      if (self.fieldAlias[field]) {
        var childName = self.fieldAlias[field].childName;
        var parentKey = self.fieldAlias[field].parentKey;
        var collection = self.fieldAlias[field].collection;
        var childColumnName = self.fieldAlias[field].columnName;
        var childPkName = self.fieldAlias[field].childPk;
        var childPk = result[childPkName];
        var type = self.fieldAlias[field].type;

        //create parent index and add it to results
        if (!index[parentPk]) {
          // create a parent instance
          var parent = {};
          _.keys(result).forEach(function(columnName) {
            var def = self.definition[columnName];
            if (def && !(def.model || def.collection)) {
              parent[columnName] = result[columnName];
            }
          });
          index[parentPk] = {
            data: parent,
            children: {}
          };
          results.push(index[parentPk].data);
        }

        //create association index and empty collection data
        if (! index[parentPk].children[childName]) {
          if (collection) {
            index[parentPk].data[childName] = [];
          } else if (childPk) {
            index[parentPk].children[childName] = {};
          }
        }

        //create child index
        if (childPk && ! index[parentPk].children[childName][childPk]) {
          index[parentPk].children[childName][childPk] = {};
          if (collection) {
            index[parentPk].data[parentKey].push(index[parentPk].children[childName][childPk]);
          } else if (childPk) {
            index[parentPk].data[parentKey] = index[parentPk].children[childName][childPk];
          }
        }
        //insert child column data
        if (childPk) {
          var value = Utils.cast(type,result[field]);
          index[parentPk].children[childName][childPk][childColumnName] = value;
        }
      }
    }
  });
  return results;
};

module.exports = Cursor;
{'fraction_non_alphanumeric': 0.12462686567164179, 'fraction_numerical': 0.0, 'mean_word_length': 2.333830845771144, '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': '6762161', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1915, 'n_words': 370}