angular-odata
Version:
Client side OData typescript library for Angular
265 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = exports.ModelField = void 0;
const core_1 = require("@angular-devkit/core");
const base_1 = require("./base");
const schematics_1 = require("@angular-devkit/schematics");
const csdl_structural_property_1 = require("../metadata/csdl/csdl-structural-property");
const utils_1 = require("../utils");
class ModelField {
constructor(model, edmType) {
this.model = model;
this.edmType = edmType;
}
name() {
const required = !(this.edmType instanceof csdl_structural_property_1.CsdlNavigationProperty || this.edmType.Nullable);
const name = this.edmType.Name;
return name + (!required ? '?' : '');
}
type() {
const pkg = this.model.getPackage();
const enumType = pkg.findEnum(this.edmType.Type);
const entityType = pkg.findEntity(this.edmType.Type);
let type = "any";
if (enumType !== undefined) {
type = enumType.importedName;
type += this.edmType.Collection ? '[]' : '';
}
else if (entityType !== undefined) {
if (this.edmType.Collection) {
const collection = pkg.findCollection(this.edmType.Type);
const model = pkg.findModel(this.edmType.Type);
type = `${collection.importedName}<${entityType.importedName}, ${model.importedName}<${entityType.importedName}>>`;
}
else {
const model = pkg.findModel(this.edmType.Type);
type = `${model.importedName}<${entityType.importedName}>`;
}
}
else {
type = (0, utils_1.toTypescriptType)(this.edmType.Type);
type += this.edmType.Collection ? '[]' : '';
}
if (this.edmType.Nullable && !this.edmType.Collection) {
type += ' | null';
}
return type;
}
resource() {
const pkg = this.model.getPackage();
const resourceName = `$$${this.edmType.Name}`;
if (this.edmType instanceof csdl_structural_property_1.CsdlNavigationProperty) {
const entity = pkg.findEntity(this.edmType.Type);
return `public ${resourceName}() {
return this.navigationProperty<${entity === null || entity === void 0 ? void 0 : entity.importedName}>('${this.edmType.Name}');
}
`;
}
else {
return `public ${resourceName}() {
return this.property<${this.type()}>('${this.edmType.Name}');
}
`;
}
}
getter() {
const pkg = this.model.getPackage();
const getterName = `$${this.edmType.Name}`;
if (this.edmType instanceof csdl_structural_property_1.CsdlNavigationProperty) {
const entity = pkg.findEntity(this.edmType.Type);
return `public ${getterName}() {
return this.getAttribute<${entity === null || entity === void 0 ? void 0 : entity.importedName}>('${this.edmType.Name}') as ${entity === null || entity === void 0 ? void 0 : entity.importedName};
}
`;
}
else {
return `public ${getterName}() {
return this.getAttribute<${this.type()}>('${this.edmType.Name}') as ${this.type()};
}
`;
}
}
setter() {
const pkg = this.model.getPackage();
const setterName = `${this.edmType.Name}$$`;
if (this.edmType instanceof csdl_structural_property_1.CsdlNavigationProperty) {
const entity = pkg.findEntity(this.edmType.Type);
return `public ${setterName}(model: ${this.type()} | null, options?: ODataOptions) {
return this.setReference<${entity === null || entity === void 0 ? void 0 : entity.importedName}>('${this.edmType.Name}', model, options);
}
`;
}
else {
return `
`;
}
}
fetch() {
const pkg = this.model.getPackage();
const fetchName = `${this.edmType.Name}$`;
if (this.edmType instanceof csdl_structural_property_1.CsdlNavigationProperty) {
const entity = pkg.findEntity(this.edmType.Type);
return `public ${fetchName}(options?: ODataQueryArgumentsOptions<${entity === null || entity === void 0 ? void 0 : entity.importedName}>) {
return this.fetchAttribute<${entity === null || entity === void 0 ? void 0 : entity.importedName}>('${this.edmType.Name}', options) as Observable<${entity === null || entity === void 0 ? void 0 : entity.importedName}>;
}
`;
}
else {
return `public ${fetchName}(options?: ODataQueryArgumentsOptions<${this.type()}>) {
return this.fetchAttribute<${this.type()}>('${this.edmType.Name}', options) as Observable<${this.type()}>;
}
`;
}
}
isGeoSpatial() {
return this.edmType.Type.startsWith('Edm.Geography') || this.edmType.Type.startsWith('Edm.Geometry');
}
}
exports.ModelField = ModelField;
class Model extends base_1.Base {
constructor(pkg, options, edmType, entity) {
super(pkg, options);
this.edmType = edmType;
this.entity = entity;
}
entityType() {
return this.edmType.fullName();
}
template() {
return (0, schematics_1.url)('./files/model');
}
variables() {
var _a;
return {
type: this.name() + 'Model',
baseType: this.edmType.BaseType ? this.edmType.BaseType + 'Model' : null,
entity: this.entity,
fields: this.fields(),
hasGeoFields: this.hasGeoFields(),
geoFields: this.geoFields(),
callables: (_a = this.callables) !== null && _a !== void 0 ? _a : [],
navigations: this.navitations(),
};
}
name() {
return core_1.strings.classify(this.edmType.name()) + "Model";
}
fileName() {
return (core_1.strings.dasherize(this.edmType.name()) + '.model');
}
directory() {
return this.edmType.namespace().replace(/\./g, '/');
}
fullName() {
return this.edmType.fullName() + "Model";
}
importTypes() {
var _a, _b, _c, _d, _e, _f, _g, _h;
const pkg = this.getPackage();
const imports = [
this.entity.fullName()
];
if (this.edmType.BaseType) {
imports.push(this.edmType.BaseType);
imports.push(this.edmType.BaseType + 'Model');
}
for (let prop of (_b = (_a = this.edmType) === null || _a === void 0 ? void 0 : _a.Property) !== null && _b !== void 0 ? _b : []) {
if (!prop.Type.startsWith('Edm.')) {
imports.push(prop.Type);
imports.push(prop.Type + 'Model');
if (prop.Collection) {
imports.push(prop.Type + 'Collection');
}
}
}
for (let prop of (_d = (_c = this.edmType) === null || _c === void 0 ? void 0 : _c.NavigationProperty) !== null && _d !== void 0 ? _d : []) {
if (!prop.Type.startsWith('Edm.')) {
imports.push(prop.Type);
imports.push(prop.Type + 'Model');
if (prop.Collection) {
imports.push(prop.Type + 'Collection');
}
}
}
for (let callable of (_e = this.callables) !== null && _e !== void 0 ? _e : []) {
imports.push(...callable.importTypes());
}
const service = pkg.findEntitySet(this.edmType.fullName());
if (service) {
imports.push(...((_f = service.NavigationPropertyBinding) !== null && _f !== void 0 ? _f : []).map(b => b.entityType()));
imports.push(...((_g = service.NavigationPropertyBinding) !== null && _g !== void 0 ? _g : [])
.map(b => b.resolvePropertyType((fullName) => pkg.findEntityType(fullName)))
.filter(e => e)
.map(e => e.fullName()));
imports.push(...((_h = service.NavigationPropertyBinding) !== null && _h !== void 0 ? _h : [])
.map(b => b.resolveNavigationPropertyType((fullName) => pkg.findEntityType(fullName)))
.filter(e => e)
.map(e => e.Type));
}
return imports;
}
navitations() {
var _a, _b;
const pkg = this.getPackage();
const service = pkg.findEntitySet(this.edmType.fullName());
if (service) {
const navigations = [];
let properties = [];
let entity = this.edmType;
while (entity) {
properties = [...properties, ...((_a = this.edmType.NavigationProperty) !== null && _a !== void 0 ? _a : [])];
if (!entity.BaseType) {
break;
}
entity = this.getPackage().findEntityType(entity.BaseType);
}
let bindings = (_b = service.NavigationPropertyBinding) === null || _b === void 0 ? void 0 : _b.filter(binding => properties.every(p => {
const nav = binding.resolveNavigationPropertyType((fullName) => pkg.findEntityType(fullName));
return p.Name !== (nav === null || nav === void 0 ? void 0 : nav.Name);
}));
return this.renderNavigationPropertyBindings(bindings);
}
return [];
}
renderNavigationPropertyBindings(bindings) {
const pkg = this.getPackage();
let result = [];
let casts = [];
for (let binding of bindings !== null && bindings !== void 0 ? bindings : []) {
const nav = binding.resolveNavigationPropertyType((fullName) => pkg.findEntityType(fullName));
const isCollection = nav ? nav.Collection : false;
const navEntity = nav ? pkg.findEntityType(nav.Type) : undefined;
const bindingEntity = pkg.findEntityType(binding.entityType());
const propertyEntity = binding.resolvePropertyType((fullName) => pkg.findEntityType(fullName));
const entity = pkg.findEntity((navEntity === null || navEntity === void 0 ? void 0 : navEntity.fullName()) || '');
if (propertyEntity && bindingEntity && false) {
}
else {
const returnType = isCollection ? `ODataCollection<${entity === null || entity === void 0 ? void 0 : entity.importedName}, ODataModel<${entity === null || entity === void 0 ? void 0 : entity.importedName}>>` : `ODataModel<${entity === null || entity === void 0 ? void 0 : entity.importedName}>`;
var responseType = isCollection ? "collection" : "model";
var methodName = `as${propertyEntity === null || propertyEntity === void 0 ? void 0 : propertyEntity.Name}` + (nav === null || nav === void 0 ? void 0 : nav.Name.substring(0, 1).toUpperCase()) + (nav === null || nav === void 0 ? void 0 : nav.Name.substring(1));
var castEntity = pkg.findEntity((propertyEntity === null || propertyEntity === void 0 ? void 0 : propertyEntity.fullName()) || '');
// Navigation
result.push(`public ${methodName}(options?: ODataQueryArgumentsOptions<${entity === null || entity === void 0 ? void 0 : entity.importedName}>) {
return this.fetchNavigationProperty<${entity === null || entity === void 0 ? void 0 : entity.importedName}>('${binding.Path}', '${responseType}', options) as Observable<${returnType}>;
}`);
}
}
return result;
}
fields() {
var _a, _b;
return [
...((_a = this.edmType.Property) !== null && _a !== void 0 ? _a : []).map((p) => new ModelField(this, p)),
...((_b = this.edmType.NavigationProperty) !== null && _b !== void 0 ? _b : []).map((p) => new ModelField(this, p)),
];
}
geoFields() {
return this.fields().filter((p) => p.isGeoSpatial());
}
hasGeoFields() {
return this.geoFields().length > 0;
}
}
exports.Model = Model;
//# sourceMappingURL=model.js.map