All files / lib/model Metadata.js

100% Statements 26/26
100% Branches 6/6
100% Functions 14/14
100% Lines 26/26

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 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196    1x                   8x 8x   8x 2x     6x                                                         7x 14x 7x 7x   7x   7x 2x     7x 12x         2x     2x                         2x     2x                                                         2x 2x                                                                                                       5x 3x                                     1x 1x             1x  
"use strict";
 
const EdmxModel = require("./EdmxModel");
 
/**
 * Some api functions have (namespace, name) parameters, but allows to enter just namespace which is treated as name then.
 *
 * @param {String} [namespace] namespace or name, if name not set
 * @param {String} [name] name or undefined
 * @returns {Object} object with real name and namespace properties
 */
function fromMaybeNameOrNamespace(namespace, name) {
  let realName = name ? name : namespace;
  let realNamespace = name ? namespace : undefined;
 
  if (!realName) {
    throw new Error("Missing name parameter");
  }
 
  return {
    name: realName,
    namespace: realNamespace,
    fullName: [realNamespace, realName].filter(Boolean).join("."),
  };
}
 
/**
 * Implements helpers to get metadata informations
 *
 * <h3>Overview</h3>
 *
 * <h3>Properties</h3>
 *
 * <ul>
 *   <li>raw - metadata in JSON format passed as parameter to constructor
 *   <li>model - metadata model constructed from raw metadata
 * </ul>
 *
 * @class Metadata
 */
class Metadata {
  /**
   * Creates an instance of <code>lib/Metadata</code> class.
   * @param {Object[]} [rawMetadata] list of metadata content fetched from the service in JSON format from xml2js
   * @param {Object} [settings] settings for the metadata
   * @memberof Metadata
   */
  constructor(rawMetadata, settings) {
    let model = rawMetadata
      .map((r) => new EdmxModel(r, settings))
      .sort((a, b) => !!b.EntityContainer - !!a.EntityContainer)
      .reduce((acc, val) => acc.merge(val));
 
    model.applySchemaExtensions(settings);
 
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    Object.defineProperty(this, "model", {
      get: () => model,
    });
  }
 
  listEntitySetNames(namespace) {
    return this.model
      .getSchema(namespace)
      .getEntityContainer()
      .entitySets.map((s) => s.name);
  }
 
  /**
   * Create list of FunctionImport names
   *
   * @param {String} [namespace] is used to specify service namespace which contains EntitySets
   *
   * @returns {[String]} returns list of FunctionImport names
   *
   * @memberof Metadata
   */
  listFunctionImportNames(namespace) {
    return this.model
      .getSchema(namespace)
      .getEntityContainer()
      .functionImports.map((fi) => fi.name);
  }
 
  /**
   * Create structure for EntitySet definition. All keys has to be defined
   * and has to be generated by by engine.
   *
   * @param {String} [namespace] namespace or name, if name not set
   * @param {String} [name] name or undefined
   *
   * @returns {Object} returns structure which define entity set posibilities
   *
   * @example
   *	{
   *		"Name": "C_AllocationCycleTP",
   *		"EntityType": "FCO_MANAGE_ALLOCATION_SRV.C_AllocationCycleTPType",
   *		"Creatable": true,
   *		"Updatable": true,
   *		"Deletable": true,
   *		"Pageable": true,
   *		"Addressable": true,
   *		"Countable": true,
   *		"Searchable": false
   *	}
   *
   * @memberof Metadata
   */
  getEntitySet(namespace, name) {
    // the api should have different order of parameters, I think it should be obsoleted once there is alternative (probably in CsdlSchema)
    let target = fromMaybeNameOrNamespace(namespace, name);
    return this.model
      .getSchema(target.namespace)
      .getEntityContainer()
      .getEntitySet(target.name)
      .getLegacyApiObject();
  }
 
  /**
   * Get definition of the FunctionImport based on the metadata
   *
   * @param {String} [namespace] namespace or name, if name not set
   * @param {String} [name] name or undefined
   *
   * @returns {Object} returns structure which define FunctionImport properties and posibilities
   * @example
   * //Structure created by the getFunctionImport
   *{
   * 	"Name": "CopyAllocationSegment",
   * 	"ReturnType" : "Edm.Boolean",
   * 	"HttpMethod": "POST",
   * 	"Parameter": [{
   * 				"Name": "DraftUUID",
   * 				"Type": "Edm.Guid",
   * 				"Mode": "In"
   * 		},
   * 		{
   * 				"Name": "AllocationSegment",
   * 				"Type": "Edm.String",
   * 				"Mode": "In"
   * 		},
   * 		{
   * 				"Name": "AllocationStartDate",
   * 				"Type": "Edm.DateTime",
   * 				"Mode": "In",
   * 				"Precision": "0"
   * 		},
   * 		{
   * 				"Name": "AllocationCycle",
   * 				"Type": "Edm.String",
   * 				"Mode": "In"
   * 		},
   * 		{
   * 				"Name": "AllocationType",
   * 				"Type": "Edm.String",
   * 				"Mode": "In"
   * 		}
   * 	]
   *}
   * @memberof Metadata
   */
  getFunctionImport(namespace, name) {
    // the api should have different order of parameters, I think it should be obsoleted once there is alternative (probably in CsdlSchema)
    let target = fromMaybeNameOrNamespace(namespace, name);
    return this.model
      .getSchema(target.namespace)
      .getEntityContainer()
      .getFunctionImport(target.name)
      .getLegacyApiObject();
  }
 
  /**
   * Collect information about EntityType from metadata
   *
   * @param {String} [namespace] namespace or name, if name not set
   * @param {String} [name] name or undefined
   *
   * @returns {Object} returns structure which define EntityType structure
   *
   * @memberof Metadata
   */
  getEntityType(namespace, name) {
    // the api should have different order of parameters, I think it should be obsoleted once there is alternative (probably in CsdlSchema)
    let target = fromMaybeNameOrNamespace(namespace, name);
    return this.model
      .getSchema(target.namespace)
      .getEntityType(target.name)
      .getLegacyApiObject();
  }
}
 
module.exports = Metadata;