all files / lib/offshore/utils/ getRelations.js

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 11/11
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                        694× 694×   694× 10330× 10330×   1137× 3411× 2274× 45×       694×    
/**
 * getRelations
 *
 * Find any `junctionTables` that reference the parent collection.
 *
 * @param  {[type]} options [description]
 *    @option parentCollection
 *    @option schema
 * @return {[type]}         [relations]
 */
 
module.exports = function getRelations(options) {
 
  var schema = options.schema;
  var relations = [];
 
  Object.keys(schema).forEach(function(collection) {
    var collectionSchema = schema[collection];
    if (!collectionSchema.hasOwnProperty('junctionTable')) return;
 
    Object.keys(collectionSchema.attributes).forEach(function(key) {
      if (!collectionSchema.attributes[key].hasOwnProperty('foreignKey')) return;
      if (collectionSchema.attributes[key].references !== options.parentCollection) return;
      relations.push(collection);
    });
  });
 
  return relations;
};