@sap-cloud-sdk/odata-common
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
69 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
/**
* Represents a navigation property of an OData entity.
*
* OData is a relational data model, i.e. entities can be related to one another.
* For example, BusinessPartner is in a 1:n relation with BusinessPartnerAddress and in a 1:1 relation with Customer.
* Like normal properties, navigation properties can be used for selecting (expanding) and filtering.
* For example, when constructing a query on the BusinessPartner entity, an instance of `Link<BusinessPartner, Customer>`
* can be passed as argument to the select function, e.g. `BusinessPartner.TO_CUSTOMER`.
*
* NOTE: Due to historical development the Link and its extensions are used in the following way:
* OData v2 entity: 1:N is a {@link Link}, 1:0..1 is a {@link OneToOneLink}
* OData v4 entity: 1:N is a {@link OneToManyLink}, 1:0..1 is a {@link OneToOneLink}.
*
* See also: {@link Selectable}.
* @typeParam EntityT - Type of the entity to be linked from.
* @typeParam LinkedEntityT - Type of the entity to be linked to.
*/
class Link {
/**
* Creates an instance of Link.
* @param _fieldName - Name of the linking field to be used in the OData request.
* @param _entityApi - Entity API for building and executing the request.
* @param _linkedEntityApi - Constructor of the linked entity.
*/
constructor(_fieldName, _entityApi, _linkedEntityApi) {
this._fieldName = _fieldName;
this._entityApi = _entityApi;
this._linkedEntityApi = _linkedEntityApi;
/**
* List of selectables on the linked entity.
*/
this._selects = [];
this._expand = [];
}
/**
* Creates a selection on a linked entity. Has the same behavior as {@link @sap-cloud-sdk/odata-v2!GetAllRequestBuilder.select | GetAllRequestBuilderV2.select} and {@link @sap-cloud-sdk/odata-v4!GetByKeyRequestBuilder.select | GetByKeyRequestBuilderV4.select} but for linked entities.
*
* See also, {@link Selectable}.
* @param selects - Selection of fields or links on a linked entity.
* @returns The link itself, to facilitate method chaining.
*/
select(...selects) {
const link = this.clone();
link._selects = selects;
return link;
}
expand(...expands) {
const link = this.clone();
link._expand = expands;
return link;
}
/**
* Create a new link based on a given link.
* @typeParam EntityT - Type of the entity to be linked from.
* @typeParam LinkedEntityT - Type of the entity to be linked to.
* @returns Newly created link.
*/
clone() {
const clonedLink = new this.constructor(this._fieldName, this._entityApi, this._linkedEntityApi);
clonedLink._selects = this._selects;
clonedLink._expand = this._expand;
return clonedLink;
}
}
exports.Link = Link;
//# sourceMappingURL=link.js.map