All files / lib/model/nw/schema Association.js

100% Statements 18/18
100% Branches 6/6
100% Functions 10/10
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x 1x                                 11x 1x     11x 17x     20x 11x 25x     11x 1x                           14x                     10x                         3x       1x  
"use strict";
 
const _ = require("lodash");
const AssociationEnd = require("./AssociationEnd");
 
/**
 * Envelops an association.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/77d7ccbb-bda8-444a-a160-f4581172322f
 * (not present in OASIS-CSDL)
 *
 * @class Association
 */
class Association {
  /**
   * Creates an instance of Association.
   * @param {Object} rawMetadata raw metadata object for the association
   * @memberof Association
   */
  constructor(rawMetadata) {
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    Object.defineProperty(this, "name", {
      get: () => rawMetadata.$.Name,
    });
 
    let ends = _.get(rawMetadata, "End", []).map((p) => new AssociationEnd(p));
    Object.defineProperty(this, "ends", {
      get: () => ends,
    });
 
    if (ends.length !== 2) {
      throw new Error(
        `Association MUST have exactly two End elements defined, but it has ${ends.length}.`
      );
    }
  }
 
  /**
   * Initializes schema dependent properties. Decoupled from constructor,
   * because it needs to resolve schema (type) references.
   *
   * @param {CsdlSchema} schema to resolve references
   * @memberof EntityContainer
   */
  initSchemaDependentProperties(schema) {
    this.ends.forEach((e) => e.initSchemaDependentProperties(schema));
  }
 
  /**
   * Resolves model path within this association.
   *
   * @param {string} [path] model path
   * @returns {Object} resolved element
   * @memberof ComplexType
   */
  resolveModelPath(path) {
    return !path || path === this.name ? this : undefined;
  }
 
  /**
   * Find endpoint of the association
   *
   * @param {String} role identifer of the role which need to be find
   *
   * @returns {AssociationEnd} found endpoint of the association
   *
   * @memberof Association
   */
  findEnd(role) {
    return _.find(this.ends, (associationEnd) => associationEnd.role === role);
  }
}
 
module.exports = Association;