UNPKG

breeze2-odata4

Version:

Library to allow OData 4 support for breezejs 2

123 lines 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ts_odatajs_1 = require("ts-odatajs"); var NavigationAdapter = (function () { function NavigationAdapter() { this.associations = {}; } NavigationAdapter.prototype.adapt = function (metadata) { this.metadata = metadata; this.entityContainer = ts_odatajs_1.oData.utils.lookupDefaultEntityContainer(this.metadata.schema); ts_odatajs_1.oData.utils.forEachSchema(this.metadata.schema, this.adaptSchema.bind(this)); }; NavigationAdapter.prototype.adaptSchema = function (schema) { var _this = this; this.associations = {}; var entityTypes = schema.entityType || []; entityTypes.forEach(function (e) { return _this.adaptEntityType(schema, e); }); }; NavigationAdapter.prototype.adaptEntityType = function (schema, entityType) { var _this = this; (entityType.navigationProperty || []) .forEach(function (n) { return _this.adaptNavigationProperty(schema, entityType.name, n); }); this.setAssociations(schema); }; NavigationAdapter.prototype.adaptNavigationProperty = function (schema, entityTypeName, navProp) { var namespace = schema.namespace; var isCollection = ts_odatajs_1.oData.utils.isCollectionType(navProp.type); var fullType = ts_odatajs_1.oData.utils.getCollectionType(navProp.type) || navProp.type; var shortType = fullType.split('.').pop(); var sourceType = isCollection ? shortType : entityTypeName; var targetType = isCollection ? entityTypeName : shortType; var assoc = this.getAssociation(namespace, sourceType, targetType); var fullTargetTypeName = namespace + "." + targetType; var targetEntityType = ts_odatajs_1.oData.utils.lookupEntityType(fullTargetTypeName, this.metadata.schema); if (targetEntityType === null) { throw new Error("Could not find entity with type name " + fullTargetTypeName); } if (!assoc.referentialConstraint) { assoc.referentialConstraint = this.getReferentialConstraint(navProp, assoc.name, targetEntityType); } this.setNavigationRoles(navProp, namespace, assoc.name); }; NavigationAdapter.prototype.getReferentialConstraint = function (navProp, assocName, targetEntityType) { var isCollection = ts_odatajs_1.oData.utils.isCollectionType(navProp.type); if (isCollection) { return null; } var targetKey = targetEntityType.key.propertyRef; var sourceKey = targetKey; var constraint = (navProp.referentialConstraint || [])[0]; if (constraint) { sourceKey = [{ name: constraint.property }]; targetKey = [{ name: constraint.referencedProperty }]; } var result = { dependent: { propertyRef: sourceKey, role: assocName + "_Source" }, principal: { propertyRef: targetKey, role: assocName + "_Target" } }; return result; }; NavigationAdapter.prototype.setNavigationRoles = function (navProp, namespace, assocName) { if (!this.associations[assocName]) { return; } var isCollection = ts_odatajs_1.oData.utils.isCollectionType(navProp.type); navProp.relationship = namespace + "." + assocName; navProp.fromRole = assocName + (isCollection ? '_Target' : '_Source'); navProp.toRole = assocName + (isCollection ? '_Source' : '_Target'); }; NavigationAdapter.prototype.getAssociation = function (namespace, firstType, secondType) { var assocName1 = firstType + "_" + secondType; var assocName2 = secondType + "_" + firstType; var assoc = this.associations[assocName1] || this.associations[assocName2]; if (assoc) { return assoc; } var fullSourceTypeName = namespace + "." + firstType; var fullTargetTypeName = namespace + "." + secondType; assoc = { association: assocName1, name: assocName1, end: [ { entitySet: this.getEntitySetNameByEntityType(fullSourceTypeName), multiplicity: '*', role: assocName1 + "_Source", type: fullSourceTypeName }, { entitySet: this.getEntitySetNameByEntityType(fullTargetTypeName), multiplicity: '1', role: assocName1 + "_Target", type: fullTargetTypeName } ], referentialConstraint: null }; this.associations[assoc.name] = assoc; return assoc; }; NavigationAdapter.prototype.getEntitySetNameByEntityType = function (entityType) { var set = this.entityContainer.entitySet.find(function (s) { return s.entityType === entityType; }); return set && set.name; }; NavigationAdapter.prototype.setAssociations = function (schema) { var assoc = []; for (var key in this.associations) { assoc.push(this.associations[key]); } schema.association = assoc; this.entityContainer.associationSet = assoc; }; return NavigationAdapter; }()); exports.NavigationAdapter = NavigationAdapter; //# sourceMappingURL=navigation-adapter.js.map