pailingual-odata
Version:
TypeScript client for OData v4 services
139 lines • 5.82 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./singleSource", "./serialization", "./executable", "./utils"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const singleSource_1 = require("./singleSource");
const serialization_1 = require("./serialization");
const executable_1 = require("./executable");
const helpers = require("./utils");
class CollectionSource extends executable_1.ExecutableAndCount {
constructor(__metadata, __apiMetadata, query) {
super(query);
this.__metadata = __metadata;
this.__apiMetadata = __apiMetadata;
this._generateOperationsProperties();
}
_generateOperationsProperties() {
if (this.__apiMetadata) {
helpers.generateOperations(this, () => this.query, this.__apiMetadata, this.__metadata, true);
}
}
$byKey(key) {
let 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);
}
getExpressionByKeyValue(value) {
let md = this.__metadata;
let 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');
let keyType = md.properties[keys[0]].type;
let res = serialization_1.serializeValue(value, keyType, true);
if (!res)
throw new Error("Key must be not null value");
return res;
}
getExpressionByValues(values) {
var res = new Array();
for (let prop in values) {
let propMetadata = this.__metadata.properties[prop];
if (!propMetadata)
throw new Error(`Property '${prop}' for entity '${this.__metadata.name}' not found.`);
let value = serialization_1.serializeValue(values[prop], propMetadata.type, true);
let exp = `${prop}=${value}`;
res.push(exp);
}
return res.join(",");
}
$cast(fullTypeName) {
const typeMetadata = this.__apiMetadata.getEdmTypeMetadata(fullTypeName);
if (!typeMetadata)
throw new Error(`EntityType '${fullTypeName}' not found.`);
const q = this.query.cast(fullTypeName);
return new CollectionSource(typeMetadata, this.__apiMetadata, q);
}
$count() {
const query = this.query.count();
return new executable_1.Executable(query);
}
$delete(key) {
return this.$byKey(key).$delete();
}
$expand(prop, exp) {
const q = this.query.expand(prop, exp);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$filter(expr) {
const q = this.query.filter(expr);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$orderBy(...fields) {
return this.orderByImpl(fields);
}
$orderByDesc(...fields) {
return this.orderByImpl(fields, true);
}
orderByImpl(fields, desc = false) {
var expressions = fields.map(f => {
let exp = (typeof f == "string")
? f
: helpers.buildPathExpression(f, this.__metadata, this.__apiMetadata);
if (desc)
exp += " desc";
return exp;
});
const q = this.query.orderBy(expressions);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$insert(obj) {
const q = this.query.insert(obj);
return new executable_1.Executable(q);
}
$patch(key, obj) {
return this.$byKey(key).$patch(obj);
}
$top(num) {
const q = this.query.top(num);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$skip(num) {
const q = this.query.skip(num);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$search(expr) {
const q = this.query.search(expr);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$select(...fields) {
const q = this.query.select(fields);
return new CollectionSource(this.__metadata, this.__apiMetadata, q);
}
$unsafeExpand(exp) {
return this.$expand(exp);
}
$update(key, obj) {
return this.$byKey(key).$update(obj);
}
$urlWithCount() {
return this.query.count({ inline: true }).url();
}
}
exports.CollectionSource = CollectionSource;
});
//# sourceMappingURL=collectionSource.js.map