UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

117 lines (96 loc) 3.43 kB
'use strict'; const InternalError = require('./../utils/errors/internalError'); function NavExp() { this.showAllProperties = true; //default show property, and navprop as link this.expandAllAvailExpands = true; //default expand all available navigations from $expand query this.isStar = false; //overwrite the .properties this.properties = []; // explicitly shown properties this.expandNavigations = []; // explicitly expanded navigations, means // the navigation is defined in $expand and $select this.availableExpands = {}; // available expands defined by $expand } NavExp.prototype.toPureJsonObject = function () { const ret = { showAllProperties: this.showAllProperties, expandAllAvailExpands: this.expandAllAvailExpands, isStar: this.isStar, properties: this.properties, expandNavigations: this.expandNavigations, availableExpands: {}, }; for (const exp in this.availableExpands) { if (this.availableExpands.hasOwnProperty(exp)) { ret.availableExpands[exp] = this.availableExpands[exp].toPureJsonObject(); } } return ret; }; NavExp.prototype.getNav = function (name) { return this.availableExpands[name]; }; NavExp.prototype.addNav = function (name, nav) { this.availableExpands[name] = nav; return this.availableExpands[name]; }; NavExp.prototype.applySelect = function (select) { const part = select.shift(); if (part === '*') { this.isStar = true; this.expandAllAvailExpands = false; } else { this.showAllProperties = false; this.expandAllAvailExpands = false; const nav = this.availableExpands[part]; if (nav) { if (this.properties.indexOf(part) === -1) { this.properties.push(part); //also a property which is not in the availNavigation list can be a navigation property } if (this.expandNavigations.indexOf(part) === -1) { this.expandNavigations.push(part); } if (select.length > 0) { nav.applySelect(select); } } else { if (this.properties.indexOf(part) === -1) { this.properties.push(part); //also a property which is not in the availNavigation list can be a navigation property } if (select.length > 0) { throw new InternalError('part to long'); } } } }; function processSelects(selects, tree) { for (const select of selects) { tree.applySelect(select); } } function createExpandTree(expandClauses) { const tree = new NavExp(); if (expandClauses) { for (const expandClause of expandClauses) { let root = tree; for (const expandItem of expandClause) { const nav = root.getNav(expandItem); if (nav !== undefined) { root = nav; } else { root = root.addNav(expandItem, new NavExp()); } } } } return tree; } function processExpandSelectTree(expands, selects) { const tree = createExpandTree(expands); if (selects) { processSelects(selects, tree); } return tree; } exports.processExpandSelectTree = processExpandSelectTree;