UNPKG

@odata2ts/odata-query-objects

Version:

Q-Objects are the magic sauce for the odata-query-builder and allow for renaming and type conversion

56 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QEntityCollectionPath = void 0; const QFilterExpression_1 = require("../QFilterExpression"); const QOrderByExpression_1 = require("../QOrderByExpression"); class QEntityCollectionPath { constructor(path, qEntityFn) { this.path = path; this.qEntityFn = qEntityFn; if (!path || !path.trim()) { throw new Error("Path must be supplied!"); } if (!qEntityFn || typeof qEntityFn !== "function") { throw new Error("Function which returns query object must be supplied!"); } } getPath() { return this.path; } getEntity(withPrefix = false) { return new (this.qEntityFn())(withPrefix ? this.path : undefined); } isCollectionType() { return true; } lambdaFunction(operationName, fn, prefix = "a") { // create new qObject with given prefix const qEntity = new (this.qEntityFn())(prefix); const expression = fn ? fn(qEntity) : undefined; // if no expression was provided => function call without args if (!expression || !expression.toString()) { return new QFilterExpression_1.QFilterExpression(`${this.path}/${operationName}()`); } return new QFilterExpression_1.QFilterExpression(`${this.path}/${operationName}(${prefix}:${expression})`); } any(fn, prefix) { return this.lambdaFunction("any" /* LambdaFunctions.ANY */, fn, prefix); } all(fn, prefix) { return this.lambdaFunction("all" /* LambdaFunctions.ALL */, fn, prefix); } /** * Sort by total count of these collection items in ascending order. */ countAsc() { return new QOrderByExpression_1.QOrderByExpression(`${this.getPath()}/$count asc`); } /** * Sort by total count of these collection items in ascending order. */ countDesc() { return new QOrderByExpression_1.QOrderByExpression(`${this.getPath()}/$count desc`); } } exports.QEntityCollectionPath = QEntityCollectionPath; //# sourceMappingURL=QEntityCollectionPath.js.map