pailingual-odata
Version:
TypeScript client for OData v4 services
160 lines (159 loc) • 6.95 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var singleSource_1 = require("./singleSource");
var serialization_1 = require("./serialization");
var executable_1 = require("./executable");
var helpers = require("./utils");
var CollectionSource = /** @class */ (function (_super) {
__extends(CollectionSource, _super);
function CollectionSource(__metadata, __apiMetadata, query) {
var _this = _super.call(this, query) || this;
_this.__metadata = __metadata;
_this.__apiMetadata = __apiMetadata;
_this._generateOperationsProperties();
return _this;
}
CollectionSource.prototype._generateOperationsProperties = function () {
var _this = this;
if (this.__apiMetadata) {
helpers.generateOperations(this, function () { return _this.query; }, this.__apiMetadata, this.__metadata, true);
}
};
CollectionSource.prototype.$byKey = function (key) {
var expression = (typeof key === "object" && !(key instanceof Date))
? this.getExpressionByValues(key)
: this.getExpressionByKeyValue(key);
var query = this.query.byKey(expression);
return new singleSource_1.SingleSource(this.__metadata, this.__apiMetadata, query);
};
CollectionSource.prototype.getExpressionByKeyValue = function (value) {
var md = this.__metadata;
var keys = this.__metadata.keys;
while (md.baseType && !keys) {
md = md.baseType;
keys = md.keys;
}
if (!keys)
throw new Error("Metadata: Keys not defined for entity type '" + md.name + "'");
if (keys.length > 1)
throw new Error('For entity with composite key use named parameters');
var keyType = md.properties[keys[0]].type;
var res = serialization_1.serializeValue(value, keyType, true);
if (!res)
throw new Error("Key must be not null value");
return res;
};
CollectionSource.prototype.getExpressionByValues = function (values) {
var res = new Array();
for (var prop in values) {
var propMetadata = this.__metadata.properties[prop];
if (!propMetadata)
throw new Error("Property '" + prop + "' for entity '" + this.__metadata.name + "' not found.");
var value = serialization_1.serializeValue(values[prop], propMetadata.type, true);
var exp = prop + "=" + value;
res.push(exp);
}
return res.join(",");
};
CollectionSource.prototype.$cast = function (fullTypeName) {
var typeMetadata = this.__apiMetadata.getEdmTypeMetadata(fullTypeName);
if (!typeMetadata)
throw new Error("EntityType '" + fullTypeName + "' not found.");
var q = this.query.cast(fullTypeName);
return new CollectionSource(typeMetadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$count = function () {
var query = this.query.count();
return new executable_1.Executable(query);
};
CollectionSource.prototype.$delete = function (key) {
return this.$byKey(key).$delete();
};
CollectionSource.prototype.$expand = function (prop, exp) {
var q = this.query.expand(prop, exp);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$filter = function (expr) {
var q = this.query.filter(expr);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$orderBy = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return this.orderByImpl(fields);
};
CollectionSource.prototype.$orderByDesc = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
return this.orderByImpl(fields, true);
};
CollectionSource.prototype.orderByImpl = function (fields, desc) {
var _this = this;
if (desc === void 0) { desc = false; }
var expressions = fields.map(function (f) {
var exp = (typeof f == "string")
? f
: helpers.buildPathExpression(f, _this.__metadata, _this.__apiMetadata);
if (desc)
exp += " desc";
return exp;
});
var q = this.query.orderBy(expressions);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$insert = function (obj) {
var q = this.query.insert(obj);
return new executable_1.Executable(q);
};
CollectionSource.prototype.$patch = function (key, obj) {
return this.$byKey(key).$patch(obj);
};
CollectionSource.prototype.$top = function (num) {
var q = this.query.top(num);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$skip = function (num) {
var q = this.query.skip(num);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$search = function (expr) {
var q = this.query.search(expr);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$select = function () {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i] = arguments[_i];
}
var q = this.query.select(fields);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
};
CollectionSource.prototype.$unsafeExpand = function (exp) {
return this.$expand(exp);
};
CollectionSource.prototype.$update = function (key, obj) {
return this.$byKey(key).$update(obj);
};
CollectionSource.prototype.$urlWithCount = function () {
return this.query.count({ inline: true }).url();
};
return CollectionSource;
}(executable_1.ExecutableAndCount));
exports.CollectionSource = CollectionSource;