all files / lib/offshore/query/ deepCursor.js

94.87% Statements 74/78
82.22% Branches 37/45
100% Functions 9/9
94.87% Lines 74/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                  42× 42× 42× 18× 18× 18×       18× 18×   18× 10× 60×   12× 12× 22× 22× 22×                         10× 68×   10×                     98× 98× 44×     54× 24×   54× 44×   10×       54× 24×   54×     44× 54×       24× 24× 24× 24× 24×     24× 44× 44×   44× 44×   328× 53× 53× 14×   53× 66× 66× 66×               24×     24× 24× 24× 24× 24×     42×                            
/**
*
*         ROOT LEVEL
*      +--------------+
*      | ADAPTER EXEC |
*      +------+-------+
*             v
*      +--------------+
*      |  PARENTS     |
*      +--------------+   INDEX REFERENCES
*      |  CHILDREENS  +-------+ BY PATH/PK
*      +--------------+       |
*                             |
*       GET LEVEL n           v CURSOR
*       PARENTS PK       +----+----+
*       BY    +----------+  INDEX  |
*       PATH  |          +----+----+
*             v               ^
*      +------+-------+       |
*      | ADAPTER EXEC |       |
*      +------+-------+       |
*             v               |
*      +--------------+       |
*      |  PARENTS     |       |
*      +--------------|-------+
*      |  CHILDREENS  |  MERGE PARENTS IN REFERENCES
*      +--------------+              AND
*                        INDEX CHILDREENS BY PATH/PK
*
*/
 
 
var _ = require('lodash');
 
var DeepCursor = module.exports = function(path, data, paths) {
  this.path = path;
  this.paths = paths;
  if (data) {
    this.root = data;
    this.parents = {};
    this.deepIndex(this.root);
  }
};
 
DeepCursor.prototype.deepIndex = function(data) {
  var child;
  var alias;
  // many
  if (Array.isArray(data)) {
    for (var i in data) {
      for (alias in data[i]) {
        if (_.isObject(data[i][alias]) && !_.isDate(data[i][alias]) && this.paths[this.path].children[alias]) {
          // to many
          Eif (_.isArray(data[i][alias])) {
            for (j in data[i][alias]) {
              child = data[i][alias][j];
              Eif (!_.isFunction(child)) {
                this.index(alias, child[this.paths[this.path].children[alias].primaryKey], child);
              }
            }
          } else { // to one
            child = data[i][alias];
            if (child) {
              this.index(alias, child[this.paths[this.path].children[alias].primaryKey], child);
            }
          }
        }
      }
    }
  } else { // one
    for (alias in data) {
      if (_.isObject(data[alias]) && !_.isDate(data[alias]) && this.paths[this.path].children[alias]) {
        // to many
        if (_.isArray(data[alias])) {
          for (var j in data[alias]) {
            child = data[alias][j];
            Eif (!_.isFunction(child)) {
              this.index(alias, child[this.paths[this.path].children[alias].primaryKey], child);
            }
          }
        } else { // to one
          child = data[alias];
          Eif (child) {
            this.index(alias, child[this.paths[this.path].children[alias].primaryKey], child);
          }
        }
      }
    }
  }
};
 
DeepCursor.prototype.index = function(alias, pk, dest) {
  var path = this.path + '.' + alias;
  if (!this.paths[path]) {
    return;
  }
 
  if (!this.paths[path].refs) {
    this.paths[path].refs = {};
  }
  if (!this.paths[path].refs[pk]) {
    this.paths[path].refs[pk] = [dest];
  } else {
    this.paths[path].refs[pk].push(dest);
  }
 
  // add to path parents
  if (!this.parents[path]) {
    this.parents[path] = [];
  }
  this.parents[path].push(pk);
};
 
DeepCursor.prototype.extend = function(pk, object) {
  this.paths[this.path].refs[pk].forEach(function(ref) {
    _.extend(ref, object);
  });
};
 
DeepCursor.prototype.zip = function(data) {
  var currentAlias = this.path.substring(this.path.lastIndexOf('.') + 1, this.path.length);
  var previousPath = this.path.substring(0, this.path.lastIndexOf('.'));
  var parentPk = this.paths[previousPath].children[currentAlias].primaryKey;
  var parents = data;
  Iif (!Array.isArray(parents)) {
    parents = [parents];
  }
  for (var parentIterator in parents) {
    var parentData = data[parentIterator];
    var parentPkVal = parentData[parentPk];
    // insert new parents' data in previous child
    this.extend(parentPkVal, parentData);
    for (var attribute in parentData) {
      // if attribute is an association then index childs
      if (this.paths[this.path].children[attribute]) {
        var childs = parentData[attribute];
        if (childs && _.isObject(childs) && !_.isArray(childs)) {
          childs = [childs];
        }
        for (var childIterator in childs) {
          var child = childs[childIterator];
          Eif (!_.isFunction(child)) {
            this.index(attribute, child[this.paths[this.path].children[attribute].primaryKey], child);
          }
        }
      }
    }
  }
};
 
DeepCursor.prototype.getParents = function() {
  return this.parents[this.path];
};
 
DeepCursor.prototype.getChildPath = function(path) {
  var pathCursor = new DeepCursor(path);
  pathCursor.paths = this.paths;
  pathCursor.parents = this.parents;
  pathCursor.root = this.root;
  return pathCursor;
};
 
DeepCursor.prototype.getRoot = function() {
  return this.root;
};
 
/**
 *                    CURSOR
 *         +------------|-----------------+
 *        /  ROOT LEVEL v                  \
 *       +/\/ /\/ /\/####HHH/ /\/ /\/ /\/ /\+
 *      /-MERGED-----###c***O)               \
 *     +/\ \/\ \/\ \/####HHH -----------------+
 *    /  INDEXED     LEVEL n ZIPING PATH --->
 *   +/\ \/\ \/\ \/\ \/\ \/\ -------------------+
 *
 */