Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add complex type configuration #157

Open
wants to merge 2 commits into
base: lts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion data-application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import { IApplication, ConfigurationBase,
ApplicationService, ApplicationBase, ApplicationServiceConstructor, SequentialEventEmitter } from "@themost/common";
import {DataContext} from "./types";
import {SyncSeriesEventEmitter} from '@themost/events';

export declare class DataApplication extends SequentialEventEmitter implements ApplicationBase {
serviceLoaded: SyncSeriesEventEmitter<{ target: DataApplication, serviceType: ApplicationServiceConstructor<any>, service: any }>;
constructor(cwd?: string);
configuration: ConfigurationBase;
useStrategy(serviceCtor: ApplicationServiceConstructor<any>, strategyCtor: ApplicationServiceConstructor<any>): this;
Expand All @@ -12,4 +14,4 @@ export declare class DataApplication extends SequentialEventEmitter implements A
getService<T>(serviceCtor: ApplicationServiceConstructor<T>): T;
getConfiguration(): ConfigurationBase;
createContext(): DataContext;
}
}
7 changes: 7 additions & 0 deletions data-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
var {Args, PathUtils, SequentialEventEmitter} = require('@themost/common');
var {DataConfiguration} = require('./data-configuration');
var {DefaultDataContext} = require('./data-context');
var { SyncSeriesEventEmitter } = require('@themost/events')
/**
* @class
* @param {string} cwd - A string which defines application root directory
*/
class DataApplication extends SequentialEventEmitter {
constructor(cwd) {
super();
this.serviceLoaded = new SyncSeriesEventEmitter();
Object.defineProperty(this, '_services', {
configurable: true,
enumerable: false,
Expand Down Expand Up @@ -51,6 +53,11 @@ class DataApplication extends SequentialEventEmitter {
writable: true,
value: new strategyCtor(this)
});
this.serviceLoaded.emit({
target: this,
serviceType: serviceCtor,
service: this._services[serviceCtor.name]
});
return this;
}
useService(serviceCtor) {
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ module.exports = {
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
moduleNameMapper: {
"@themost/data": "<rootDir>/index",
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down
33 changes: 33 additions & 0 deletions odata.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
import {DataContext} from "./types";
import {ConfigurationBase} from "@themost/common";
import {AsyncSeriesEventEmitter} from '@themost/events';

export declare interface SystemQueryOptions {
$filter?: string;
Expand Down Expand Up @@ -73,15 +74,22 @@ export declare interface ProcedureParameter {
fromBody?: boolean;
}

export declare interface Annotation {
term: string;
value: any;
}

export declare interface EntityTypeProperty {
name: string;
type: string;
nullable?: boolean;
annotations?: Annotation[];
}

export declare interface EntityTypeNavigationProperty {
name: string;
type: string;
annotations?: Annotation[];
}

export declare interface EntityContainerConfiguration {
Expand Down Expand Up @@ -131,6 +139,7 @@ export declare class EntityTypeConfiguration {
navigationProperty: Array<EntityTypeNavigationProperty>;
actions: Array<ActionConfiguration>;
functions: Array<FunctionConfiguration>;
annotations?: Annotation[];
collection: any;
ignore(name: string): EntityTypeConfiguration;
derivesFrom(name: string): EntityTypeConfiguration;
Expand Down Expand Up @@ -182,6 +191,7 @@ export declare interface ModelBuilderJsonFormatterOptions {
}

export declare class ODataModelBuilder {
loaded: AsyncSeriesEventEmitter<{target: ODataModelBuilder}>;
constructor(configuration: ConfigurationBase);
serviceRoot: string;
defaultNamespace: string;
Expand Down Expand Up @@ -214,3 +224,26 @@ export declare class ODataConventionModelBuilder extends ODataModelBuilder{
}

export declare function defineDecorator(proto: Object|Function, key: string, decorator:Function): void;

export declare interface ComplexTypeProperty extends EntityTypeProperty {
}

export declare interface ComplexTypeNavigationProperty extends EntityTypeNavigationProperty {
}

export declare class ComplexTypeConfiguration {
constructor(builder: any, name: string);
getBuilder(): any;
name: string;
openType: boolean;
abstract: boolean;
baseType: string;
property: Array<ComplexTypeProperty>;
navigationProperty: Array<ComplexTypeNavigationProperty>;
annotations?: Annotation[];
addProperty (name: string, type: string, nullable?: boolean): ComplexTypeConfiguration;
removeProperty(name: string): ComplexTypeConfiguration;
addNavigationProperty(name: string, type: string, multiplicity?: 'Many' | 'ZeroOrOne' | 'Unknown' | 'One'): ComplexTypeConfiguration;
removeNavigationProperty(name: string): ComplexTypeConfiguration;

}
125 changes: 120 additions & 5 deletions odata.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var {DefaultSchemaLoaderStrategy} = require('./data-configuration');
var {instanceOf} = require('./instance-of');
var {Args} = require('@themost/common');
var {hasOwnProperty} = require('./has-own-property');
var { AsyncSeriesEventEmitter } = require('@themost/events');
/**
* @enum
*/
Expand Down Expand Up @@ -342,7 +343,7 @@ EntityTypeConfiguration.prototype.getBuilder = function() {
* @returns EntityTypeConfiguration
*/
EntityTypeConfiguration.prototype.derivesFrom = function(name) {
Args.notString(name,'Enity type name');
Args.notString(name,'Entity type name');
this.baseType = name;
return this;
};
Expand Down Expand Up @@ -1211,7 +1212,7 @@ function schemaToEdmDocument(schema) {
/**
* @classdesc Represents the OData model builder of an HTTP application
* @property {string} serviceRoot - Gets or sets the service root URI
* @param {ConfigurationBase} configuration
* @param {import('@themost/common').ConfigurationBase} configuration
* @class
*/
function ODataModelBuilder(configuration) {
Expand All @@ -1221,8 +1222,9 @@ function ODataModelBuilder(configuration) {
this[entityContainerProperty] = [];
this.defaultNamespace = null;
this.defaultAlias = null;
this.loaded = new AsyncSeriesEventEmitter();
/**
* @returns {ConfigurationBase}
* @returns {import('@themost/common').ConfigurationBase}
*/
this.getConfiguration = function() {
return configuration;
Expand Down Expand Up @@ -1495,7 +1497,7 @@ ODataModelBuilder.prototype.getEdmSync = function() {
*/
ODataModelBuilder.prototype.getEdmDocument = function() {
var self = this;
return Q.promise(function(resolve, reject) {
return new Q.promise(function(resolve, reject) {
try{
return self.getEdm().then(function(schema) {
var doc = schemaToEdmDocument.bind(self)(schema);
Expand Down Expand Up @@ -2386,6 +2388,118 @@ EdmMapping.getOwnActions = function(obj) {
});
};

/**
* @param {ODataModelBuilder} builder
* @param {string} name
* @constructor
*/
function ComplexTypeConfiguration(builder, name) {
this.name = name;
this.abstract = false;
this.openType = false;
this.property = [];
this.navigationProperty = [];
// set builder
this[builderProperty] = builder;
}

/**
* @param {string} name
* @param {string} type
* @param {boolean=} nullable
* @returns {ComplexTypeConfiguration}
*/
ComplexTypeConfiguration.prototype.addProperty = function (name, type, nullable) {
const property = this.property.find(function(property) { return property.name === name; });
if (property) {
Object.assign(property, { type, nullable });
return this;
}
this.property.push({
'name': name,
'type': type,
'nullable': nullable
});
return this;
}
/**
* @param {string} name
* @param {string} type
* @param {('Many' | 'One' | 'Unknown' | 'ZeroOrOne')=} multiplicity
* @return {ComplexTypeConfiguration}
*/
ComplexTypeConfiguration.prototype.addNavigationProperty = function (name, type, multiplicity) {
const navigationProperty = this.navigationProperty.find(function(property) { return property.name === name; });
if (navigationProperty) {
Object.assign(navigationProperty, {
'type': multiplicity === 'Many' ? sprintf('Collection(%s)', type) : type
});
return this;
}
this.navigationProperty.push({
'name': name,
'type': multiplicity === 'Many' ? sprintf('Collection(%s)', type) : type,
});
return this;
}
/**
* @param {string} name
* @return {ComplexTypeConfiguration}
*/
ComplexTypeConfiguration.prototype.removeProperty = function(name) {
Args.notString(name,'Property name');
var hasProperty =this.property.findIndex( function(x) {
return x.name === name;
});
if (hasProperty>-1) {
this.property.splice(hasProperty, 1);
}
return this;
};
/**
* @param {string} name
* @return {ComplexTypeConfiguration}
*/
ComplexTypeConfiguration.prototype.removeNavigationProperty = function(name) {
Args.notString(name,'Property name');
var hasProperty =this.navigationProperty.findIndex( function(x) {
return x.name === name;
});
if (hasProperty>-1) {
this.navigationProperty.splice(hasProperty, 1);
}
return this;
};
/**
*
* @param {import('@themost/xml').XNode} element
*/
ComplexTypeConfiguration.prototype.writeXml = function (element) {
/**
* @type {import('@themost/xml').XDocument}
*/
const document = element.ownerDocument;
const complextType = document.createElement('ComplexType');
complextType.setAttribute('Name', this.name);
complextType.setAttribute('Abstract', this.abstract);
complextType.setAttribute('OpenType', this.openType);!Paro
// add properties
this.property.forEach(function(property) {
const propertyElement = document.createElement('Property');
propertyElement.setAttribute('Name', property.name);
propertyElement.setAttribute('Type', property.type);
propertyElement.setAttribute('Nullable', property.nullable);
complextType.appendChild(propertyElement);
});
// add navigation properties
this.navigationProperty.forEach(function(property) {
const propertyElement = document.createElement('NavigationProperty');
propertyElement.setAttribute('Name', property.name);
propertyElement.setAttribute('Type', property.type);
complextType.appendChild(propertyElement);
});
}


//exports
module.exports = {
Expand All @@ -2401,6 +2515,7 @@ module.exports = {
ODataModelBuilder,
ODataConventionModelBuilder,
EdmMapping,
defineDecorator
defineDecorator,
ComplexTypeConfiguration
}

Loading